Get Errorcode of Reg Query in one Line - batch-file

i want to get either 0 or 1 from the following reg query:
reg query "hklm\Software\Microsoft\Windows\Currentversion\WindowsUpdate\Auto Update\Rebootrequired"
Instead of getting the Updates or the Error Message, i want to output the Errorcode.
The Problem is, that the whole command must be put in one Line!
Something like "reg query.... 2>&1 | echo %Errorlevel%
Thanks!
Sorry for my bad English!

If you need to do it all on one line then you will need to force delayed expansion to be enabled so that you can echo the errorlevel correctly.
cmd /V:on /C "reg query "hklm\Software\Microsoft\Windows\Currentversion\WindowsUpdate\Auto Update\Rebootrequired" >nul 2>&1 &echo !errorlevel!"
You can also use this.
reg query "hklm\Software\Microsoft\Windows\Currentversion\WindowsUpdate\Auto Update\Rebootrequired" >nul 2>&1 &CALL echo %^errorlevel%

Related

Processing reg query output as a single line

I want to get some details from the Windows registry in a single line (one per key) format. But what I have tried so far gives me the details I want, but split over 2/3 lines, which makes post processing harder.
This is on Windows 10.
One option is to query the registry, so I run the following:
reg query hklm\system\currentcontrolset\enum /s /f "DeviceDesc"
This gives me output in the following format (snippet):
HKEY_LOCAL_MACHINE\system\currentcontrolset\enum\HID\VID_1A2C&PID_2124&MI_01&Col02\7&2a45f711&0&0001
DeviceDesc REG_SZ #input.inf,%hid_device_system_control%;HID-compliant system controller
HKEY_LOCAL_MACHINE\system\currentcontrolset\enum\HID\VID_1A2C&PID_2124&MI_01&Col02\8&9a82e8&0&0001
DeviceDesc REG_SZ #input.inf,%hid_device_system_control%;HID-compliant system controller
HKEY_LOCAL_MACHINE\system\currentcontrolset\enum\HID\VID_2149&PID_2117&MI_00\7&1e3fba77&0&0000
DeviceDesc REG_SZ #input.inf,%hid_device_touch_screen%;HID-compliant touch screen
HKEY_LOCAL_MACHINE\system\currentcontrolset\enum\HID\VID_2149&PID_2117&MI_01\7&316fd6b5&0&0000
DeviceDesc REG_SZ #input.inf,%hid_device_vendor_defined_range%;HID-compliant vendor-defined device
HKEY_LOCAL_MACHINE\system\currentcontrolset\enum\HID\VID_24AE&PID_2003&MI_00\8&456ad84&0&0000
DeviceDesc REG_SZ #keyboard.inf,%hid.keyboarddevice%;HID Keyboard Device
The format is:
BLANK LINE
HKEY_LOCAL.....
DeviceDesc .....
What I want is the HKEY_LOCAL... and DeviceDesc to appear on the same line of output text, so that I can use FIND/FINDSTR to get the complete info for the device I am interested in.
With the output as it stands, I cannot get the two piece of information together using DOS commands.
Is there a way to make DeviceDesc appear of the same line ?
I could write a Java/C# for this, but it seems overkill.
The command REG has no options to define the output format.
A FOR loop can be used to concatenate registry key and the device description string value for output on one line. The entire output of FOR loop can next be filtered with command FINDSTR for the device description of interest:
#echo off
setlocal EnableExtensions EnableDelayedExpansion
(for /F "tokens=1,2*" %%A in ('%SystemRoot%\System32\reg.exe query HKLM\System\CurrentControlSet\enum /s /f "DeviceDesc"') do if /I not "%%A" == "DeviceDesc" (set "RegKey=%%A") else echo !RegKey! %%C) | %SystemRoot%\System32\findstr.exe /I /L /C:"HID-compliant touch screen"
endlocal
Please note that registry keys or description values containing one or more ! are not correct processed by this batch code because of enabled delayed environment variable expansion.
There are four spaces used to separate registry key from device description. It is of course also possible to use for example a horizontal tab.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
echo /?
endlocal /?
findstr /?
reg /?
reg query /?
set /?
setlocal /?
From what I understand, you want to combine each lines Key Path & Data. The easiest way to do this (From My knowledge) Is to grab each result of the FOR loop and use an IF statement to set the strings we will later combine.
Bellow you will find the script - In my case I just outputted the results to a document. This is fine and can be called later to with a TYPE statement inside of a FOR. However if you want to do something with these variables in the loop, just simply continue your code in-place of the ECHO [!Location! !Data!] >> Output.txt.
#ECHO OFF
#setlocal EnableDelayedExpansion
Set "RUN=0"
for /F "tokens=*" %%A in ('reg query hklm\system\currentcontrolset\enum /s /f "DeviceDesc"') DO (
Rem | Grab & organize output variables to string.
If "!RUN!"=="1" (
Rem | Second Cycle
Set "Data=%%A"
ECHO !Location! !Data! >> Output.txt
Rem | Restart Cycle
Set "Data="
Set "Location="
Set "RUN=0"
) ELSE (
Rem | First Cycle
Set "Location=%%A"
Set "RUN=1"
)
)
Goto :EOF

