1

This works great for downloading 7zr for doing decompression tasks in a batch script if 7z.exe isn't in the working directory.

if not exist "7z.exe" curl -L "https://www.7-zip.org/a/7zr.exe" -o 7z.exe

But what if I have 7-zip installed and 7z.exe is in the PATH. I would prefer to skip downloading 7zr in that case, then continue with the decompression tasks.

Edit: This use case is just an example. My intention is to distribute batch scripts that download required stuff if necessary.

2

2 Answers 2

1

You can check the registry entry and check the errolevel that will return from this query :

Reg Query "HKEY_CURRENT_USER\SOFTWARE\7-Zip" /v "Path"

And here is a whole example that check for the path of 7-zip in the registry and if it not exist he download it.

@echo off
Title check the path of 7-Zip 
Call :Check_7Zip
echo "%EXE_7Z_PATH%"
if not exist "%EXE_7Z_PATH%" Call :Download7Zip
pause & exit /b
::-----------------------------------------------------------------------------------------------------------------------------
:Check_7Zip
Reg Query "HKEY_CURRENT_USER\SOFTWARE\7-Zip" /v "Path">nul 2>&1
If [%errorlevel%] EQU [1] (Call :Download7Zip
    ) else (
    @FOR /f "tokens=2*" %%i in ('Reg Query "HKEY_CURRENT_USER\SOFTWARE\7-Zip" /v "Path" 2^>nul') do (Set "EXE_7Z_PATH=%%j7z.exe")
)
Exit /B
::-----------------------------------------------------------------------------------------------------------------------------
:Download7Zip
curl -L "https://www.7-zip.org/a/7zr.exe" -o 7z.exe
Exit /B
::------------------------------------------------------------------------------------------------------------------------------
1
  • Ignore my previous comment. I am being stupid this morning. This is REALLY nice work but still doesn't answer how to tell if something is in the actual path. Aug 31, 2022 at 16:42
1

If not exist 7z.exe in PATH try this:

where /q 7z.exe || curl -L "https://www.7-zip.org/a/7zr.exe" -o 7z.exe

If 7z.exe already exists just output the 7z.exe path and if not exist it will running curl -L "https://www.7-zip.org/a/7zr.exe" -o 7z.exe:

where 7z.exe 2>nul || curl -L "https://www.7-zip.org/a/7zr.exe" -o 7z.exe

Example

C:> where /q 7z.exe || curl -L "https://www.7-zip.org/a/7zr.exe" -o 7z.exe
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  564k  100  564k    0     0   240k      0  0:00:02  0:00:02 --:--:--  240k

C:\>set "path=%cd%;%path%"
C:\>where 7z.exe
C:\7z.exe

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .