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?
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?
This worked for me:
for /r "c:\source_directory\" %%x in (*.pdf) do move "%%x" "c:\target_directory\"
This command will copy recursively al pdf files from source to target directory using cmd in windows 7 - tested and works.
Hope it helps.
The outer for loop lists the sub-directories in the working directory, the inner for loop lists the sub-directories to move to the destination path:
for /d %f in (*.*) do for /d %e in (%f\*.*) do move "%e" DestinationPath
This works best if DestinationPath
is not a subfolder of the working directory, as it will try to move DestinationPath
into itself.
To confirm the command before running it wholesale, start out just echoing the final move commands like so:
for /d %f in (*.*) do for /d %e in (%f\*.*) do echo move "%e" DestinationPath
and copy/paste one of the results to run it and confirm it worked the way you expected. Then remove the echo and get moving.
I don't think there's a wildcard that will work on subfolders, so you want to use a loop to go through each subfolder and move *.pdf;
FOR /R [your root folder path] %%G IN (*.pdf) DO move %%G [new path]
The command after DO is inherently in its own quotes. If you anticipate spaces in your source or destination, use double quotes to encapsulate them, e.g.:
FOR /R "source folder with spaces" %%G IN (*.pdf) DO move "%%G" "dest path with spaces"
NOTE the quotes around %%G, these are required for the move command to resolve the path.
**EDIT: In response to the accepted answer, From command prompts on Windows XP and Windows 7, respectively:
This shows that a wildcard does not work in paths, only for files in a single directory (e.g. C:\folder*.files). The command prompt does not operate recursively when it encounters a wildcard.
There is another way to do this in Windows Explorer (GUI, not command prompt):
I know this is superlate, but just in case it helps anyone.
Used this to search all sub-folders for a .MKV file and move them to the current directory the batch file resides in.
FOR /D /r %%F in ("*") DO (
pushd %CD%
cd %%F
FOR %%X in (*.mkv) DO (
move "%%X" "%CD%"
)
popd
)
Robocopy did wonders for me:
robocopy c:\cache c:\cache-2012 ?????-2012*.hash /S /MOV
I used it to move all files with certain mask out of c:\cache
and its numerous subdirectories.
@echo on
for /r "F:\All_drawings\newdrg\" %%x in (*.tiff) do move "%%x" "F:\Alldrawings"
pause
{moves all files from the newdrg
folder and its "sub-folders" to the target folder Alldrawings
, this command is for batch file operation for command line use single "%" in both the places}.
Just taking a wild stab in the dark here, but if I remember correctly DOS can handle globs and the equivalent of mv
is MOVE
, so:
MOVE C:\FOLDER1\*\*.PDF C:\FOLDER1\
MOVE "C:\FOLDER 1\PDF FILES\*.pdf" "C:\FOLDER 1"
MOVE "C:\FOLDER 1\DB FILES\*.db" "C:\FOLDER 1"
After the move command, you have the source folder followed by the destination where the files will be moved to. The * in front of each file extension is a wildcard function that will select all of the specified filetype existing within that directory.
Also, if you can create a .bat file with these commands if you want to. To do this, paste your commands into notepad and save it as .bat instead of .txt
Then, you can double-click the file and it will execute the commands within the file each time you do. This is useful if you have any repetitive tasks that require this.