僅複製特定資料夾的目錄樹和檔案內容

僅複製特定資料夾的目錄樹和檔案內容

我正在嘗試僅從具有特定名稱的子資料夾中歸檔大量文件。我還想保留原始資料夾結構,以便我的團隊可以輕鬆存取存檔檔案。

到目前為止,我已經找到如何使用 robocopy 對特定檔案名稱執行此操作,但我似乎無法使其適用於特定資料夾。

robocopy "c:\folder1[wildcard?]\obsolete" "c:\folder2\" \E

例子:

c:\folder1\client1\obsolete\
c:\folder1\client1\obsolete\ (files & folders I'd like to copy)
c:\folder1\client1\project1\
c:\folder1\client1\file.txt
c:\folder1\client2\project1\
c:\folder1\client2\project1\obsolete\ (files & folders I'd like to copy)
c:\folder1\client2\project2\
c:\folder1\client3\obsolete\
c:\folder1\client3\obsolete\ (files & folders I'd like to copy)

結果:

c:\folder2\client1\obsolete\
c:\folder2\client1\obsolete\ (files & folders)
c:\folder2\client2\project1\obsolete\
c:\folder2\client2\project1\obsolete\ (files & folders)
c:\folder2\client3\obsolete\
c:\folder2\client3\obsolete\ (files & folders)

答案1

這應該可以完成這項工作:

@ECHO OFF
SETLOCAL EnableDelayedExpansion



REM **************************************************

SET source_dir=C:\folder1

SET target_dir=C:\folder2

SET wildcard=obsolete

REM **************************************************



FOR /D /R %%G IN ("%source_dir%") DO (
    SET "folder_name=%%G"
    ECHO !folder_name! | FINDSTR /R "%wildcard%" >nul && CALL :copy
)


ECHO. & ECHO. & ECHO. & ECHO. & ECHO. & ECHO Done^^!
PAUSE
EXIT


:copy    
SET target_folder_name=!folder_name:%source_dir%\=!

SET endbit=!target_folder_name:*%wildcard%=!
SET target_folder_name=!target_folder_name:%endbit%=!

SET endbit=!folder_name:*%wildcard%=!
SET folder_name=!folder_name:%endbit%=!

ROBOCOPY "!folder_name!" "%target_dir%\!target_folder_name!" /E
EXIT /B

區分wildcard大小寫,除非您將/I選項新增至FINDSTR

ECHO !folder_name! | FINDSTR /I /R "%wildcard%" >nul && CALL :copy

相關內容