Search code examples
batch-filexcopy

Copying files with a specific extension from multiple directories


If I have several directories that contain, lets say, word documents and I want to copy every single file in the parent directory that have the ".doc" extension, how could I do that in a batch file?

I know that if I write:

xcopy "C:\Users\Documents\*.doc" C:\Users\NewDestination /s /i 

I'll copy all .doc files in the Documents directory to a new destination, but what if the only .doc files in Documents are located in subdirectories? How could I do that without having to write an xcopy line for each subdirectory?


Solution

  • for /r "C:\Users\Documents\" %# in (*doc) do @copy %# "C:\Users\NewDestination" /Y
    

    or when used from batch file:

    for /r "C:\Users\Documents\" %%# in (*doc) do copy %%# "C:\Users\NewDestination" /Y