Batch File: Copy Only Some Files From One Folder to the Other using a text file as reference

Posted on 2009-05-14
Last Modified: 2012-08-14
I have a text file, called TheList.txt, in which I have:

and so on... (a long list of about 400 filenames)

I have a directory in A:\FileStorage, where 1000s of PDFs reside.

I need a batch file to copy ALL files from A:\FileStorage to C:\FilesNeeded, ONLY IF the filenames match in TheList.txt

Question by:super786
  • 5
  • 4
LVL 58

Expert Comment

ID: 24391257
for /f %i in (C:\Whatever\TheList.txt) do @copy A:\FileStorage\%i C:\FilesNeeded
LVL 16

Accepted Solution

t0t0 earned 250 total points
ID: 24392880
@echo off
for /f %%a in (thelist.txt) do copy a:\filestorage\%%a c:\filesneeded\
LVL 16

Expert Comment

ID: 24397199
Thank you....
LVL 58

Expert Comment

ID: 24397913
super786, could you please explain this? Both comments are identical except that one is written for command line and another one for batch file. Even if that mattered, at least split should have been done.
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

LVL 16

Expert Comment

ID: 24399061
with the greatest of respect amit_g, the asker asked for a batch file. Your code is a command - the single '%' won't work in batch files.

You must always assume the asker knows absolutely nothing about batch files or DOS. in your case, the asker may have attempted to run your command as a batch file only to discover it did not run as expected.

.... damned good command though....

LVL 58

Expert Comment

ID: 24399651
I have already noted that distinction and while it is true, all askers are expected to be fair to all participants. At the same time you should also have noted that you are offering a amended solution to a previously mentioned comment. Anyone, even if the distinction between % and %% is not know, can see that both comments are identical and so at least a chance was warranted before awarding all credit to a later and almost identical solution.
LVL 16

Expert Comment

ID: 24400853
You are an accomplished expert and not being awarded points for this question does not in any way undermine your expertise.

>> "you are offering a amended solution to a previously mentioned comment"

This is not true. My code was written from the ground up. In most cases, every piece of code I write is fully tested before posting - even one-liners!

>> "both comments are identical"

Please take a closer look at our codes. There are NINE significant differences between our codes - and that's just ONE line of code!

>> "almost identical solution"


The asker states:

>> "I need a batch file ..."
LVL 58

Expert Comment

ID: 24403651

The comments written anytime have to be in relation to what has already been posted. While you may have written and tested your comment yourself, the fact remains when you posted your comment, you knew that a working comment has already been posted. You are sticking to the point that it would not work (and I understand that and acknowledged that already) as a "batch file" but you also knew that the only alteration was to make % to %%. While the asker may not know the difference in % and %%, you know it very well. So it would have been much better for the asker and for the poster, if you at least mentioned that the previously posted comment has to be tweaked to work in batch file.

I am not active in EE any more that to make my premium level every month and to read through some questions that interest me without even participating, so points are not an issue here. What happened here is unfair and both the asker and you did some part of it. Even then, I only asked the asker to at least explain why he chose to do so. In no way, I intended to start "my comment was better argument".
LVL 16

Expert Comment

ID: 24404020

With respect to the asker and myself, this is not the place to have this discussion.

There is nothing more that can be done at this stage. Please accept that.

>> "The comments written anytime have to be in relation to what has already been posted"

I disagree. I read the question and provided a solution.

It appeared to me your intention was to demonstrate a command-style solution rather than a batch file solution.  Knowing how 'picky' some askers can be, I tend to stick as close to the criteria as possible - if the asker requests a batch file then I provide him with a batch file (or an option of both).

>> "but you also knew that the only alteration was to make % to %%"

Here are the necessary alterations:

>> "for /f %i in (C:\Whatever\TheList.txt) do @copy A:\FileStorage\%i C:\FilesNeeded"

1) customary to start with '@echo off' as first line of code
2) must use '%%' insteade of '%'
3) customary to use '%%i' (or in your case '%i') as an index or when incrementing a loop counter - better to use '%%a' (or '%a' in your case, as it's the first available FOR variable)
4) no need to specify drive or path in FOR set
5) preferable to use 'copy' with '@cho off' rather than '@copy' in batch files
6) source should be '%%a' (or in your case '%%i') - not '%i'
7) preferable to append a trailing backslash ('\') to destination

Style and consistency comes with experience.

>> "What happened here is unfair"

If you feel that way perhaps you should have raised your concerns with a senior member of EE.

The asker does not have to enter into a discussion as to why he chose one particular solution over another.

Had your solution met the asker's requirements specification then you might of had good cause to feel dissatisfied.

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
This article was inspired by a question here at Experts Exchange ( The requirements stated in that question are (1) reduce the file size of a large number of…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now