7za: Reask password if wrong

1

I'm trying to extract a single file via a bash script with 7zip from a big container.

Command: 7za e archive.zip singlefilename

If the password is wrong 7zip creates an empty file called singlefilename and the script proceeds which is not intended.

Is there any switch to reask passwords if they are wrong with 7za? Works smoothly with unzip :/

Faulwurf

Posted 2017-01-03T19:02:29.357

Reputation: 13

If one of the answers solved your problem, please click on the checkmark underneath the answer to mark your question as solved. – Hydraxan14 – 2017-04-05T17:47:29.833

Answers

2

Exit Code

Same as Axel's answer, but (since you mentioned bash) here's some bash code to get you started:

#!/bin/bash
EXIT=1
while [ ! $EXIT -eq 0 ]
do
        # 7-zip stuff
        EXIT=$?
done

When most programs finish without any errors, they set the exit code to 0.

Testing for a non-zero exit code is often a good way to detect when a program failed in some way.

Hydraxan14

Posted 2017-01-03T19:02:29.357

Reputation: 578

0

Whenever you enter a wrong password, 7z will return a non-zero ERRORLEVEL to the calling shell.

This can be used to launch a retry as follows in a .cmd script:

:again
::   -y  allows overwrite of existing file
7z.exe e -y xxx.zip "xxx.txt"
if errorlevel 1 goto try_again
echo. got it!
goto xit

:try_again
echo. sorry! Try again ...
goto again

:xit

Note:
The stand-alone version 7za is not present in all installations.

Axel Kemper

Posted 2017-01-03T19:02:29.357

Reputation: 2 892