递归地将所有文件向上移动

时间:2018-02-02 08:16:50

标签: batch-file cmd

我想以递归方式提升所有文件。

当前文件夹结构如下所示

D:\_Courses>tree /a
D:.
+---Course 1
|   \---Download
+---Course 2
|   \---Download
+---Course 3
|   \---Download
\---Course 4
    \---Download

我想将所有文件从“下载”文件夹移动到其父文件夹。

这意味着,

“课程1 /下载”中的所有文件都应转到“课程1”

“课程2 /下载”中的所有文件都应转到“课程2”

“课程3 /下载”中的所有文件都应转到“课程3”

“课程4 /下载”中的所有文件都应转到“课程4”

问题1:显示不存在的文件夹

当我使用以下命令时

D:\_Courses>FOR /R /D %I IN (Download\) DO @ECHO %I

输出如下所示

D:\_Courses\Download\
D:\_Courses\Course 1\Download\
D:\_Courses\Course 1\Download\Download\
D:\_Courses\Course 2\Download\
D:\_Courses\Course 2\Download\Download\
D:\_Courses\Course 3\Download\
D:\_Courses\Course 3\Download\Download\
D:\_Courses\Course 4\Download\
D:\_Courses\Course 4\Download\Download\

但是,没有名为

的文件夹
D:\_Courses\Course 1\Download\Download

问题2:转移到错误的目录

当我发出以下命令时

D:\_Courses>FOR /R /D %I IN (Download\) DO move "%I"*.* ..

所有4个下载文件夹中的所有文件都移至D:

输出是 -

D:\_Courses>move "D:\_Courses\Download\"*.* ..
The system cannot find the file specified.

D:\_Courses>move "D:\_Courses\Course 1\Download\"*.* ..
D:\_Courses\Course 1\Download\518057_00_01.mp4
D:\_Courses\Course 1\Download\518057_00_02.mp4
D:\_Courses\Course 1\Download\518057_00_03.mp4
...
       28 file(s) moved.

D:\_Courses>move "D:\_Courses\Course 1\Download\Download\"*.* ..
The system cannot find the file specified.

D:\_Courses>move "D:\_Courses\Course 2\Download\"*.* ..
D:\_Courses\Course 2\Download\486757_00_01.mp4
D:\_Courses\Course 2\Download\486757_00_02.mp4
D:\_Courses\Course 2\Download\486757_00_03.mp4
D:\_Courses\Course 2\Download\486757_00_04.mp4
...
       40 file(s) moved.

D:\_Courses>move "D:\_Courses\Course 2\Download\Download\"*.* ..
The system cannot find the file specified.

D:\_Courses>move "D:\_Courses\Course 3\Download\"*.* ..
D:\_Courses\Course 3\Download\569336_00_01.mp4
D:\_Courses\Course 3\Download\569336_00_02.mp4
D:\_Courses\Course 3\Download\569336_00_03.mp4
D:\_Courses\Course 3\Download\569336_01_01.mp4
D:\_Courses\Course 3\Download\569336_01_02.mp4
...
       19 file(s) moved.

D:\_Courses>move "D:\_Courses\Course 3\Download\Download\"*.* ..
The system cannot find the file specified.

D:\_Courses>move "D:\_Courses\Course 4\Download\"*.* ..
D:\_Courses\Course 4\Download\518056_00_01.mp4
D:\_Courses\Course 4\Download\518056_00_02.mp4
D:\_Courses\Course 4\Download\518056_00_03.mp4
...
       17 file(s) moved.

D:\_Courses>move "D:\_Courses\Course 4\Download\Download\"*.* ..
The system cannot find the file specified.

3 个答案:

答案 0 :(得分:1)

好的,这应该做你想要的。

@echo off
for /r /d %%a in (Download) do (
echo move "%%a\*" "%%a\.."
)

只有在您满意后才能删除回声,当回声结束时结果看起来很好。

小心放置批处理文件的位置,因为它将以递归方式从批处理文件的根目录中搜索名为Download的所有目录。还要确保批处理文件不存在于名为Download

的目录中

编辑:从cmdline而不是批处理运行(确保完全复制代码):

for /r /d %a in (Download) do (echo move "%a\*" "%a\..")

答案 1 :(得分:0)

this answer复制:

您可以在Batch中编写一个递归算法,它可以精确控制您在每个嵌套子目录中执行的操作:

@echo off
call :treeProcess
goto :eof

:treeProcess
rem Do whatever you want here over the files of this subdir, for example:
move *.* ..
for /D %%d in (*) do (
    cd "%%d"
    call :treeProcess
    cd ..
)
exit /b

答案 2 :(得分:0)

此PowerShell脚本使用目录和文件对象Parent属性来复制文件。如果您确信将正确的文件复制到正确的目标位置,请从Move-Item cmdlet中删除WhatIf

Get-ChildItem -Directory -Path C:/src/t/_Courses -Recurse -Filter 'Download' |
    ForEach-Object {
        $newdir = $_.Parent.FullName
        Get-ChildItem -File -Path $_.FullName |
            ForEach-Object { Move-Item -LiteralPath $_.FullName $newdir -WhatIf }
    }

要从cmd shell中使用它,请将上面的代码放入诸如movedowns.ps1之类的文件中,然后运行它。

powershell -NoProfile -File movedowns.ps1