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...

3
  • Please specify the reason for downvote...do I need to provide additional information? Commented Feb 24, 2014 at 8:07
  • I don't know why this was downvoted, (it is similar to some other questions) but there is nothing wrong with this question. Commented Feb 24, 2014 at 8:27
  • Does exactly one file go into each folder? Commented Feb 24, 2014 at 9:19

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

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<dirs.txt( E:\>
See the edit above - it only echos the command to the screen.
Hi @foxidrive...I tried your edit. However it is copying all the files to Folder 1 which is the 1st item in dirs.txt.
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.