这是一个Windows批处理脚本。
第一个脚本将仅在目录结构中移动文件,类似于OP问题中显示的文件。
编辑脚本变量“topfolder”的值以包含目录结构的“基本文件夹”。
编辑脚本变量“subfolderprefix”的值,以包含要在“topfolder”中搜索的“第一级”文件夹的公共前缀。在OP问题中,这个“前缀”是“文件夹”,但可以根据需要将其更改为任何字符串。例如,使用“folder”作为前缀,脚本将搜索如下文件夹:
top_folder
folder1
folder2
folder3
folder99
folderABC
但是,“top_folder”中的其他文件夹将被跳过,如下所示:
top_folder
somefolder
someotherfolder
将搜索文件的“第二级”子文件夹具有父foldername的公共前缀,后跟“_”( folder1\folder1_xxx
)。同样,将跳过任何其他文件夹:
top_folder
folder1
folder1_2
(Files here will be moved to "folder1")
folder1_99
(Files here will be moved to "folder1")
folder1_ABC
(Files here will be moved to "folder1")
folder2
folder2_2
(Files here will be moved to "folder2")
somesubfolder
(Files here will be skipped)
folder3
folder3_1
(Files here will be moved to "folder3")
folder3_2
(Files here will be moved to "folder3")
folder99
folder99_XYZ
(Files here will be moved to "folder99")
folderABC
folderABC_123
(Files here will be moved to "folderABC")
somefolder
(Folders here will be skipped)
someotherfolder
(Folders here will be skipped)
这是批处理脚本:
@echo off
set "topfolder=C:\Temp\SU373589\top_folder"
set "subfolderprefix=folder"
set "startdir=%CD%"
cd /d "%topfolder%"
set ctoptest=0
set cfilecount=0
for /d %%f in ("%subfolderprefix%*") do call :worklevel1 "%%~f"
if %ctoptest% EQU 0 echo There are no matching subfolders in "%topfolder%".
if %cfilecount% EQU 0 echo There were no files moved.
if %cfilecount% EQU 1 echo %cfilecount% file was moved.
if %cfilecount% GEQ 2 echo %cfilecount% files were moved.
cd /d "%startdir%"
goto :EOF
:worklevel1
set "subfolder=%~1"
set /a ctoptest+=1
echo Processing subfolder "%subfolder%" ...
set "subsubfolderprefix=%subfolder%\%subfolder%_"
set cwork1test=0
for /d %%g in ("%subsubfolderprefix%*") do call :worklevel2 "%%~g"
if %cwork1test% EQU 0 echo There are no matching subfolders in "%subfolder%"
echo.
set "subfolder="
set "subsubfolderprefix="
set cwork1test=
goto :EOF
:worklevel2
set "subsubfolder=%~1"
set /a cwork1test+=1
echo Processing subsubfolder "%subsubfolder%" ...
set cwork2test=0
for %%h in ("%subsubfolder%\*") do call :workmove "%%~h"
if %cwork2test% EQU 0 echo There are no files to move in "%subsubfolder%"
set "subsubfolder="
set cwork2test=
goto :EOF
:workmove
set "targetfile=%~1"
set /a cwork2test+=1
set /a cfilecount+=1
echo Moving file "%targetfile%" to "%subfolder%" (%cwork2test%)...
move "%targetfile%" "%subfolder%" >nul 2>&1
set "targetfile="
goto :EOF
第二个批处理脚本将以类似的方式工作,查看所有“第一级”子文件夹,并将所有文件从所有“第二级”子文件夹移动到“第一级”子文件夹。它不会与任何foldernames的任何“前缀”匹配。
编辑脚本变量“topfolder”的值以包含目录结构的基本文件夹。
@echo off
set "topfolder=C:\Temp\SE373589\top_folder"
set "startdir=%CD%"
cd /d "%topfolder%"
set ctoptest=0
set cfilecount=0
for /d %%f in ("*") do call :worklevel1 "%%~f"
if %ctoptest% EQU 0 echo There are no matching subfolders in "%topfolder%".
if %cfilecount% EQU 0 echo There were no files moved.
if %cfilecount% EQU 1 echo %cfilecount% file was moved.
if %cfilecount% GEQ 2 echo %cfilecount% files were moved.
cd /d "%startdir%"
goto :EOF
:worklevel1
set "subfolder=%~1"
set /a ctoptest+=1
echo Processing subfolder "%subfolder%" ...
set cwork1test=0
for /d %%g in ("%subfolder%\*") do call :worklevel2 "%%~g"
if %cwork1test% EQU 0 echo There are no matching subfolders in "%subfolder%"
echo.
set "subfolder="
set cwork1test=
goto :EOF
:worklevel2
set "subsubfolder=%~1"
set /a cwork1test+=1
echo Processing subsubfolder "%subsubfolder%" ...
set cwork2test=0
for %%h in ("%subsubfolder%\*") do call :workmove "%%~h"
if %cwork2test% EQU 0 echo There are no files to move in "%subsubfolder%"
set "subsubfolder="
set cwork2test=
goto :EOF
:workmove
set "targetfile=%~1"
set /a cwork2test+=1
set /a cfilecount+=1
echo Moving file "%targetfile%" to "%subfolder%" (%cwork2test%)...
move "%targetfile%" "%subfolder%" >nul 2>&1
set "targetfile="
goto :EOF
一个更简单的批处理脚本版本,没有检查空文件夹等...:
@echo off
set "topfolder=C:\Temp\SE373589\top_folder"
set "startdir=%CD%"
cd /d "%topfolder%"
for /d %%f in (*) do (
echo Processing folder: "%%~f"
for /d %%g in ("%%f\*") do (
echo Processing sub-folder: "%%~g"
for %%h in ("%%~g\*") do (
echo Moving file: "%%~h" to "%%~f"
move "%%~h" "%%~f" >nul 2>&1
)
)
echo.
)
cd /d "%startdir%"
最后,一个没有消息传递的“最低限度”脚本,假设您已经在正确的“基本文件夹”中:
@echo off
for /d %%f in (*) do (
for /d %%g in ("%%f\*") do (
for %%h in ("%%~g\*") do move "%%~h" "%%~f" >nul 2>&1
)
)
而且,以防万一你期待它,你可以从命令提示符运行“一个班轮”:
@for /d %f in (*) do @(for /d %g in ("%f\*") do @(for %h in ("%~g\*") do @move "%~h" "%~f" >nul 2>&1))