How to add password command in zip bat file

8,210

Solution 1

It would have been easier for you if batch file was formatted properly. Last line of this batch file is responsible to execute 7z command line. This is the point where you can add -p option.

"%SevenZip%" a -pYourPassword -tzip "%DestZip%" -r "%BaseDir%\*.*"

I have also formatted the file

@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%\*.*"

When I try to extract output file, it asks for password:

enter image description here

To encrypt file name, you need to add -mhe=on option to encrypt file header. But this option does not work with zip format. To make it work for 7z format, make following changes:

change target file name

set DestZip=D:\Destinationtest\BACKUP.7z

Edit last line

"%SevenZip%" a -mhe=on -pYourPassword "%DestZip%" -r "%BaseDir%\*.*"

After these changes, it will ask for password every time.

Solution 2

you can use 7zip open source utility Steps

  1. Install 7zip
  2. Add "C:\Program Files\7-Zip" in the Environment Variables.
  3. Use the following sample code to create commands in your bat file.

Syntax -p{password}

{password} Specifies password. Examples

7z a archive.7z -psecret -mhe *.txt

compresses *.txt files to archive.7z using password "secret". Also it encrypts archive headers (-mhe switch), so filenames will be encrypted.

7z x archive.zip -psecret

extracts all files from archive.zip using password "secret".

Share:
8,210
Moaana
Author by

Moaana

Updated on September 18, 2022

Comments

  • Moaana
    Moaana over 1 year

    I have a script below which is working perfectly, all I need is to add a password & encryption while compressing. Any idea where do I put the -p command to make it work? I tried but it is not working for me.

    @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%\*.*"
    
  • Moaana
    Moaana almost 6 years
    I already add '-p' in my script but result file is not password protected. its seems '-p' command is not working or am i miss something here. I've also tried your formatting and save as new '.bat' file. ON run its scanning drive and give the error of access denied exactly this one paste.ubuntu.com/p/TbymKfbfQk
  • Sandeep
    Sandeep almost 6 years
    @T.j I see why you are getting error. I have updated the code (last line). Also, when I try to extract output file, it asked for password. I am using 7zip 18 (x64) version.
  • Moaana
    Moaana almost 6 years
    Damn all i do is to trying open and it shows the files in the .zip it's working and ask for pass when i extract it. Sometimes a little mistakes makes you work like hell. Thanks mate cheers. PS The file names are visible but not extracting until you put the password is there any way to hide the names of the files
  • Sandeep
    Sandeep almost 6 years
    Yes, 7zip do have option to encrypt file names too but it does not work with zip format. For 7z format, you can set backup file name as BACKUP.7z and update last line as "%SevenZip%" a -mhe=on -pPassword@1 "%DestZip%" -r "%BaseDir%\*.*"