1. 程式人生 > 其它 >批處理指令碼:打包制定資料夾中的所有子資料夾

批處理指令碼:打包制定資料夾中的所有子資料夾

技術標籤:工具# 批處理批處理windows

@echo off
echo 資料夾子目錄打包指令碼 V1.0.0.20210106

setlocal enabledelayedexpansion

set sDstDirPath=%1
SET sDstDirPath="%sDstDirPath%"
SET sDstDirPath=%sDstDirPath:"=%
rem echo sDstDirPath=%sDstDirPath%

if "%sDstDirPath%"=="" (
	echo 目標資料夾路徑為空
	echo =========================
	echo 呼叫格式為CompressDir2Zip 目標資料夾路徑 壓縮程式路徑
	echo 壓縮程式路徑可以為空
	exit /b 1
)

set sZipExePath=%2
SET sZipExePath="%sZipExePath%"
SET sZipExePath=%sZipExePath:"=%
rem echo sZipExePath=%sZipExePath%

if "%sZipExePath%"=="" (
	set sZipExePath=C:\Program Files\7-Zip\7z.exe
)

if not exist "%sZipExePath%" (
	echo 壓縮程式%sZipExePath%不存在
	exit /b 2
)

pushd %~dp0

cd /d "%sDstDirPath%"
echo 即將打包下列資料夾
dir /B /A:D
echo ========打包開始==========
for /f "delims=" %%i in ('dir /B /A:D') do (
	set sDirName=%%i
	rem echo sDirName=!sDirName!
	set sZipName=!sDirName!.zip
	rem echo sZipName=!sZipName!
	if exist "!sZipName!" (del /Q "!sZipName!") >nul || goto :error
	"%sZipExePath%" a "!sZipName!" "!sDirName!" >nul || goto :error
	echo 已將%%i資料夾的打包
)

goto :success
echo ========打包結束==========
endlocal

:error
popd
echo Failed with error #%errorlevel%.
exit /b %errorlevel%

:success
popd
exit /b 0