How to move all files with specific extension from all subdirectories to their parent using CMD?

mihai picture mihai · Dec 5, 2010 · Viewed 71.1k times · Source

Folder c:\folder1 contains subfolder1, subfolder2, etc..

These subdirectories hold .pdf and .db files.

How can all the .pdf files be moved to c:\folder1 using the Windows command interpreter?

Answer

pablo M picture pablo M · Nov 6, 2012

This worked for me:

In a .bat / .cmd file:

for /r "c:\source_directory\" %%x in (*.pdf) do move "%%x" "c:\target_directory\"

For direct input into a Command Prompt window (not PowerShell):

for /r "c:\source_directory\" %x in (*.pdf) do move "%x" "c:\target_directory\"

This command will copy recursively all *.pdf files from the source (and all of its subdirectories) to the target directory.

To exclude files in subdirectories omit the /r switch.

Hope it helps.