復制目錄結構
使用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
調用方式,如: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
調用方式,如: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
執行方式,如:cef "" c:\tmp3 或者 cef "c:\tmp1\" c:\tmp3
參考:https://bbs.csdn.net/topics/370122833
=======================================================================================
刪除空文件夾
批處理遞歸刪除當前及子目錄中的空文件夾
將以下代碼復制保存至def.bat文件中,直接雙擊運行即可(不推薦使用)
@echo off :deldir setlocal enabledelayedexpansion for /d %%a in (%1*) do ( set p="%%a\" set p=!p:.\"=..\"! rd !p! || ( call :deldir !p! & rd !p! ) )
出處:https://www.cnblogs.com/hhddd-1024/p/14275097.html
=======================================================================================
個人使用
優化:根據上面的思路修改,必須指定目標文件夾參數,調用方式:def.bat "c:\tmp"
@echo off cls :deldir setlocal enabledelayedexpansion if [%1] equ [] echo 未指定文件夾 & goto :eof set fp=%1 set fp="%fp%" & set fp=%fp:"=% if "%fp%" neq "" if "%fp:~-1%" neq "\" set fp=%fp%\ set fp="%fp%" for /d %%a in (%fp%*) do ( set p="%%a" set p=!p:.\"=..\"! rd !p! || ( call :deldir !p! & rd !p!) )
=======================================================================================
