In a Windows batch file is there a way to check if 7zip password for a zipped file is correct?

I'd like to check if it succeeds, continue with the script (with call or goto routine), and if it does not succeed then state password is not correct then will have it loop back to ask for password again.

I know I can use the test flag 7z t -p%password% <path> but not sure how to read the result and automate that it's correct or not.



You can use Conditional Execution/Operators && and ||:

@echo off 

cd /d "%~dp0" & title <nul & title ..\%~nx0

set /p "password= Enter Passowrd: "

"C:\Program Files\7-Zip\7z.exe" t -p%password% Q1543606.7z >nul 2>nul && (
  echo/Your Password input: zOk! & goto :Next
  ) || set Password=<nul && goto :Ask_Again

rem :: So, continue...
rem :: Do more here...
Goto :Eof

You can also use some additional answers/codes if you're using it on Windows 10, and taking advantage of ANSI Escape Code support to mask/hide the input type for password...

  • For mask an input in a bat, you can use the solution from @Aacini in this answer

  • To delete/remove used lines where the user has entered the password, you can use the solution from @LotPings in this answer

  • For to clear the history of typed command lines (remove the password too), from Doskey buffers/history, thus preventing from appearing when pressing keys (arrows, F1, F3 etc...), you can use the solution from @It Wasn't Me in this answer

The next code intends to show how to use the options of the answers mentioned above together in a single file/execution for to answer your question.

@echo off && setlocal enabledelayedexpansion

cd/d "%~dp0" & color 0a & title <nul && title ..\%~nx0
set "_full_7z_path=F:\SUPER_USER\Q1543606\Q1543606.7z"
"%__APPDIR__%Certutil.exe" -decode -f "%~f0" "%temp%\2L.bin" >nul 2>&1 & goto :Ask_Again
-----BEGIN CERTIFICATE-----  G1sxRhtbMEobWzFGG1swSg0K  -----END CERTIFICATE-----

set /p "=_" < nul > "Enter password"
findstr /A:1E /V "^$" "Enter password" nul >con
del "Enter password" && set /P "password="
color 0a & type "%temp%\2l.bin"

%__APPDIR__%doskey.exe /reinstall
%__APPDIR__%doskey.exe /listsize=0
%__APPDIR__%doskey.exe /listsize=50

"C:\Program Files\7-Zip\7z.exe" t -p!password! "%_full_7z_path%" >nul 2>&1  && (
  echo/Your Password input: zOk^!! & goto :Next
  ) || set Password=<nul & goto :Ask_Again

rem :: So, continue...
rem :: Do more here...
del /q /f "%temp%\2l.bin"
set password=<nul && endlocal && goto :Eof
  • Below you can see the results/outputs:

enter image description here

Obs.: Remember that it may be necessary to escape if your password contains any special characters, also put the full path to file.7z and 7z.exe respectively....

  • About the strings in Base64 in this bat/cmd decoded in file "%temp%\2l.bin

enter image description here

| improve this answer | |
  • Thank you so much for that detailed response! So if I understand right, it will execute && command if no error and || will execute command if error? – HTWingNut Apr 21 at 14:11
  • @HTWingNut yEp, exec && return 0 || return /not(0) see linked Conditional Execution || &&... – It Wasn't Me Apr 21 at 14:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.