在我的 Windows 机器上,S
驱动器的文件夹下OF
有以下结构:
S://OF
├── FolderA
│ └── FolderA1
│ └── FolderA2
│ └── FolderX
| └── myFolder
│
│
├── FolderB
│ └── FolderB1
│ └── FolderB2
| └── FolderX
| └── myFolder
│
│
├── FolderC
│ └── FolderC1
│ └── FolderC2
└── FolderX
└── myFolder
我想将所有目录复制到驱动器上的myFolder
新目录 ( AH
)中R
R://AH
├── myFolder1
├── myFolder2
├── myFolder3
我尝试了以下操作,但没有成功!
For /D %G In ("OF\*") Do @%__AppDir__%Robocopy.exe "%G\myFolder" ".Destination\%~nxG_myFolder">NUL
答案1
这应该可以为您完成此操作,它使用遍历文件夹树dir
,当找到匹配项时创建文件夹。
如果您还想复制或移动内容,请根据您要执行的操作将其替换md
为xcopy
或copy
或。robocopy
@echo off
setlocal enabledelayedexpansion
set "_pattern=MyFolder"
rem change this line to the fully qualified root of the tree where where your targets are:
set "_path=S drive path"
rem change this line to the fully qualified new location they will be copied to:
set "_newroot=R drive path"
rem loop through the tree where your the folders are:
for /f "delims=" %%i in ('dir "%_path%" /b /ad /s') DO (
rem this is what you will compare to:
@set "_foundpath=%%i"
rem extract the last 'n' characters of the path found:
call set _compareto=!_foundpath:~-8!
rem if the folder pattern matches, perform the action:
IF !_compareto!@ equ %_pattern%@ (
set /a _cnt+=1
rem remove the echo in this line to make the directory:
echo md %_newroot%\%_pattern%!_cnt!
)
)
endlocal
exit /b