0

I'm trying to write a batch file that will unarchive .7z files...extracting them to their original folder. The problem I'm having is adding the password to the code. Can someone help?

Thanks!

Here is the code so far...

@echo off

FOR /D /r %%F in ("*") DO (
    pushd %CD%
    cd %%F
        FOR %%X in (*.7z) DO (
            "C:\Program Files\7-zip\7z.exe" x "%%X"
        )
    popd
)

2 Answers 2

1

Here is how I would do it too..

I am a little odd in the fact that I prefer to reduce the amount of code a few times... in one loop, where it is possible:

@echo off 

for /f tokens^=* %%i in ('2>nul %__AppDir__%where.exe /r "d:\your\folder" *.7z'
     )do "c:\program files\7-zip\7z.exe" x "%%~fi" -o"%%~dpi\" -p"YouRPassWord"
0

Here is how I would do it..

I am a little odd in the fact that I prefer using a function based approach with call :<function_name> and goto :EOF.

This solves problems that would otherwise require EnableDelayedExpansion (which I don't like using). To each their own... this code should work.

ALSO, REMOVE THE ECHO STATEMENT!! THIS IS HERE SO YOU CAN TEST THIS CODE BEFORE ACTUALLY RUNNING IT! ;)

@echo off
for /d /r %%d in (*) do call :ProcessDirectory "%%d"
goto :EOF

:ProcessDirectory
pushd %1
::Make sure pushd success.. otherwise, return to loop to the next dir
if not ERRORLEVEL 0 goto :EOF

for %%f in ("*.7z") do echo "C:\Program Files\7-zip\7z.exe" x %%f -pTHEPASSWORD
popd
goto :EOF
1
  • I think some secret agent are around here down answer without expose yourself...
    – Io-oI
    Apr 26, 2021 at 18:00

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.