Batch Search & Copy files

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
amandaoc
Posts: 1
Joined: 21 Aug 2011 21:41

Batch Search & Copy files

#1 Post by amandaoc » 21 Aug 2011 21:48

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!

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Batch Search & Copy files

#2 Post by dbenham » 22 Aug 2011 09:39

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

Post Reply