我正在重新組織現有數據,我需要建立一個新的子目錄來接收所有現有文件和目錄。這樣做的目的是為新產品線騰出空間,同時改進現有資料的命名約定。
例如,目前的數據如下所示:
\root directory\
+-proj1357
+-closing binder
+-refi
+-compliance
+-proj2468
+-disbursements
+-compliance
+-proj3579
+-pre-close
+-compliance
實際上應該看起來像這樣:
\root directory\
+-proj1357
+-dpl
+-closing binder
+-refi
+-compliance
+-proj2468
+-dpl
+-disbursements
+-compliance
+-proj3579
+-dpl
+-pre-close
+-compliance
我遇到的問題是,我似乎無法在不陷入遞歸循環的情況下獲得正確的命令。在將其組合在一起時,我經常遇到錯誤“無法執行循環複製”。
好消息是我已經有了一個可行的解決方案,但它需要三個單獨的命令以及每個專案目錄的自訂語法。
command 1> ROBOCOPY G:\proj1357 DPL /E /MOVE
command 2> MOVE G:\proj1357\DPL\DPL\*.* G:\proj1357\DPL
command 3> RMDIR G:\proj1357\DPL\DPL\
據我所知,命令 1 將所有檔案和目錄移至新的子目錄中。透過將專案級目錄中的檔案移至更深的子目錄中,也會導致遞歸問題。因此,我依靠命令 2 來恢復曾經位於專案級目錄中的檔案。然後指令 3 刪除更深的子目錄。
有沒有更好的方法可以透過基於 Windows 的系統運行命令或批次檔? 理想情況下,它會爬行所有專案級目錄,並將內容移至新的子目錄。我正在處理一千多個項目,所以我認為花時間弄清楚如何正確循環它是值得的。
更新
在嘗試使用上面概述的三步驟命令列方法使其工作後,我屈服並調整了 jiggunjer 的答案以適應我的環境。下面是我使用的 MS 批次檔。我還添加了註釋來闡明每行的目的。
@echo off
rem run this batch file in top level directory
rem top level directory should contain all project number directories
rem assumes that all directories starting with a digit are project number directories
rem initialize the listing of all directories
set rootlist=dir /b /a:D
rem initialize numerical filtering
set filter=findstr "^[1-9]"
rem loop through all directories that comply with filtering (start with a digit)
rem use command extension /F to process (in this case) command output
rem identifies all project number parent-directories
rem assign parent-directories to variable %%P, as in 'Project'
FOR /F %%P in ('%rootlist% ^| %filter%') DO (
rem pass the variable %%P to the moveproject loop
call :moveproject "%%P"
rem move files that were ignored by the robocopy command
rem these are 'loose' files that were in the project number directory, but not saved in any subdirectory
rem assumes the robocopy command successfully creates the DPL directory
MOVE %%P\*.* %%P\DPL\
)
rem pause to review command log
pause
:moveproject
rem ensure that the parameter has value
rem converts variable %%P to parameter no. 1
if not [%1] == [] (
rem loop through all sub-directories under each project number
rem use command extension /D to process directories only
rem removing any surrounding quotes (") from parameter no. 1 by using optional syntax -- percent-tilde
rem assign sub-directories to variable %%O, as in 'Origin'
FOR /D %%O IN ("%~1\*") DO (
rem display values
echo project number %%P
echo directory origin %%O
rem delimit origin directory using backslash character
rem use command extension /F to process (in this case) strings
rem assign the second delimited piece to variable %%D, as in 'Destination'
rem effectively drops all text to the left of the first backslash character
FOR /F "tokens=2 delims=\" %%D IN ("%%O") DO (
rem display values
echo %%D
echo directory destination %%P\DPL\%%D
rem move all directories to DPL sub-directory
rem simultaniously creates the receiving DPL directory
robocopy /E /MOVE "%%O" "%%P\DPL\%%D"
)
)
)
答案1
在這 8 個根目錄中的每一個中執行以下命令:
FOR /D %p IN ("*") DO robocopy /E /MOVE "%p" "%p/DPL"
Powershell 3.0+版本:
gci -dir -name | foreach-object{robocopy /E /MOVE $_ ($_ + '\DPL')}
或者
根據您的螢幕截圖,一體化解決方案將類似於:
@echo off
REM Assume: Running in top level dir.
REM Assume: Only project parent fodlers start with a digit.
set rootlist=dir /b /a:D
set filter=findstr "^[1-9]"
FOR /f %%d in ('%rootlist% ^| %filter%') DO (
call :moveproject "%%d"
)
:moveproject
if not [%1] == [] (
FOR /D %%p IN ("%~1\*") DO robocopy /E /MOVE "%%p" "%%p/DPL"
)
快樂的重新養育!
答案2
你可以這樣做:
@echo off
mkdir %2
cd %1
SETLOCAL EnableDelayedExpansion
for /f "delims=" %%D in ('dir /a:d /b') do (
set project_path=%%~fD
move "!project_path!" "%~2%\temp"
mkdir "!project_path!"
move "%~2%\temp" "!project_path!\dpl"
)
採用 2 個參數。第一個是包含專案的路徑,第二個是臨時路徑。
它所做的只是將專案移至臨時路徑,然後將其移回舊專案名稱/dpl。
注意:它假設第一個路徑僅包含專案目錄,並且將忽略該層級的非目錄。