Locating the 7z.exe command-line tool on Windows
Posted by jpluimers on 2018/09/18
From one of my scripts: it will find a 64-bit 7z.exe if it was installed as part of the 7-zip installer, then run it with the parameters provided to the batch file.
setlocal
:verify7zip
:: registry trick from http://www.robvanderwoude.com/files/sortdate2_nt.txt
:: extra trick: tokens=2* allows to get the 3rd (and beyond: space delimited!) value in one variable %%b
for /F "tokens=2*" %%a IN ('REG QUERY "HKEY_LOCAL_MACHINE\SOFTWARE\7-Zip" /v Path64 2^>nul') do set sevenZipDirectoryPath=%%b
call :checkMissingSetting sevenZipDirectoryPath || goto :help
set sevenZipExeFilePath=%sevenZipDirectoryPath%7z.exe
if not exist "%sevenZipExeFilePath%" call :showError "No 7-zip executable at %sevenZipExeFilePath%" || goto :help
:run7zip
"%sevenZipExeFilePath%" %*
endlocal
goto :end
:checkMissingSetting
if not defined %1 call :notifyMissingSetting %1 && exit /b 1
call :showSetting %1
exit /b 0
goto :end
:notifyMissingSetting
echo Registry didn't provide the environment variable "%1"
goto :end
:showError
:: remove double quotes using tilde trick:
echo %~1
:help
echo Syntax: %0 7z.exe-commandline-parameters
goto :end
:end
–jeroen






Leave a comment