0

I am not really familiar with batch scripts so I would really appreciate some help here! I have been trying to copy and rename a bunch of files using two txt files. The chart_list.txt file will have the origin names.

chart_list.txt
Chart_A1_86_88.png
Chart_A2_86_88.png
Chart_B1_86_88.png
Chart_B2_86_88.png

The newchart_list.txt file will have the destination names.

newchart_list.txt
Chart_A1.png
Chart_A2.png
Chart_B1.png
Chart_B2.png

Looking at the following post (in windows batch file, how to loop 2 sets simultaneously and Copy a list (txt) of files), I came up with the following :

setlocal enableextensions enabledelayedexpansion
@echo off

set src_folder=C:\Source
set dst_folder=C:\Destination
set flag=0
for /f "tokens=*" %%i in (chart_list.txt) DO (
set /A flag+=1
echo !flag!
set flag2=0
    for /f "tokens=*" %%j in (newchart_list.txt) DO (
        set /A flag2+=1
        echo !flag2!
        if !flag! == !flag2! (
        copy "%src_folder%\%%i" "%dst_folder%\%%j" /Y
        )
    )
)
@pause

The purpose is really to have the name in the first line of chart_list and newchart_list to match. However, when I execute the command nothing happens. the cmd window shows

1
0
0
0
0
2
0
0
0
0
3
0
0
0
0
4
0
0
0
0

(I added the echo command to see if flag and flag2 were actually changing but it doesn't seem to be the case for flag2 which remains set to 0).

Thanks a lot in advance for your help!

  • 3
    eg. `set flag = 0` Batch is sensitive to spaces in a `SET` statement. `SET FLAG = N` sets a variable named "FLAG[Space]" to a value of "[Space]N" `Flag[space]` is NOT the same as `Flag` – Magoo May 18 '18 at 14:46
  • @Magoo Thanks a lot! I edited the post based on your comments but cannot get flag2 to "deviate" from 0... – Samuel Champanhet May 18 '18 at 14:59
  • 1
    There's a typo: `set A/ flag2+=1` should read `set /A flag2+=1` (note the position of the `/`)... – aschipfl May 18 '18 at 15:11
  • 1
    Possible duplicate of [How can two text files be read in parallel by a batch file?](https://stackoverflow.com/questions/38214874/how-can-two-text-files-be-read-in-parallel-by-a-batch-file) or [Combining multiple text files into one](https://stackoverflow.com/questions/14521799/combining-multiple-text-files-into-one) – aschipfl May 18 '18 at 15:16
  • @aschipfl Thanks for the links and the typo. Question has been edited – Samuel Champanhet May 22 '18 at 07:00

2 Answers2

1

Here's one way of doing it:

@Echo Off
SetLocal DisableDelayedExpansion
Set "fileOld=chart_list.txt"
Set "fileNew=newchart_list.txt"
Set "dirSrce=C:\Source"
Set "dirDest=C:\Destination"

(For /F "UseBackQ Tokens=*" %%A In ("%fileOld%") Do (Set /P "B="<&3
        SetLocal EnableDelayedExpansion
        Echo(Copy /Y "%dirSrce%\%%A" "%dirDest%\!B!"
        EndLocal)) 3<"%fileNew%"
Pause

Just fill in the variables on lines 3-6 with your specific data, (do not add trailing backslahes to directory paths and do not include doublequotes or remove those which currently exist).

If you are happy with the output, you can remove Echo( from line 10 and delete the last line, to actually perform the Copy procedure.

The script does not check whether those variables are valid, exist, have matching number of lines etc. I'll leave that to you to work out as necessary.

Edit

Here's an alternative method more along the lines of your original, (but using the Find command):

@Echo Off
Setlocal EnableExtensions EnableDelayedExpansion
Set "src_folder=C:\Source"
Set "dst_folder=C:\Destination"
Set "flag=0"
For /F "UseBackQ Tokens=*" %%A In ("chart_list.txt") Do (
    Set /A flag+=1
    For /F "Tokens=1* Delims=]" %%B In ('
        "Find /N /V ""<"newchart_list.txt"|Find "[!flag!]""'
    ) Do Copy /Y "%src_folder%\%%A" "%dst_folder%\%%C")

…and without the flag or need for delayed expansion, (same method as Aacini's but using Find instead of FindStr):

@Echo Off
Set "src_folder=C:\Source"
Set "dst_folder=C:\Destination"
For /F "Tokens=1* Delims=[]" %%A In ('Find /N /V ""^<"chart_list.txt"') Do (
    For /F "Tokens=1* Delims=[]" %%C In ('Find /N /V ""^<"newchart_list.txt"'
    ) Do If "%%C"=="%%A" Copy /Y "%src_folder%\%%B" "%dst_folder%\%%D")
Compo
  • 36,585
  • 5
  • 27
  • 39
1

Simpler, but not very efficient:

@echo off
setlocal

set src_folder=C:\Source
set dst_folder=C:\Destination

for /f "tokens=1* delims=:" %%i in ('findstr /N "^" chart_list.txt') DO (
    for /f "tokens=1* delims=:" %%x in ('findstr /N "^" newchart_list.txt') DO (
        if %%i == %%x (
            copy "%src_folder%\%%j" "%dst_folder%\%%y" /Y
        )
    )
)
pause

If the files to process are large, it is better to use anyone of the other methods suggested elsewehere. See this comment

Aacini
  • 65,180
  • 12
  • 72
  • 108