3

I have a folder with a multitude of files, and I have a list (txt) of specific files which I want to copy from this folder.

It will go something like this: For each filename: cp (filename in folder) to (filename in new_folder)

How can I do the above using batch script?

3
  • Does cd folder copy *.txt newfolder work?
    – Hennes
    Nov 5, 2012 at 14:08
  • No, I'm not trying to copy every *.txt file, but I have a txt file which contains a list of desired files I want to copy.
    – drum
    Nov 5, 2012 at 14:13
  • You have similar answer and question [here][1] [1]: stackoverflow.com/questions/1224880/… Nov 5, 2012 at 14:43

1 Answer 1

6

Iterate over the text file:

for /f "delims=" %%L in (foo.txt) do

Copy the files:

copy "%%L" new_folder

Done:

for /f "delims=" %%L in (foo.txt) do copy "%%L" new_folder
5
  • Which should be my current folder when running this?
    – drum
    Nov 5, 2012 at 14:11
  • So for your script I need to be in the folder with all of the files. I noticed that the batch doesn't work unless I have everything in one single line.
    – drum
    Nov 5, 2012 at 14:28
  • Worked great. I put the single-line batch file and foo.txt in the same folder as the files to be copied. I generated foo.txt with dir /b > foo.txt
    – Tiberia
    Jun 7, 2015 at 16:46
  • @Tiberia, why would you bother with the text file in that case? Just use for %%F in (*) do copy "%%F" new_folder instead (or directly on the command line by replacing %%F with %F). However, in that case you can also use plain copy: copy * new_folder.
    – Joey
    Jun 8, 2015 at 6:10
  • @Joey - Because I only wanted to copy certain files in that folder (which had hundreds of files.) I generated a list of files, edited the list, and used this batch to copy them.
    – Tiberia
    Jun 8, 2015 at 16:30

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .