仅复制特定文件夹的目录树和文件内容

仅复制特定文件夹的目录树和文件内容

我正在尝试仅从具有特定名称的子文件夹中归档大量文件。我还想保留原始文件夹结构,以便我的团队可以轻松访问已归档的文件。

到目前为止,我已经找到了如何使用 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

相关内容