Page 1 of 1

Batch Search & Copy files

Posted: 21 Aug 2011 21:48
by amandaoc
I need to find the two files (times 780) that start out as EX: "FileName123" that are stored in a subfolder of Directory A then copy the files to Directory B. The list of filenames that I need are stored in a txt file. PLEASE HELP!

Re: Batch Search & Copy files

Posted: 22 Aug 2011 09:39
by dbenham
I'm not sure I fully understand your requirements.

Assuming:
- Your text file does not contain full filenames, so when searching for FileName123 you are actually looking for FileName123*
- The text file has 1 partial filename per line
- Your source directory tree will not produce two files with the same name. Or if it does, then you don't care which one gets copied to DirB because the last one copied will over-write the first one.
- None of your file names start with a <space> or ; (very rare occurrances) There are easy ways to get around this limitation, but I don't want to unnecessarily complicate the solution.

Then something like this should work (untested):

Code: Select all

for /f "tokens=*" %%a in (file.txt) do (
  for /f "tokens=*" %%b in ('dir /s /b "DirA\%%~a*"') do copy /y "%%b" "DirB"
)


Dave Benham