0

I have two text files...files.txt containing a list of filenames and dirs.txt containing the list of directories the files need to be copied to.
This is how the files need to be copied:

File 1 ------------------------> Folder 1  
File 2 ------------------------> Folder 2  
File 3 ------------------------> Folder 3  

How do I implement this using batch? Thanks in advance...

user2966174
  • 21
  • 1
  • 6

2 Answers2

1

Try this:

@echo off
setlocal enabledelayedexpansion
for /f "delims=" %%a in (files.txt) do (
set /p dir=
echo copy "%%~a" "!dir!"
)<dirs.txt
pause

The above works - Mona can revise or remove the following:

setlocal enabledelayedexpansion
3<dirs.txt(
for /f "delims=" %%a in (files.txt) do (
set /p dir=<&3
copy "%%~a" "!dir!"
)
)

And that should do what you want. Note if dirs.txt has less lines then files.txt, this will fail.

Mona.

foxidrive
  • 40,353
  • 10
  • 53
  • 68
Monacraft
  • 6,510
  • 2
  • 17
  • 29
  • Unfortunately this doesn't seem to work.. This is what I get: `E:\>cmd.bat E:\>setlocal enabledelayedexpansion The syntax of the command is incorrect. E:\>3 ` – user2966174 Feb 24 '14 at 10:32
  • See the edit above - it only echos the command to the screen. – foxidrive Feb 24 '14 at 10:45
  • Hi @foxidrive...I tried your edit. However it is copying all the files to `Folder 1` which is the **1st** item in `dirs.txt`. – user2966174 Feb 25 '14 at 10:28
0

Well I managed to figure it out...thanks to this answer from @foxidrive. Here is the code:

@echo off
setlocal enabledelayedexpansion
set /A i=0
for /F "usebackq delims==" %%a in (files.txt) do (
set /A i+=1
call set array1[%%i%%]=%%a
call set n=%%i%%
)
set /A i=0
for /F "usebackq delims==" %%a in (dirs.txt) do (
set /A i+=1
call set array2[%%i%%]=%%a
)

for /L %%i in (1,1,%n%) do call copy "%%array1[%%i]%%" "%%array2[%%i]%%"

It's definitely not the best solution...but it works!
Thanks everyone for your help.

Community
  • 1
  • 1
user2966174
  • 21
  • 1
  • 6