开发者

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

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 comman开发者_如何学Pythond interpreter?


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.


There is another way to do this in Windows Explorer (GUI, not command prompt):

  • Navigate to the top-level directory
  • In the search box in the top-right, type *.pdf and hit search
  • Select all the files and drag them to the top-level folder
  • Respond to any prompts about overwriting files


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:

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

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.


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
)


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.


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.


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\


@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}.


To copy all text files to a folder and preserve directory structure:

xcopy *.txt /s D:Folder


Open CMD at location c:/folder1

Run the following command:

mv ./subfolder1/*.pdf .
mv ./subfolder2/*.pdf .

Where ./subfolder1/*.pdf selects all(*) pdf files in ./subfolder1/

and

mv command moves all of them to the relative path ., which is the current directory c:/folder1

In place of relative paths (like ./subfolder1/, ./subfolder2/ & .) you can also type full or absolute paths like c:/folder1/subfolder1/, c:/folder1/subfolder2/ & c:/folder1/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