我有下面这个脚本,它运行得很好,我只需要在压缩时添加密码和加密。你知道我把命令放在哪里才能-p
让它工作吗?我试过了,但它对我来说不起作用。
@echo off
setlocal enabledelayedexpansion
REM Define file and folder locations
set BaseDir=D:\SourceTest
set DestZip=D:\Destinationtest\BACKUP.zip
set SevenZip=C:\Program Files\7-Zip\7z.exe
REM Zip all files and folders
echo Getting time ...
for /f "tokens=1-9" %%a in ('wmic.exe Path Win32_LocalTime Get Day^,DayOfWeek^,Hour^,Minute^,Month^,Quarter^,Second^,WeekInMonth^,Year ^| find /v ""') do (
set /a Line += 1
if "!Line!"=="1" (set VarA=%%a&set VarB=%%b&set VarC=%%c&set VarD=%%d&set VarE=%%e&set VarF=%%f&set VarG=%%g&set VarH=%%h&set VarI=%%i)
if "!Line!"=="2" (set !VarA!=%%a&set !VarB!=%%b&set !VarC!=%%c&set !VarD!=%%d&set !VarE!=%%e&set !VarF!=%%f&set !VarG!=%%g&set !VarH!=%%h&set !VarI!=%%i)
)
for %%a in (Month Day Hour Minute Second) do (if !%%a! LSS 10 set %%a=0!%%a!)
set TimeStamp=%Year%%Month%%Day%_%Hour%%Minute%%Second%
echo Compressing '%BaseDir%' ...
for %%a in ("%DestZip%") do (set DestZip=%%~dpna-%TimeStamp%%%~xa)
"%SevenZip%" a -tzip "%DestZip%" -r "%BaseDir%\*.*"
答案1
如果批处理文件的格式正确,那么对您来说会更容易。此批处理文件的最后一行负责执行7z
命令行。这是您可以添加选项的地方-p
。
"%SevenZip%" a -pYourPassword -tzip "%DestZip%" -r "%BaseDir%\*.*"
我还格式化了文件
@echo off
setlocal enabledelayedexpansion
REM Define file and folder locations
set BaseDir=D:\SourceTest
set DestZip=D:\Destinationtest\BACKUP.zip
set SevenZip=C:\Program Files\7-Zip\7z.exe
REM Zip all files and folders echo Getting time ...
for /f "tokens=1-9" %%a in ('wmic.exe Path Win32_LocalTime Get Day^,DayOfWeek^,Hour^,Minute^,Month^,Quarter^,Second^,WeekInMonth^,Year ^| find /v ""') do (
set /a Line += 1
if "!Line!"=="1" (
set VarA=%%a&set VarB=%%b&set VarC=%%c&set VarD=%%d&set VarE=%%e&set VarF=%%f&set VarG=%%g&set VarH=%%h&set VarI=%%i
)
if "!Line!"=="2" (
set !VarA!=%%a&set !VarB!=%%b&set !VarC!=%%c&set !VarD!=%%d&set !VarE!=%%e&set !VarF!=%%f&set !VarG!=%%g&set !VarH!=%%h&set !VarI!=%%i
)
)
for %%a in (Month Day Hour Minute Second) do (
if !%%a! LSS 10 set %%a=0!%%a!
)
set TimeStamp=%Year%%Month%%Day%_%Hour%%Minute%%Second%
echo Compressing '%BaseDir%' ...
for %%a in ("%DestZip%") do (
set DestZip=%%~dpna-%TimeStamp%%%~xa
)
"%SevenZip%" a -pPassword@1 -tzip "%DestZip%" -r "%BaseDir%\*.*"
当我尝试提取输出文件时,它会要求输入密码:
要加密文件名,您需要添加-mhe=on
选项来加密文件头。但此选项不适用于zip
格式。要使其适用于7z
格式,请进行以下更改:
更改目标文件名
set DestZip=D:\Destinationtest\BACKUP.7z
编辑最后一行
"%SevenZip%" a -mhe=on -pYourPassword "%DestZip%" -r "%BaseDir%\*.*"
这些更改之后,它每次都会要求输入密码。
答案2
你可以使用 7zip 开源实用程序步骤
- 安装7zip
- 在环境变量中添加“C:\Program Files\7-Zip”。
- 使用以下示例代码在您的 bat 文件中创建命令。
句法-p{password}
{password} 指定密码。示例
7z a archive.7z -psecret -mhe *.txt
使用密码“secret”将 *.txt 文件压缩为 archive.7z。它还会加密存档头(-mhe 开关),因此文件名将被加密。
7z x archive.zip -psecret
使用密码“secret”从archive.zip中提取所有文件。