How to detect in 7-Zip fatal error #2 "Wrong password" in windows batch?

Microsoft Windows
Post Reply
PiotrMP006
Posts: 19
Joined: 2021-Sep-01, 10:57 am

How to detect in 7-Zip fatal error #2 "Wrong password" in windows batch?

Post by PiotrMP006 »

Hi

How to detect in 7-Zip fatal error #2 "Wrong password" in windows batch?

Code: Select all

ERROR: a.7z : Cannot open encrypted archive. Wrong password?
Syntax
Posts: 3
Joined: 2023-Oct-05, 9:37 pm

Re: How to detect in 7-Zip fatal error #2 "Wrong password" in windows batch?

Post by Syntax »

I have not done extensive testing, but it exits with an errorlevel of 2 if you input the wrong password to extract a 7z file.

Code: Select all

C:\Program Files\7-Zip>7z x %userprofile%\downloads\file.7z

7-Zip 23.01 (x64) : Copyright (c) 1999-2023 Igor Pavlov : 2023-06-20

Scanning the drive for archives:
1 file, 32818 bytes (33 KiB)

Extracting archive: C:\Users\user\downloads\file.7z
--
Path = C:\Users\user\downloads\file.7z
Type = 7z
Physical Size = 32818
Headers Size = 162
Method = LZMA2:19 7zAES
Solid = -
Blocks = 1


Enter password (will not be echoed):
ERROR: Data Error in encrypted file. Wrong password? : file

Sub items Errors: 1

Archives with Errors: 1

Sub items Errors: 1

C:\Program Files\7-Zip>echo %errorlevel%
2
Simon_Weel
Posts: 31
Joined: 2021-Dec-13, 3:53 pm

Re: How to detect in 7-Zip fatal error #2 "Wrong password" in windows batch?

Post by Simon_Weel »

You could try to intercept the error text 'ERROR: Data Error in encrypted file. Wrong password?' by piping the output of 7Zip to the Find command.
Don't know the exact syntax for 7Zip, but maybe something like

Code: Select all

7z L %userprofile%\downloads\file.7z | find /i "wrong password"
And then check the errorlevel. This will list the content of the ZIP-file and I guess you need a password for that as well, if a password is set.
Post Reply