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

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

Who is Participating?
t0t0Connect With a Mentor Commented:
@echo off
for /f %%a in (thelist.txt) do copy a:\filestorage\%%a c:\filesneeded\
for /f %i in (C:\Whatever\TheList.txt) do @copy A:\FileStorage\%i C:\FilesNeeded
Thank you....
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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

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

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

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.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.