Getting errorlevel from usebackq - batch-file

i've ran into a problem with adding registry keys using reg.exe via batch.
I'm using this:
FOR /F "usebackq tokens=3*" %%A IN (`REG QUERY "HKEY_LOCAL_MACHINE\SOFTWARE\iNiT" /v Basic`) DO (
SET BASICVALUE=%%A %%B
)
ECHO ERROR: %ERRORLEVEL%
EDIT:
Normaly REG.exe outputs a errorlevel when executed;
0 - Successful
1 - Failed
And i get:
ERROR:
Somehow the errorlevel gets wiped or not saved.
I need to get the %errorlevel% out of it when executed, how do i do this, this dosn't seem to work.
Can you somehow set the errorlevel to a variable? I've tested this:
FOR /F "usebackq tokens=3*" %%A IN (`REG QUERY "HKEY_LOCAL_MACHINE\SOFTWARE\iNiT" /v Basic && SET ERROR=%ERRORLEVEL%`) DO (
SET BASICVALUE=%%A %%B
)
ECHO %ERROR%
Nor does that work.
%% wasn't expected
Any information would be helpful :)

REG QUERY "HKEY_LOCAL_MACHINE\SOFTWARE\iNiT" /v Basic > tempFile.txt
echo %errorlevel%
FOR /F "usebackq tokens=3*" %%A IN (tempFile.txt) DO (
SET BASICVALUE=%%A %%B
)

I've used the below code in one of my script and may answer your question -
reg query "HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\VisualSVN\VisualSVN Server" /V RepositoriesRoot >nul 2>&1 & if %errorlevel%==1 (echo.Visual SVN not installed)
Cheers, G

Related

For /F binary reg value neq (result = always success)

What I'm I missing?
I try to grab a binary key value from UserPreferencesMask, the binary value is 9032078010000000 (or hex:90,32,07,80,10,00,00,00 in regedit).
Result %%A does print 9032078010000000, but when I use this result in/with if %% NEQ or EQU I always goto:HEX_okay.
for /f "skip=2 tokens=3 delims= " %%A in ('reg query "HKCU\Control Panel\Desktop" /f UserPreferencesMask /d /T REG_BINARY') do (
echo.RESULT="%%A"
REM if %%a NEQ 9032078010000000 goto FiX
if %%A EQU 9032038010000000 goto HEX_okay
goto:FiX
)
I use skip=2 because I don't use the first 2 lines,
and delims= " to skip to the 3rd token, sort of speak...
edit:
The UserPreferencesMask has to be 9032078010000000 (binary value), if not, change it to this specific binary value (use reg add).
ie. REG.exe ADD "HKCU\Control Panel\Desktop" /V "UserPreferencesMask" /T REG_BINARY /D "9032078010000000" /F
I also tried:
for /f "tokens=3" %%i in ('reg query "HKCU\Control Panel\Desktop" /v UserPreferencesMask /t REG_BINARY') do (
echo.RESULT="%%i"
IF NOT %%i equ 9032038010000000 goto FiX
IF %%i equ 9032038010000000 goto HEX_okay
goto:DO_NOTING
)
...But with same result and changing equ into == doesn't help either.
when I change the value for UserPreferencesMask in regedit and place aa (or what ever) I do get some result; goto FiX. BUT when I just add some numbers (in regedit) then I always goto HEX_okay, so annoying :'(
These next changes (below) don't help either, then it's always, goto FIX:
IF NOT "%%i"=="9032038010000000" goto FiX
IF "%%i"=="9032038010000000" goto HEX_okay
These next lines do seem to "work", WELL SORT OFF:
when I change the binary value of "UserPreferencesMask" like a lot than just 1 or 2 digits, then it does seem to work as expected...
Weird and not completely 'monkey proof', it's NOT always doing as expected.
for /f "skip=2 tokens=3 delims= " %%i in ('reg query "HKCU\Control Panel\Desktop" /F "UserPreferencesMask" /D /C /E /T REG_BINARY') do (
echo.RESULT="%%i"
IF NOT %%i equ 9032038010000000 goto FiX
IF %%i equ 9032038010000000 goto HEX_okay
goto:NO_UserPreferencesMask
)
PS. I can use for /f "tokens=3" for the same result though
This workaround does seem to function though:
reg query "HKCU\Control Panel\Desktop" /F "UserPreferencesMask" /D /C /E /T REG_BINARY | find /i "9032078010000000"
if errorlevel 1 goto FiX
if errorlevel 0 goto HEX_okay
Here's how I would do it using Reg.exe in a For loop:
#Echo Off
Set "DUPM=9032078010000000"
Set "MASK="
For /F "EOL=H Tokens=2*" %%A In (
'Reg Query "HKCU\Control Panel\Desktop" /V UserPreferencesMask'
) Do If /I Not "%%B"=="%DUPM%" Set "MASK=%%B"
If Not Defined MASK GoTo HEX_okay
:FiX
Echo The value data %MASK% needs fixing!
Pause
GoTo :EOF
:HEX_okay
Echo The value data matches %DUPM%!
Pause
GoTo :EOF
EditBased upon your comment, and given that you're still not using the Reg Query options correctly/efficiently, here is how you should perform that task:
#Echo Off
Reg Query "HKCU\Control Panel\Desktop" /V UserPreferencesMask|Find /I "9032078010000000" >Nul && GoTo HEX_okay
:FiX
Echo The value data needs fixing!
Pause
GoTo :EOF
:HEX_okay
Echo The value data matches!
Pause
GoTo :EOF
Side note: as mentioned in my comments why aren't you simply adding the key? If the existing key matches, overwriting it will not matter, if it doesn't you've changed it:
Reg Add "HKCU\Control Panel\Desktop" /V UserPreferencesMask /T REG_BINARY /D 9032078010000000 /F>Nul

