user1097771
user1097771

Reputation:

Batch File: If registry key's data is equal to

I'm trying to make a .bat toggler for certain Explorer settings. To do this I need the batch file to query the Registry key's data and then set the key accordingly. For example, in ActionScript 3 or JavaScript it would be something along the lines of this:

if (HideFileExt == "00000000"){
    HideFileExt = 00000001;
else {
    HideFileExt = 00000000;
}

This way, every time it runs it will set the key's data to be the opposite of what it currently is - a toggler.

I have Google-d this extensively and after quite a long time of chopping up and splicing multiple examples, I eventually got this:

REG QUERY HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced /v HideFileExt /t REG_DWORD /d 00000000

if errorlevel 1 (
    echo Num 1
) else (
    echo Num 2
)

rem The "echo Num"s are there just so that I could eventually figure out what the errorlevel does

which returns the error:

ERROR: Invalid syntax.
Type "REG QUERY /? for usage.
num 1

If I remove the /d 00000000 from the REG QUERY then it returns the accurate data value of the key without error. I have also tried it with /d 0, /d 0x0 and /d 0x00000000 and they didn't work either.

Upvotes: 3

Views: 53844

Answers (3)

Andy McRae
Andy McRae

Reputation: 665

In 2020 this is how you can do it:

In cmd write:

powershell -executionpolicy unrestricted C:\path_to_powershell_file.ps1

Make a powershell file with the following code and save it (Example to check if chrome is default browser):

$path = 'HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.html\UserChoice'
$value = 'ProgId'
$path_verif = Get-itemProperty -Path $path | Select-Object -ExpandProperty $value
if($path_verif -match 'chrome'){
    Write-Host "Chrome is default browser" -ForegroundColor Green

} else{
    Write-Host "Chrome is NOT default browser. -ForegroundColor Red 
}

Upvotes: 0

SmithMart
SmithMart

Reputation: 2801

The Answer from Dennis is correct, but I thought id paste the whole batch file so you can see it all working.

REG QUERY "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced" /v "HideFileExt" | Find "0x0"
IF %ERRORLEVEL% == 1 goto turnoff
If %ERRORLEVEL% == 0 goto turnon

goto end
:turnon
REG ADD "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced" /v 

HideFileExt /t REG_DWORD /f /D 1
goto end

:turnoff
REG ADD "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced" /v 

HideFileExt /t REG_DWORD /f /D 0
goto end

:end
@exit

Upvotes: 5

Dennis
Dennis

Reputation: 14477

The /d switch doesn't do what you think. It is a modifier to the /f switch, which is used to specify a search pattern. Unfortunately, /v already defines a search pattern, and they do not get along.

To check whether HideFileExt is set to 0, you can pipe reg's result to find:

reg query HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced /v HideFileExt | find "0x0"
if errorlevel 1 echo "HideFileExt is 0"
if errorlevel 0 echo "HideFileExt is not 0"

Upvotes: 7

Related Questions