Using the FOR command to copy files listed in a text file

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
BBDSBD
Posts: 2
Joined: 05 Nov 2013 05:07

Using the FOR command to copy files listed in a text file

#1 Post by BBDSBD » 05 Nov 2013 05:38

Hi,

I want to be able to move selected files from a folder (and hopefully a bunch of subfolders in one directory)

This is what I have in my .bat file:
for /f "delims=" %%i in (TEST.txt) do echo D|xcopy "S:\TEST 1\%%i" "S:\TEST 2\%%i" /i /z /y

My TEST.txt document contains the following information:
1.xlsx
3.xlsx

My files I want to move are in S:\TEST 1 (3 excel files named "1","2" and "3") and i want to move them to S:\TEST 2

It copies the files but creates a new folder for each moved file named "1.xlsx" and "3.xlsx" with the file inside them.

Can somebody help me stop it doing this and just simply place the files into the folder without creating new folders?

Also if somebody knows how to do this on more than one folder at a time so it will find the file in the .txt file in one of 15 subfolders in the same location that would be great.

Kind Regards
Steven

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Using the FOR command to copy files listed in a text fil

#2 Post by foxidrive » 05 Nov 2013 06:00

This has the target %%i folder removed, and a backslash on the end of the target folder to eliminate the need for echo D


Code: Select all

for /f "delims=" %%i in (TEST.txt) do xcopy "S:\TEST 1\%%i" "S:\TEST 2\" /i /z /y



You can include the folders in your text file - but more information about what you need to copy is needed to give you specifics.

BBDSBD
Posts: 2
Joined: 05 Nov 2013 05:07

Re: Using the FOR command to copy files listed in a text fil

#3 Post by BBDSBD » 05 Nov 2013 07:20

The purpose above was just a test to see what can be done, to implement and help us with our work flow.

Currently we have a folder of images, and we have a database in excel listing all of these images with results. So say we want everything that is tagged floral in our database, we may need all of these together to put somewhere, and this allows us to pull them out easily, rather than a human sourcing the files required.

I have made the changes that you mentioned and it works great!
We are very grateful! :o

One last question is, this currently copy's the items and we sometimes may want to MOVE the files. Can this be tweaked to move?

Thank you

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Using the FOR command to copy files listed in a text fil

#4 Post by foxidrive » 05 Nov 2013 07:31

replace xcopy with move and remove the " /i /z /y"

Post Reply