简体   繁体   English

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

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

Folder c:\\folder1 contains subfolder1 , subfolder2 , etc..文件夹c:\\folder1包含subfolder1subfolder2等。

These subdirectories hold .pdf and .db files.这些子目录包含.pdf.db文件。

How can all the .pdf files be moved to c:\\folder1 using the Windows command interpreter?如何使用 Windows 命令解释器将所有.pdf文件移动到c:\\folder1

This worked for me:这对我有用:

In a .bat / .cmd file:.bat / .cmd文件中:

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

For direct input into a Command Prompt window (not PowerShell):要直接输入命令提示符窗口(不是 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.此命令将递归地将所有*.pdf文件从源(及其所有子目录)复制到目标目录。

To exclude files in subdirectories omit the /r switch.要排除子目录中的文件,请省略/r开关。

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

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.如果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.然后去除回声并开始移动。

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在右上角的搜索框中,输入 *.pdf 并点击搜索
  • Select all the files and drag them to the top-level folder选择所有文件并将它们拖到顶级文件夹
  • Respond to any prompts about overwriting files响应有关覆盖文件的任何提示

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]

The command after DO is inherently in its own quotes. DO 之后的命令本身就在它自己的引号中。 If you anticipate spaces in your source or destination, use double quotes to encapsulate them, eg:如果您预计源或目标中会有空格,请使用双引号将它们封装起来,例如:

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 上的命令提示:

命令提示

This shows that a wildcard does not work in paths, only for files in a single directory (eg C:\\folder*.files).这表明通配符在路径中不起作用,仅适用于单个目录中的文件(例如 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.使用它来搜索 .MKV 文件的所有子文件夹,并将它们移动到批处理文件所在的当前目录。

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 为我创造了奇迹:

 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.我用它来将所有带有特定掩码的文件移出c:\\cache及其众多子目录。

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.此外,如果您愿意,可以使用这些命令创建 .bat 文件。 To do this, paste your commands into notepad and save it as .bat instead of .txt为此,请将您的命令粘贴到记事本中并将其另存为.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.如果您有任何需要这样做的重复性任务,这将非常有用。

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

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}. {将所有文件从newdrg文件夹及其“子文件夹”移动到目标文件夹Alldrawings ,此命令用于批处理文件操作,命令行在两个地方都使用单个“%”}。

To copy all text files to a folder and preserve directory structure:要将所有文本文件复制到文件夹并保留目录结构:

xcopy *.txt /s D:Folder xcopy *.txt /s D:文件夹

Open CMD at location c:/folder1在位置c:/folder1打开 CMD

Run the following command:运行以下命令:

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

Where ./subfolder1/*.pdf selects all(*) pdf files in ./subfolder1/./subfolder1/*.pdf选择所有(*)的PDF文件./subfolder1/

and

mv command moves all of them to the relative path . mv 命令将它们全部移动到相对路径. , which is the current directory c:/folder1 , 即当前目录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/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用Windows cmd命令将所有文件移动到父文件夹 - How can I move all files to the parent folder using windows cmd command 如何使用CMD / Powershell查找目录(包括子目录)中的所有文件,然后显示具有某些扩展名的文件 - How to, using CMD/Powershell, find all files in a directory (including subdirectories), and then display files that have certain extensions 将一个父目录下的子目录中的所有文件移动到N个子目录中 - Move all files in subdirectories contained in one parent directory to N sub-subdirectories 如何删除带有cmd批处理的具有特定扩展名的所有文件(不包括2个特定单词)? 视窗 - How can delete all files with a specific extension with cmd batch, excluding 2 specific words? Windows 如何使用 windows 命令将所有文件从父文件夹中移动到新的子文件夹中? - How to move all files excecpt one from parent folder into a new child folder using windows command? 使用for循环将所有文件夹从特定目录复制到多个子目录 - Copy all folders from a specific direcory into multiple subdirectories using for loop 使用批处理脚本将所有文件从所有子文件夹移动到父文件夹 - Move All Files from all Subfolders to Parent Folder using batch script cmd或powershell获取所有子目录 - cmd or powershell to get all subdirectories 如何合并所有子目录中具有相同名称的文本文件? - How to merge text files with the same name from all subdirectories? 如何在特定驱动器的所有子目录中创建文件? - How to create a file in all subdirectories on a specific drive?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM