equalizer
equalizer

Reputation: 156

Batch file to copy certain file types from certain subdirectories to one folder

Looking to copy all files with extension *.cpp in "development" subfolders to new directory.

For example, C:\source has 10 folders named A1 to A10. Each A folder has subfolder such as development, pending, released. I want to extract all *.cpp from all development subfolders and place in C:\destination.

Example C:\source\A1\development\ folder contains file one.cpp C:\source\A1\released\ folder contains two.cpp

I want only files *.cpp that are in path that contain development name. So in this example only one.cpp should be copied to newfolder

I can name these files from a cmd prompt: dir /s /b *.cpp | findstr /i /m "development" > outfile.txt

(Note that dir command above is searching only filenames, not the contents of the files!)

Guess I could take the outfile.txt and run another batch file

Batch script to copy files listed in text file from a folder and its subfolders to a new location

However, there must be easier way.

BTW:

DOS Batch file to copy certain file types from subdirectories to one folder with rename

does not allow limiting to subfolders.

Upvotes: 0

Views: 1505

Answers (1)

itChi
itChi

Reputation: 662

From here:

But does need to be run from a batch file.

for /f "delims=" %%A in ('findstr /i /M "glossary" *.txt') do copy "%%A" C:\destination

EDIT:

No need for findstr for just file names, so I've used your initial dir search, and combined it with the for statement. Works for me :)

for /f "delims=" %%A in ('dir /s /b *development*.cpp') do copy "%%A" C:\destination

EDIT2:

Right, so this should be it, now the for statement runs with the folders inside the dir as a variable. Note: it does generate false positives that you can filter out with an if exists statement, but I'm sure you can experiment with that yourself:

for /f "tokens=*" %%D in ('dir /b /s /a:d "C:\source\*"') do copy %%D\development\*.cpp C:\destination

EDIT3:

In case you wanted to find several .cpp's in your mystery dir :)

for /f "tokens=*" %%D in ('dir /b /s /a:d "C:\source\*"') do (for /f "delims=" %%A in ('dir /s /b "%%D\development\*.cpp"') do copy "%%A" C:\destination)

Upvotes: 1

Related Questions