how to set a variable for the value of " ping 8.8.8.8 | find /c "TTL=" " [duplicate]

This question already has answers here:
Assign output of a program to a variable using a MS batch file
(12 answers)
Closed 9 months ago.
I'm trying to assign the output of a command to a variable - as in, I'm trying to set the current flash version to a variable. I know this is wrong, but this is what I've tried:
set var=reg query hklm\SOFTWARE\Macromedia\FlashPlayer\CurrentVersion>
or
reg query hklm\SOFTWARE\Macromedia\FlashPlayer\CurrentVersion >> set var
Yeah, as you can see I'm a bit lost. Any and all help is appreciated!
A method has already been devised, however this way you don't need a temp file.
for /f "delims=" %%i in ('command') do set output=%%i
However, I'm sure this has its own exceptions and limitations.
This post has a method to achieve this
from (zvrba)
You can do it by redirecting the output to a file first. For example:
echo zz > bla.txt
set /p VV=<bla.txt
echo %VV%
You can't assign a process output directly into a var, you need to parse the output with a For /F loop:
#Echo OFF
FOR /F "Tokens=2,*" %%A IN (
'Reg Query "HKEY_CURRENT_USER\Software\Macromedia\FlashPlayer" /v "CurrentVersion"'
) DO (
REM Set "Version=%%B"
Echo Version: %%B
)
Pause&Exit
http://ss64.com/nt/for_f.html
PS: Change the reg key used if needed.
Okay here some more complex sample for the use of For /F
:: Main
#prompt -$G
call :REGQUERY "Software\Classes\CLSID\{3E6AE265-3382-A429-56D1-BB2B4D1D}"
#goto :EOF
:REGQUERY
:: Checks HKEY_LOCAL_MACHINE\ and HKEY_CURRENT_USER\
:: for the key and lists its content
#call :EXEC "REG QUERY HKCU\%~1"
#call :EXEC "REG QUERY "HKLM\%~1""
#goto :EOF
:EXEC
#set output=
#for /F "delims=" %%i in ('%~1 2^>nul') do #(
set output=%%i
)
#if not "%output%"=="" (
echo %1 -^> %output%
)
#goto :EOF
I packed it into the sub function :EXEC so all of its nasty details of implementation doesn't litters the main script.
So it got some kinda some batch tutorial.
Notes 'bout the code:
the output from the command executed via call :EXEC command is stored in %output%. Batch cmd doesn't cares about scopes so %output% will be also available in the main script.
the # the beginning is just decoration and there to suppress echoing the command line. You may delete them all and just put some #echo off at the first line is really dislike that. However like this I find debugging much more nice.
Decoration Number two is prompt -$G. It's there to make command prompt look like this ->
I use :: instead of rem
the tilde(~) in %~1 is to remove quotes from the first argument
2^>nul is there to suppress/discard stderr error output. Normally you would do it via 2>nul. Well the ^ the batch escape char is there avoids to early resolving the redirector(>). There's some simulare use a little later in the script: echo %1 -^>... so there ^ makes it possible the output a '>' via echo what else wouldn't have been possible.
even if the compare at #if not "%output%"==""looks like in most common programming languages - it's maybe different that you expected (if you're not used to MS-batch). Well remove the '#' at the beginning. Study the output. Change it tonot %output%==""-rerun and consider why this doesn't work. ;)
This is work for me
#FOR /f "delims=" %i in ('reg query hklm\SOFTWARE\Macromedia\FlashPlayer\CurrentVersion') DO set var=%i
echo %var%

Bat file stopping and i'm not sure why

