[英]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
包含subfolder1
、 subfolder2
等。
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:这对我有用:
.bat
/ .cmd
file:在.bat
/ .cmd
文件中:for /r "c:\source_directory\" %%x in (*.pdf) do move "%%x" "c:\target_directory\"
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,而不是命令提示符)中还有另一种方法可以做到这一点:
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.