Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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?

share|improve this question
    
Does cd folder copy *.txt newfolder work? –  Hennes Nov 5 '12 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 '12 at 14:13
    
You have similar answer and question [here][1] [1]: stackoverflow.com/questions/1224880/… –  mno2014 Nov 5 '12 at 14:43

1 Answer 1

up vote 3 down vote accepted

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
share|improve this answer
    
Which should be my current folder when running this? –  drum Nov 5 '12 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 '12 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 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 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 at 16:30

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.