我希望能夠將資料夾(以及任何子資料夾和檔案)從一個位置移動到另一個位置;但是,我只想從起始位置的每個資料夾中移動一個特定的子資料夾。
這是我現有的資料夾結構的範例:
E:\Estimates\Estimating Files\E27001
E:\Estimates\Estimating Files\E27001\27001A - Customer1\Drawings
E:\Estimates\Estimating Files\E27001\27001A - Customer1\Costings
E:\Estimates\Estimating Files\E27001\27001B - Customer2\Drawings
E:\Estimates\Estimating Files\E27001\27001B - Customer2\Costings
E:\Estimates\Estimating Files\E27001\27001C - Customer3\Drawings
E:\Estimates\Estimating Files\E27001\27001C - Customer3\Costings
E:\Estimates\Estimating Files\E27002
E:\Estimates\Estimating Files\E27002\27002A - Customer1\Drawings
E:\Estimates\Estimating Files\E27002\27002A - Customer1\Costings
我有很多估計,因為您可以看到一些具有相同的數字但後綴字母不同,並且客戶名稱明顯發生了變化。
我只想將 Drawings 資料夾移至新的磁碟機和資料夾結構,如下所示:
S:\E27xxx\0xx\01\A - Customer1\
S:\E27xxx\0xx\01\B - Customer2\
S:\E27xxx\0xx\01\C - Customer3\
S:\E27xxx\0xx\02\A - Customer1\
繪圖資料夾將包含我想要移動到新位置的子資料夾和檔案。
到目前為止,我只成功創建了以下級別的空白資料夾:
S:\E27xxx\0xx\01\A - Customer1\
S:\E27xxx\0xx\01\B - Customer2\
S:\E27xxx\0xx\01\C - Customer3\
S:\E27xxx\0xx\02\A - Customer1\
使用小批次文件,估計 27000 到 30000 之間的所有資料。
但是移動文件和資料夾超出了我的能力,希望有人能夠幫助我,或者知道可能有用的現有實用程式!
謝謝!
答案1
我只想將 Drawings 資料夾移至新的磁碟機和資料夾結構,如下所示:
這個批次檔可以幫助你。它找到所有“Drawings”子資料夾,將目錄路徑的部分標記化以建構新的目錄結構,然後使用以下命令複製每個找到的目錄及其子目錄機器人複製工具。
很重要:
確保將批次文件放入“...\估計文件" 資料夾並從那裡運行它,否則,手動設定
sourceDir
變數。在使用此腳本之前,您應該手動調整循環的令牌計數
For
。例如,我使用這個來源資料夾
C:\Source\Estimates\Estimating Files
,我們需要錯過前四個標記,因為我們想在該Estimating Files
部分之後開始計數,這就是我Tokens=4,5,6
在下面的腳本中設定的原因,如果您有不同的路徑,那麼您應該調整標記。如果您的來源目錄是,
E:\Estimates\Estimating Files
那麼您應該像這樣設定 tokens 參數:Tokens=3,4,5
。如果想要自動刪除複製的來源結構,只需
/Move
在 RoboCopy 參數中新增 a 即可。
原始碼
@Echo OFF
Set "sourceDir=%CD%"
Set "targetDir=C:\Target"
Set "findPattern=Drawings"
For /F "Tokens=4,5,6 Delims=\" %%a In (
'Dir /B /S /A:D "%sourceDir%\*%findPattern%"'
) Do (
Call Set "Token1=%%~a"
Call Set "Token2=%%~b"
Call Set "Token3=%%~c"
Call Set "sourcePath=%CD%\%%~a\%%~b\%%~c"
Call Set "targetPath=%targetDir%\%%Token1%%\%%Token1:~3%%\%%Token2:~3,2%%\%%Token2:~5%%\%%Token3%%"
Echo+
Call Echo Source: "%%sourcePath%%"
Call Echo Target: "%%targetPath%%"
(Call RoboCopy.exe "%%sourcePath%%" "%%targetPath%%" /E /ZB /COPYALL)1>Nul
)
Pause&Exit /B 0
輸出
來源:“C:\Source\Estimates\Estimating Files\E27001\27001A - Customer1\Drawings”
目標:“C:\Target\E27001\001\01\A - Customer1\Drawings”
來源:“C:\Source\Estimates\Estimating Files\E27001\27001B - Customer2\Drawings”
目標:“C:\Target\E27001\001\01\B - Customer2\Drawings”
來源:“C:\Source\Estimates\Estimating Files\E27001\27001C - Customer3\Drawings”
目標:“C:\Target\E27001\001\01\C - Customer3\Drawings”
來源:“C:\Source\Estimates\Estimating Files\E27002\27002A - Customer1\Drawings”
目標:“C:\Target\E27002\002\02\A - Customer1\Drawings”
答案2
試試這個腳本。不過,您可能需要編輯磁碟機號
setlocal EnableDelayedExpansion
@echo off
Q:
cd "Estimating\Estimating Files"
FOR /D /R %%G IN ("*Drawings*") DO (
FOR /F "tokens=4,5 delims=\" %%H IN ("%%G") DO (
set temp=%%H
set num=!temp:~4,2!
set temp=%%I
set alpha=!temp:~5!
MKDIR "Q:\E27XXX\0XX\!num!\!alpha!\Drawings"
CALL :mover "%%G" !num! !alpha!
)
)
:mover
FOR /R %1 %%X IN (*) DO (
COPY "%%X" "Q:\E27XXX\0XX\%2\%3 %4 %5\Drawings"
)
答案3
這是我用來將資料夾複製到新目錄的最終程式碼。感謝@ElektroStudios 的幫助。
@Echo OFF
Set "sourceDir=%CD%"
Set "targetDir=S:\E30xxx"
Set "findPattern=2 - Drawings"
For /F "Tokens=6,7,8,9 Delims=\" %%a In (
'Dir /B /S /A:D "%sourceDir%\*%findPattern%"'
) Do (
Call Set "Token1=%%~a"
Call Set "Token2=%%~b"
Call Set "Token3=%%~c"
Call Set "Token4=%%~d"
Call Set "sourcePath=%CD%\%%~a\%%~b\%%~c\%%~d"
Call Set "targetPath=%targetDir%\%%Token1:~3,1%%xx\%%Token2:~4,2%%\%%Token3:~6%%"
Echo+
Call Echo Source: "%%sourcePath%%"
Call Echo Target: "%%targetPath%%"
(Call RoboCopy.exe "%%sourcePath%%" "%%targetPath%%" /E /ZB /COPYALL)1>Nul
)
Pause&Exit /B 0
這只是掌握 RoboCoby 和分隔字串標記化的一個例子。