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
Related
I am struggling to finish up this batch file and I am not sure what I am missing, although I am certain it's a small mistake. I think it's with my delims or possibly the subkey naming convention.
#echo off
#echo 12 (2014)
#echo 13 (2016)
#echo 14 (2017)
#echo 15 (2019)
set /p "version=Enter Version: "
set "value_name=ParentInstance"
set "value_data=MSSQL%version%E.LOCALDB"
set "key_path=HKEY_CURRENT_USER\SOFTWARE\Microsoft\Microsoft SQL Server\UserInstances"
for /f "tokens=2* delims={}" %%a in ('reg query "%key_path%" /s 2^>nul') do (
for /f "tokens=2*" %%b in ('reg query "%%b" /v "%value_name%" 2^>nul') do (
set "subkey=%%b"
goto :break
)
)
:break
if defined subkey (
reg add "%subkey%" /v "%value_name%" /t REG_SZ /d "%value_data%" /f
echo Updated Value data of key : %subkey% to : %value_data%
) else (
echo Subkey with value name "%value_name%" not found.
)
endlocal
TIMEOUT /t 5 /nobreak
I am looking for a way to retrieve all subkeys under
HKEY_CURRENT_USER\SOFTWARE\Microsoft\Microsoft SQL
Server\UserInstances
Then for each subkey check if it has a value named "ParentInstance" by querying the value under that subkey. If it finds any, it stores that subkey in the variable "subkey", then uses the reg add command to change the data of the value named "ParentInstance" to the value in the variable "value_data" which is "MSSQL%version%E.LOCALDB" (%version% is a number that gets input).
Example of Registry (UserInstances may include multiple subkeys)
To perform the task using the same methodology you laid out in your question:
Get all next level subkeys within your %key_path%, (I added some additional robustness targeting only those child keys with the format {*-*-*-*-*}).
For each of those subkeys, query if it has a value named %value_name%.
If it finds any, change the value data for %value_name% for that subkey using the reg add command to the content of the variable, %value_data%.
For /F "Delims=" %%G In ('%SystemRoot%\System32\reg.exe Query "%key_path%" /F "{*-*-*-*-*}" 2^>NUL ^| %SystemRoot%\System32\find.exe "\"') Do %SystemRoot%\System32\reg.exe Query "%%G" /F "%value_name%" /V /E 2>NUL 1>&2 && %SystemRoot%\System32\reg.exe Add "%%G" /V "%value_name%" /D "%value_data%" /F 1>NUL
set "key_path=HKEY_CURRENT_USER\SOFTWARE\Microsoft\Microsoft SQL Server\UserInstances"
for /f "tokens=2* delims={}" %%a in ('reg query "%key_path%" /s 2^>nul') do (
echo %%%% a=%%a %%%% b=%%b
for /f "tokens=2*" %%B in ('reg query "%%b" /v "%value_name%" 2^>nul') do (
echo %%%% B=%%B %%%% C=%%C
set "subkey=%%b"
goto :break
)
)
Note that the metavariables a, b, B and C is one of the few instances in batch where case is significant. %%b and %%B are different variable.
See for /? from the prompt for documentation.
for /f "tokens=2,*delims={}" %%u... will parse the text line in question, assign the second "token" to %%u and the rest-of-the-line-after-token-2 (*) to %%v.
Tokens are calculated left-to-right from the format {delimiters}token1{delimiters}token2{delimiters}token3{delimiters}token4... - leading delimiters optional, and {delimiters} is any string of any delimiter specified between delims= and ".
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
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
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
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!