windows 如何使用 CMD 将所有具有特定扩展名的文件从所有子目录移动到其父目录?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4357233/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 07:50:25  来源:igfitidea点击:

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

windowsfile-iocmddirectorysubdirectory

提问by mihai

Folder c:\folder1contains subfolder1, subfolder2, etc..

文件夹c:\folder1包含subfolder1subfolder2等等。

These subdirectories hold .pdfand .dbfiles.

这些子目录保存.pdf.db文件。

How can all the .pdffiles be moved to c:\folder1using the Windows command interpreter?

如何使用 Windows 命令解释器.pdf将所有文件移动到c:\folder1

回答by pablo M

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.

此命令将使用 Windows 7 中的 cmd 递归地将所有 pdf 文件从源目录复制到目标目录 - 已测试并有效。

Hope it helps.

希望能帮助到你。

回答by Craig Nichols

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 循环列出工作目录中的子目录,内部 for 循环列出要移动到目标路径的子目录:

for /d %f in (*.*) do for /d %e in (%f\*.*) do move "%e" DestinationPath

This works best if DestinationPathis not a subfolder of the working directory, as it will try to move DestinationPathinto itself.

如果DestinationPath它不是工作目录的子文件夹,则效果最佳,因为它会尝试移动DestinationPath到自身中。

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.

并复制/粘贴其中一个结果以运行它并确认它按您预期的方式工作。然后去除回声并开始移动。

回答by Eaglebird

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;

我认为没有通配符适用于子文件夹,因此您想使用循环遍历每个子文件夹并移动 *.pdf;

FOR /R [your root folder path] %%G IN (*.pdf) DO move %%G [new path]

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.:

DO 之后的命令本身就在它自己的引号中。如果您预计源或目标中会有空格,请使用双引号将它们封装起来,例如:

FOR /R "source folder with spaces" %%G IN (*.pdf) DO move "%%G" "dest path with spaces"

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.

注意 %%G 周围的引号,这些是移动命令解析路径所必需的。

**EDIT: In response to the accepted answer, From command prompts on Windows XP and Windows 7, respectively:

**编辑:响应接受的答案,分别来自 Windows XP 和 Windows 7 上的命令提示:

command prompts

命令提示

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.

这表明通配符在路径中不起作用,仅适用于单个目录中的文件(例如 C:\folder*.files)。命令提示符在遇到通配符时不会递归运行。

回答by Will Sheppard

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

在 Windows 资源管理器(GUI,而不是命令提示符)中还有另一种方法可以做到这一点:

  • 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
  • 导航到顶级目录
  • 在右上角的搜索框中,输入 *.pdf 并点击搜索
  • 选择所有文件并将它们拖到顶级文件夹
  • 响应有关覆盖文件的任何提示

回答by Alfredo

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.

使用它来搜索 .MKV 文件的所有子文件夹,并将它们移动到批处理文件所在的当前目录。

FOR /D /r %%F in ("*") DO (
    pushd %CD%
    cd %%F
        FOR %%X in (*.mkv) DO (
            move "%%X" "%CD%"
        )
    popd
)

回答by subbu

@echo on

for /r "F:\All_drawings\newdrg\" %%x in (*.tiff) do move "%%x" "F:\Alldrawings"

pause

{moves all files from the newdrgfolder 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}.

{将文件newdrg夹及其“子文件夹”中的所有文件移动到目标文件夹Alldrawings,此命令用于批处理文件操作,命令行在两个地方都使用单个“%”}。

回答by Daniel Mo?mondor

Robocopy did wonders for me:

Robocopy 为我创造了奇迹:

 robocopy c:\cache c:\cache-2012 ?????-2012*.hash /S /MOV

I used it to move all files with certain mask out of c:\cacheand its numerous subdirectories.

我用它把所有带有特定掩码的文件移出了c:\cache它的众多子目录。

回答by ido

Just taking a wild stab in the dark here, but if I remember correctly DOS can handle globs and the equivalent of mvis MOVE, so:

只是在这里在黑暗中疯狂刺伤,但如果我没记错的话,DOS 可以处理 globs 和mvis的等价物MOVE,所以:

MOVE C:\FOLDER1\*\*.PDF C:\FOLDER1\

MOVE C:\FOLDER1\*\*.PDF C:\FOLDER1\

回答by THE DOCTOR

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 .batinstead of .txt

此外,如果您愿意,可以使用这些命令创建 .bat 文件。为此,请将您的命令粘贴到记事本中并将其另存为.bat而不是.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.

然后,您可以双击该文件,每次执行该文件时它都会执行该文件中的命令。如果您有任何需要这样做的重复性任务,这将非常有用。

回答by Anmol Agarwal

Open CMD at location c:/folder1

在位置打开CMD c:/folder1

Run the following command:

运行以下命令:

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

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

其中./subfolder1/*.pdf选择所有(*)pdf文件./subfolder1/

and

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

mv 命令将它们全部移动到相对路径.,即当前目录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/

代替相对路径(如./subfolder1/, ./subfolder2/& .),您还可以键入完整或绝对路径,如c:/folder1/subfolder1/, c:/folder1/subfolder2/&c:/folder1/