简体   繁体   中英

Get Registry Value into a variable in a batch file and use it in an if case

I would like to get a Registry Value (in my case true or false) and depending on the state (true or false) I would like to execute some commands.

I have found a few answers that might be working, but i was unable to get them to work (I'm not experienced with batch files at all)

Now to the more case specific question. I am changing a registry value depending on if my PC is in Sleep or Active. I now want to read the registry value and if the value is true (which means the PC is active) I want the script to do nothing, but if the value is false (which means the PC is in Sleep) I want to execute a few commands that will wake the PC from Sleep, wait 30 minutes and put the PC back to sleep.

I really would appreciate examples if I'm not asking for too much.

In my case I change the value with the following commands:

reg add HKCU\Software\Current_State /t REG_SZ /v Status_Active /d true /f

or

reg add HKCU\Software\Current_State /t REG_SZ /v Status_Active /d false /f

My idea was to check the value inside an if case and do actions depending on the value, but I have no idea how I should do that.

Additional info from the command section to better express what I'm looking for:

Yeah that's right, my PC is already waking itself from sleep at 1am and goes back into sleep 30 minutes later (or whatever i use in the script, its 1800 seconds atm). I am also changing a registry entry from true to false depending one the state I'm in, "true" if I am using it and "false" if the PC is already in sleep. I now only need to check which value the registry entry has and depending on that use a few commands(if the value is false) or not (if the value is true)

And here is a picture where the registry entry is located if that helps: http://i.imgur.com/tHTbgOy.png it is the "Status_Active" one which changes from true to false and vice versa

My solution (quote from the comment section):

I have found a workaround to that, I'm now using a one liner after the reg query command:

if %errorlevel% equ 0 
   (echo PC is Active & Echo Aborting Operations) 
else 
   (echo PC was Sleeping & echo Continue Operation & timeout /t 3600 /nobreak & rundll32.exe powrprof.dll,SetSuspendState 0,1,0)

Ok, we can use (as you already are) the reg utility , but in a form slightly different that you pointed out: instead of having a ( boolean ) value that will store the computer state we'll use the existence/not existence of a registry key (as opposed to most of Ux shells Win cmd shell is not smart enough to be able to assign a command output to an environment variable), but we can use the return code.

So, considering the registry key : HKCU\\Software\\Current_State\\Sleeping (no subkeys or values required under it), we will use it as a flag: when the computer goes to sleep, it will create it, when it wakes up, it will delete it.

Here's the .bat file skeleton:

@Edit1: First time I tried this .bat on XP , and it ran OK with/without the key present. When moving to W7 , when the key is present it apparently executes all the statements. That is because of the (enclosing) paranthesis from the first echo command (which indicate that the if block ends there). They need to be escaped (I will also try this on XP ):

@echo off
set SENTINEL_KEY=HKCU\Software\Current_State\Sleeping
reg query %SENTINEL_KEY%
if %errorlevel% equ 0 (
    echo command returned 0 ^(success^), the key was found ^(the computer is sleeping^)
    echo do your extra operations here
    echo you might want to delete the key just before waking up the computer if you're not doing that from some place else
    echo delete the key using: reg delete %SENTINEL_KEY% /f
) else (
    echo command returned !=0 ^(1, error^), the key was not found ^(the comuter is awake^)
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM