Bishnu Paudel
Bishnu Paudel

Reputation: 2079

Recursively search for a file name and copy in MS DOS

I have a text file containing names of files separated by newline and a folder with many sub folders which would contain the files matching with names in text file.

I want to pick file names from text file which can be done using for loop; and recursively search for file name in the folder and if the file is found copy it to a different location.

Can anyone please shed a light on it?

Thanks,

Upvotes: 0

Views: 2591

Answers (2)

foxidrive
foxidrive

Reputation: 41244

@echo off
for /f "usebackq delims=" %%a in ("file names.txt") do (
   for /f "delims=" %%b in (' dir "c:\folder\%%a" /b /s /a-d ') do (
      copy "%%b" "c:\new folder"
   )
)

Upvotes: 1

Monacraft
Monacraft

Reputation: 6610

Very easy (though you'll have to a bit more specific so you can tweak the code to suit your situation.

Base Code:

@echo off
for /f "usebackq tokens=*" %%a in ("file names.txt") do (
forfiles /p "C:\users\...[path to main file]" /s /m "%%a" /c "cmd /c copy @path "C:\users\...[target path]""

Not sure if the above double quotes will stuff up, if it does then we can replace with a call and enableextensions.

Tell me if that doesn't work (since it will only work on Win7). Because there are many other ways to do it.

Mona

Upvotes: 0

Related Questions