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   jack_Meng  阅读(3123)  评论(0编辑  收藏  举报

编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
历史上的今天:
2018-04-06 树莓派时间不正确问题 ---- 几种修复方式
2018-04-06 树莓派安装系统并设置中文界面

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示

喜欢请打赏

扫描二维码打赏

支付宝打赏

主题色彩