So, I have a bat file I've been building to do a simple profile backup/restore. I'm running Windows 7 64bit.
I get through 99% of the script until it gets to this point and then dies. To be clear, in the environment I'm working in, I HAVE to turn off UAC for certain things. Someone much higher up than me made that call.
However, this needs to do this but it dies after the "[ COMPLETE ]" is echoed. It doesn't close the window, it just goes back to the C prompt.
The only other thing it's going (trying) to do after that is log the user off after a 15 second timer.
Any help would be much appreciated.
echo [ Turning off UAC... ]
C:\Windows\System32\cmd.exe /k %windir%\System32\reg.exe ADD HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System /v EnableLUA /t REG_DWORD /d 0 /f
ping -n 2 -w 1000 127.0.0.1 > nul
echo [ COMPLETE ]
ping -n 2 -w 1000 127.0.0.1 > nul
echo.
ECHO ==========================================================
ECHO = Restore Complete =
ECHO = Computer will logoff in 15 seconds to apply changes. =
ECHO ==========================================================
TIMEOUT 16
shutdown /l
#pause
:EOF
I made a test batch file that does what you're trying:
#echo off
echo before
cmd /k echo I'm doing it!
echo after
And here was the result:
C:\files\j>test
before
I'm doing it!
So I changed it to call:
#echo off
echo before
call echo I'm doing it!
echo after
And got this:
C:\files\j>test
before
I'm doing it!
after
So clearly, /k isn't for you. But CALL will put it in a new shell... So my recommendation is to just change that line to be :
%windir%\System32\reg.exe ADD HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System /v EnableLUA /t REG_DWORD /d 0 /f
No cmd, no \k, no anything - just run the REG.exe.
If you have to, use the CALL since you're adding a registry key and it should stay there.

Switch between two .reg files

I'm looking for a quick and easy way (I don't need any error checking) to switch between two reg files by opening a batch file.
If a user runs a batch file, the batch file should merge reg file A to the registry. If the user runs it again, the batch file should merge reg file B to the registry. If the user runs it again, file A is merged... you get the point.
In general, I think there are two ways to do this:
Create some sort of variable to store which reg should be run next.
Check the registry key values to see which reg has been merged most recently (probably the preferred method).
Currently, I'm doing this:
REG QUERY "KeyName" /v "ValueName" | Find "x"
IF ERRORLEVEL 1 regedit /S file1.reg
IF ERRORLEVEL 0 regedit /S file2.reg
The REG QUERY part seems to work, but at the IF ERRORLEVEL statements something is going wrong. But maybe I should use a different method altogether.
Hoping for some suggestions.. Thanks in advance!
EDIT
Sorry for being a bit vague: I believe the previous solution didn't work, because when the first IF is correct, the second IF will be too after the first IF's command. So I will need an IF ELSE statement to prevent running the second IF.
I've now come up with this solution, which works:
REG QUERY "KeyName" /v "ValueName" | Find "x
IF ERRORLEVEL 1 (REGEDIT /S "file1.reg") ELSE REGEDIT /S "file2.reg"
I was also wondering: would it be an improvement to have the registry key/values added inside the batch file instead of using seperate .reg files? The value types are REG_BINARY and REG_DWORD.
I would try this:
REG QUERY "KeyName" /v "ValueName" | Find "x"
IF ERRORLEVEL 1 (
regedit /S file1.reg
goto :EOF
)
regedit /S file2.reg
The IF condition is set to TRUE when the ERRORLEVEL is equal to, or greater than, the ERRORLEVEL number (see here: http://support.microsoft.com/kb/39585/en-us) so the second IF condition is always evaluated as true.
You can store a value in a enviroment variable
#Echo OFF
:: By Elektro H#cker
Set | FIND "Merged" >NUL && Regedit /S "File_B.reg" || Regedit /S "File_A.reg" && SETX Merged YES >NUL
Pause&Exit
This is the same code but indented and with a quickly explanation:
#Echo OFF
:: By Elektro H#cker
REM If it's batfile first launch then I add a value "YES" to a variable and then only merges the File A.
REM If isn't batfile first launch then only merges FILE B
Set | FIND "Merged" >NUL && (
Regedit /S "File_B.reg"
) || Regedit /S "File_A.reg" && (
SETX Merged YES >NUL
)
Pause&Exit

Need batch file to query registry key and write computer name to a log file

I need to query the registry for HKCU\Software\test If this file exists I need to write the computer name to a log file. c:\Log.txt. I can query the registry but I have not been able to figure out how to use the if statement to add the computer name to the log file. Any help would be appreciated.
reg query "hkcu\software\test"
echo %COMPUTERNAME% >> c:\Log.txt
echo %COMPUTERNAME% >> c:\Log.txt
thats all :)
Try this:
#ECHO OFF
REG QUERY "HKCU\Software\test" >nul 2>&1
IF %ERRORLEVEL%==0 ECHO %COMPUTERNAME%>>C:\Log.txt
The >nul 2>&1 will hide the output of the REG command. If you want to see the output, just remove that part.
You could use the same approach as in this answer to your previous question, only use && in this case:
REG QUERY "whatever\you\want\to\query" >NUL && ECHO %COMPUTERNAME%>>C:\Log.txt
Similarly to FINDSTR, REG also sets ERRORLEVEL to a non-zero value if the search was unsuccessful, which allows us to use constructs with || and && as appropriate. The command after && is executed only if the search has been successful.
The above command suppresses standard output of REG with >NUL. If the search fails, the corresponding error message will still be displayed, because it is sent to the standard error device, rather than to the standard output. You can additionally suppress possible error messages by adding 2>NUL or like in #aphoria's answer.

Categories

Resources