使用批处理脚本将多个文件压缩成多个 zip 文件

Zipping multiple files into multiple zip files with batch script

我需要获取一个批处理脚本 运行,它获取一个源文件夹并递归地通过所有子文件夹将每个文件压缩到它自己的 zip 文件中,并将 zip 文件保存到给定的目的地。

这是一个工作变体,它只将源文件夹压缩并将压缩文件保存到目的地:

 @echo off


 set year=%date:~-4,4%
 set month=%date:~-10,2%
 set day=%date:~-7,2%
 set hour=%time:~-11,2%
 set hour=%hour: =0%
 set min=%time:~-8,2%

 set zipfilename=%~n1.%year%_%month%_%day%_%hour%_%min%
 set destination=%~dp1

 set source="%~1\*"
 set destpath="%~2"
 set destname="%~3"
 set ziptyp="%~4"
 set dest= "%destpath%\%destname%_%year%_%month%_%day%_%hour%_%min%.%ziptyp%"
 REM "%destination%Backups\%zipfilename%.%desttyp%"

 IF [%1]==[/?] GOTO BLANK
 IF [%1]==[] GOTO BLANK
 IF [%1]==[/h] GOTO BLANK
 IF [%1]==[?] GOTO BLANK

 set AppExePath="%ProgramFiles(x86)%-Zipz.exe"
 if not exist %AppExePath% set AppExePath="%ProgramFiles%-Zipz.exe"

 if not exist %AppExePath% goto notInstalled

 echo Backing up %source% to %dest%


 if /I %ziptyp%=="zip" ( 
 %AppExePath% a -rtzip %dest% %source%)

 if /I %ziptyp%=="7z" ( 
 %AppExePath% a -rt7z %dest% %source%)


 echo %source% backed up to %dest% is complete!

 goto end

 :BLANK


 goto end

 :notInstalled

 echo Can not find 7-Zip


 :end

为此我需要更改以下部分:

 if /I %ziptyp%=="zip"

和 如果 /I %ziptyp%=="7z"

我尝试了以下方法以及更多方法:

 (
 cd  %source%
 FORFILES %source% /M *.* /C "%AppExePath% a -rtzip "%%~nG.zip" ".\%%G\*"")

 FOR /R %source% %%G in (.) do ( 
 Pushd %%G 
 %AppExePath% a -rtzip "%%~nG.zip" ".\%%G\*"
 Popd )

 for /R %%a in (%source%) do (zip -r -p "%%~na.zip" ".\%%a\*")

有人知道如何让它工作吗?

提前致谢,

流浪汉

你很接近!

我刚刚测试了一下,这似乎能够处理一个目录中的每个文件:

@echo off
cd /D "%~1"
for /r %%i in (*) do (
    REM Process file here!
    echo Full path: %%i
    echo Filenames with extension: %%~nxi
)

这将移动到您作为参数提供的路径,然后使用 * 而不是 . 将获取每个文件。您现在可以按照您想要的方式处理它。

要检查如何根据您的需要修改结果,我建议阅读 this great answer 关于路径参数修改的内容。

感谢aschipfl提醒我/D的cd和路径的修改!