bat 复制目录结构(仅复制目录结构、复制目录及文件)

复制目录结构

使用xcopy复制目录结构

示例:复制当前目录结构及子目录到上层目录的t1文件夹中

xcopy %cd% ..\t1 /C /E /K /T /Y

另外在给几个参考:

例如A目录下里有A1,A2,A3文件夹再分别有其余子目录,复制到B目录下的时候也需要存在A1,A2,A3文件夹及其余子目录,保留目录结构,目录里的文件在对应的目录下。
A:\A1\a2\new.txt
复制后:
B:\A1\a2\new.txt

1)仅复制以A为开头的目录结构

xcopy c:\xxx\a\A? d:\yyy\b /C /E /K /T /Y

2)复制以A为开头的目录结构及文件:
xcopy c:\xxx\a\A? d:\yyy\b /C /E /H /K /R /Y

 

参考:https://bbs.csdn.net/topics/392055120

 

=======================================================================================

使用mkdir创建目录文件夹树结构

参考网上的 批处理, 递归遍历目录路径:https://bbs.csdn.net/topics/370122833

上面的文章中和评论里的遍历方式,我希望仅仅复制目录结构,不复制任何文件。保存以下内容到cef.bat文件

@echo off

:createDir
setlocal enabledelayedexpansion
if [%2]==[] (echo [%time%] The destination folder is null. & goto :eof)

for /d %%a in (%1*) do (
set p=%%a\
set p=!p:.\"=..\"!

echo exec md "%2\!p!"
md "%2\!p!" 
call :createDir "!p!" %2
)
endlocal

执行方式,如:cef "" c:\tmp

 

优化1

根据上面的代码,再次优化,可以指定复制层数

@echo off
cls
echo [%time%] copy empty folder
rem pLevel可设置复制几层文件夹
set pLevel=2

:createDir
setlocal enabledelayedexpansion
if [%2]==[] (echo [%time%] The destination folder is null. & goto :eof)
set fp=%1
set fp=%fp: =% 
set fp=%fp:"=% 
set fp=%fp:\= %
set pCount=0
for %%i in (%fp%) do set /a pCount+=1
if %pCount% GEQ %pLevel% goto :eof

for /d %%a in (%1*) do (
set p=%%a\
set p=!p:.\"=..\"!

echo exec md "%2\!p!"
md "%2\!p!" 
call :createDir "!p!" %2
)
endlocal
View Code

 调用方式,如:cef "" c:\tmp3   或者  cef "c:\tmp1\" c:\tmp3

 

优化2

不使用递归调用,只使用一层循环

@echo off
cls

if [%2]==[] (echo [%time%] The destination folder is null. & goto :eof)
set dest=%2
set dest="%dest%" & set dest=%dest:"=%
if "%dest:~-1%" neq "\" (set dest=%dest%\)
set sour=%1
set sour="%sour%" & set sour=%sour:"=%
if "%sour%"=="" set sour=%cd%
if "%sour:~-1%" neq "\" (set sour=%sour%\)

setlocal enabledelayedexpansion
for /f "tokens=*" %%i in ('dir /s /b /ad "%sour%"') do (
::for /r %sour% %%i in (.) do (
  set p=%%i
  set p=!p:%sour%=!&set p=!p:\.=\!
  echo exec md "%dest%!p!"
  md "%dest%!p!"
  echo.
)
endlocal
View Code

调用方式,如:cef "" c:\tmp3   或者  cef "c:\tmp1\" c:\tmp3

 

优化3

优化变量命名和一些bug修复

@echo off
cls
rem copyLevel可设置复制几层文件夹
set copyLevel=1
set sPath=%~1
if "%sPath%" equ "" set sPath=%cd%
if "%sPath:~-1%" neq "\" set sPath=%sPath%\
echo [%time%] copy %copyLevel% Levels of empty folder from %sPath% &echo;
set tPath=%~2
if [%tPath%]==[] (echo [%time%] The target folder is null. & goto :eof)
if "%tPath:~-1%" neq "\" set tPath=%tPath%\
rem 通过sFolder计算源文件夹的总共有几层
set sFolder=%sPath: =%
set sFolder=%sFolder:\= %
set sLevel=0
for %%i in (%sFolder%) do set /a sLevel+=1
::echo sPath=%sPath%

:createDir <sourceFolder> <targetFolder>
setlocal enabledelayedexpansion
set currSourcePath=%~1
if "%currSourcePath%" equ "" set currSourcePath=%sPath%
if "%currSourcePath:~-1%" neq "\" set currSourcePath=%currSourcePath%\
set sourceFolderList=%currSourcePath: =%
set sourceFolderList=%sourceFolderList:\= %

set currLevel=0
for %%i in (%sourceFolderList%) do set /a currLevel+=1
set /a currLevel=%currLevel%-%sLevel%
if %currLevel% GEQ %copyLevel% goto :eof

for /d %%a in ("%currSourcePath%*") do (
set currPath=%%~a
set currPath=!currPath:.\"=..\"!
set currFolderName=!currPath!
set currFolderName=!currFolderName:.\"=..\"!
set currFolderName=!currFolderName:%sPath%=!
echo currFolderName=!currFolderName!
echo md "%tPath%!currFolderName!"
md "%tPath%!currFolderName!"
echo call :createDir "!currPath!" %tPath%
call :createDir "!currPath!" %tPath%
)
endlocal
View Code

 执行方式,如:cef "" c:\tmp3   或者  cef "c:\tmp1\" c:\tmp3

 

 

 参考:https://bbs.csdn.net/topics/370122833

=======================================================================================

 

posted on 2021-04-06 10:26  jack_Meng  阅读(3017)  评论(0编辑  收藏  举报

导航