.Bat/CMD - Hiding "ERROR" with reg.exe >nul 2>&1

FOR /F "tokens=2*" %%A IN (
'REG QUERY "HKLM\Software\EA GAMES\Need for Speed Most Wanted" /v InstallDir'
) DO (set HBMU=%%B)
GOTO END
how I can add If %ERRORLEVEL% == 1 goto CLOSE to my code? because when i put under ) DO (set HBMU=%%B) it does not work.
Try this, after completing the rest of the registry key on line 2:
For /F "Skip=1 Tokens=2*" %%A In (
'Reg Query "HKLM\SOFTWARE\..." /V "InstallDir" 2^>Nul'
) Do Set "HBMU=%%B"
You cannot redirect both to Nul otherwise nothing will be output from the loop as variable %%B
Edit
Here's a complete script which should do what your commented code was supposed to do. (As long as your software definitely places it's information in the registry according to the Operating System architecture).
#Echo Off
Set "EAG=EA GAMES\Need for Speed Most Wanted"
Set "RKM=\"
Reg Query "HKLM\Hardware\Description\System\CentralProcessor\0"^
/V "Identifier" 2>Nul|Find /I "x86">Nul||Set "RKM=\Wow6432Node\"
For /F "EOL=HTokens=2*" %%A In ('Reg Query "HKLM\Software%RKM%%EAG%" /V^
"InstallDir" 2^>Nul') Do Start "" "%%~B\unins000.exe"
Pause

Batch - Reg query + if exists doesnt work together?

I have a piece of code I want to use
reg query HKLM\SYSTEM\CurrentControlSet\Services\SharedAccess\Parameters\FirewallPolicy\StandardProfile /v EnableFirewall if %ERRORLEVEL% EQU 0 echo On
This will check if the firewall is enable if so it will echo "On".
I try testing this command but my output is:
ERROR: Invalid syntax.
Type "REG QUERY /?" for usage.
Does anybody knows how to let this code work?
Luseres
Using for you can do something like:
#echo off
for /f "tokens=3 delims= " %%i in ('reg query HKLM\SYSTEM\CurrentControlSet\Services\SharedAccess\Parameters\FirewallPolicy\StandardProfile /v EnableFirewall') do (
if "%%i" equ "0x1" (
echo activated
) else (
echo not activated
)
)

Search for data value with Reg.exe and delete parent key

I am currently trying to use reg query commands to find the data value "teststring" in a value with a random name in HKCU\Software\random characters. I want the script to find the teststring value and then delete the parent key HKCU\Software\random characters. I have tried various ways with scripting and this is what I have so far, however I keep getting syntax errors and can't get reg query to find what I need it to:
#echo off
setlocal enabledelayedexpansion
set Key="HKCU\Software"
set STRING="teststring"
for /f "delims=" %%a in ('reg.exe query "%Key%" /f "%STRING%" /d /s') do (
call :GetValueName Value "%%a"
ECHO reg.exe delete "%Key%" /v "!Value!" /f
)
goto :eof
:GetValueName
set Return=
for %%a in (%~2) do (
if "%%a"=="REG_SZ" (
set %1=!Return:~1!&goto :eof
) else (
set Return=!Return! %%a
)
)
:eof
Here's a simpler way to do it.
Remove the ECHO (not the echo) AFTER you are satisfied with what is displayed.
#echo off
set "Key=HKCU\Software"
set "STRING=teststring"
for /f "delims=" %%a in ('reg query "%Key%" /v test /s /d /f "%STRING%" /t REG_SZ') do (
echo %%a | findstr /v "REG_SZ" | findstr /v /c:"End of search:" >nul && ECHO reg.exe delete "%%a" /f
)
pause

Issues with batch file to search registry and edit

I'm not so new to batch scripting and have done a small amount of scripts for various other things but this script has stumped me.
I've actually pulled this idea from somewhere else as I'm not that deep into the for commands just yet.
What I'm trying to do with this script is search every subkey within the HKU rootkey for a specific subkey path. If that subkey path exists it'll modify a key value within that subkey path. But it seems to keep failing with no error.
This is what I have right now:
for /f %%a in ('reg query hku') do call :loop1 %%a
goto :end
:loop1
for /f %1 in (reg query %1\software\microsoft\dynamics) do call :loop2 %%b
goto :end
:loop2
if Errorlevel 1 goto :error
reg add %1\6.0\configuration /v configurationfile /t reg_sz /d \ /f
goto :end
:error
echo Error has occurrd.
goto :end
:end
Pause
When I run this batch I get the following.
c:\Users\-username-\Desktop\test>for /F %a in ('reg query hku') do call :loop1 %a
c:\Users\-username-\Desktop\test>call :loop1 HKEY_USERS\.DEFAULT
c:\Users\-username-\Desktop\test>for /f HKEY_USERS\.DEFAULT in (reg query HKEY_USER
S\.DEFAULT\software\microsoft\dynamics) do call :loop2 %b
c:\Users\-username-\Desktop\test>
It seems like it just stops running? when I check the errorlevel after it runs it returns "0" so I'd think I'd at least see the error message come up?
Am I missing something small I'm just looking over?
Run this to start with - see if what it returns is helpful to you:
This may need a later version of Windows - I'm not sure of the reg query options for earlier windows.
#echo off
for /f "delims=" %%a in ('reg query hku /s /f data /k ^| find /i "\software\microsoft\dynamics" ') do echo "%%a"
You need GOTO :eof to return from the subroutines.
for /f %%a in ('reg query hku') do call :loop1 "%%a"
pause
goto :eof
:loop1
for /f "%~1" in (reg query "%~1\software\microsoft\dynamics") do call :loop2 "%%b"
goto :eof
:loop2
if Errorlevel 1 echo Error has occurred. & pause & exit /B 1
reg add "%~1\6.0\configuration /v configurationfile" /t reg_sz /d \ /f
goto :eof
I found the bug(s). It was two places. RGuggiesberg, you are right. I needed the EOF in there. As foxidrive points out, the first line of "loop1" had some syntax issues. I was confusing myself.
Replaced:
for /f %1 in ('reg query %1\software\microsoft\dynamics') do call :loop2 %%b
with:
for /f %%b in ('reg query %1\software\microsoft\dynamics') do call :loop2 %%b
Now it's working fine. Thanks for the pointers!

Resources