【问题标题】:Move folders in subfolders在子文件夹中移动文件夹
【发布时间】:2017-08-23 17:49:36
【问题描述】:

我不知道标题是否足够清楚。 这就是我想要做的:

实际文件夹结构:

Root_Folder
 |
 +-- Folder1
 |    
 +-- Folder2
 |  |  
 |  +-- file 2.1
 |    
 +-- Folder3
 |  |  
 |  +-- file 3.1
 |  +-- file 3.2
 |    
 +-- Folder 4
 |  |  
 +  |-- Subfolder 4.1

我想要的文件夹结构:

Root_Folder
 |
 +-- Folder1
 |  |
 |  +-- Documents
 |
 +-- Folder2
 |  |  
 |  +-- Documents
 |  |  |
 |  |  +-- file 2.1
 |    
 +-- Folder3
 |  |  
 |  +-- Documents
 |  |  |
 |  |  +-- file 3.1
 |  |  +-- file 3.2
 |    
 +-- Folder 4
 |  |  
 |  +-- Documents
 |  |  |
 |  |  +-- Subfolder 4.1

我想出的脚本:

SET ROOT_FOLDER=C:\Folder\Root
SET WORK_FOLDER=C:\Temp
SET FILE_LIST=%WORK_FOLDER%\list.txt
DIR %ROOT_FOLDER% >%FILE_LIST% /a:d /b
CD %ROOT_FOLDER%

FOR /F %%i IN (%FILE_LIST%) DO ROBOCOPY "%ROOT_FOLDER%\%%i" "%ROOT_FOLDER%\%%i\Documents" /MOVE /MIR /SEC /R:1 /W:1 /COPYALL

不幸的是,它不起作用。 它似乎在做的是:

  • 在每个 FolderX 中,都会创建一个 Documents 子文件夹:good
  • folderX 中的子文件夹已移入其中:
  • 但其中还创建了另一个 Documents 子文件夹:bad
  • folderX 下的文件被移动到这个 **Documents* 子文件夹中:错误

请你们帮帮我好吗?

谢谢

【问题讨论】:

    标签: batch-file cmd robocopy


    【解决方案1】:

    据我了解,您的问题是 您不知道 Robocopy 在这种情况下会做什么。我建议你在一个简单的批处理文件中明确地实现相同的过程,这样你就永远知道你在做什么:

    @echo off
    setlocal EnableDelayedExpansion
    
    set "ROOT_FOLDER=C:\Folder\Root"
    
    rem For each folder in root folder
    cd "%ROOT_FOLDER%"
    for /D %%a in (*) do (
       cd "%%a"
    
       rem Move all existent folders into "Documents" folder
       for /F "delims=" %%b in ('dir /B /A:D') do (
          md Documents 2> NUL
          move "%%b" "Documents\%%b"
       )
    
       rem Move all existent files into "Documents" folder
       md Documents 2> NUL
       move *.* Documents
    
       cd ..
    )
    

    【讨论】:

    • 谢谢,但我想使用 ROBOCOPY 以便能够使用权限和日期移动所有内容。
    【解决方案2】:

    问题是 ROBOCOPY 正在创建 Documents 文件夹并开始复制,但 /MOVE 参数告诉它移动文件和目录,因此它会在第一个文件夹中再次创建 Documents 文件夹。

    尝试将/XD "Documents" 参数添加到您的 ROBOCOPY。

    像这样:

    SET ROOT_FOLDER=C:\Folder\Root
    SET WORK_FOLDER=C:\Temp
    SET FILE_LIST=%WORK_FOLDER%\list.txt
    DIR %ROOT_FOLDER% >%FILE_LIST% /a:d /b
    CD %ROOT_FOLDER%
    
    FOR /F %%i IN (%FILE_LIST%) DO ROBOCOPY "%ROOT_FOLDER%\%%i" "%ROOT_FOLDER%\%%i\Documents" /MOVE /MIR /SEC /R:1 /W:1 /COPYALL /XD "Documents"
    

    【讨论】:

      猜你喜欢
      • 2021-12-02
      • 1970-01-01
      • 2021-08-02
      • 1970-01-01
      • 1970-01-01
      • 2018-10-16
      • 1970-01-01
      • 2021-03-04
      相关资源
      最近更新 更多