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

Posted on 2009-05-14
Medium Priority
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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 1000 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....

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

You may have already been in the need to update a whole folder stucture using a script. Robocopy does it well and even provides a list of non-updated files in a log (if asked to). Generally those files that were locked by a user or a process by the …
Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
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…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses

800 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