Page 1 of 1

Power Plan Changer ignores the goto commands

Posted: 08 Aug 2011 15:07
by Jamisicus6
Hi Guys. First post here, so I apologize if this isn't how you'd usually see things on here.
I'm trying to create a power plan managing Batch file that gives you the option to change power plan at a certain time of night/day. I only started coding this about 20 minutes ago so I apologize for the inconsistencies with capitalisation etc.
It should give the option to not to change the plan, which it does. However, when I run it, I find that the powerplan is not changed. I put the pauses and the Echos for Day and Night to give me an idea of what was going wrong and I was given the echos and pauses, but they seemed to be ignoring the goto commands at the end of each Powercfg.exe /Getactivescheme Line. It just goes onto the next line, until it hits the :no and exits.

Code: Select all

@echo off

SET /P ANSWER=Would you like to change Power Plan Now?
Echo You Chose: %ANSWER%
if /i {%ANSWER%}=={y} (goto :yes)
if /i {%ANSWER%}=={n} (goto :no)
goto :no
:yes
Echo You chose Yes!
if /i {%powercfg.exe /GETACTIVESCHEME%}=={Power Scheme GUID: 4c99af8d-43ab-4fff-b305-0aa164740418 (Daytime)} (goto :night)
Echo Daytime
pause
if /i {%powercfg.exe /GETACTIVESCHEME%}=={Power Scheme GUID: 83fb7ed3-151e-4578-9390-668114991c44 (Night)} (goto :day)
Echo Night
Pause
:no
echo You Pressed No!
Pause
Exit /b 0
:night
powercfg.exe /setactive 4c99af8d-43ab-4fff-b305-0aa164740418
pause
Exit /b 0
:day
powercfg.exe /setactive 83fb7ed3-151e-4578-9390-668114991c44
pause
Exit /b 0


Can anyone see any obvious flaws in this (despite the lack of a time check to change the plan, which I was going to put in after I had checked it all worked so far).

Re: Power Plan Changer ignores the goto commands

Posted: 08 Aug 2011 15:45
by Ed Dyreen
'
Obvious error:

Code: Select all

if /i {%powercfg.exe /GETACTIVESCHEME%}
You did not intend this as a variable did you :)

Next method enumerates output from exec, can be used to do variable comparison.

Code: Select all

> "result.TMP" powercfg.exe /GETACTIVESCHEME
for /f "usebackq tokens=*" %%! in ( "result.TMP" ) do echo.$token=%%!_
Additionally try to avoid the goto command, it is banished from modern languages because it provokes bad programming behavior.
In modern languages goto is replaced with "while, do until", in DOS we use "call :label" or "call batchfile.CMD"

Re: Power Plan Changer ignores the goto commands

Posted: 09 Aug 2011 02:08
by Jamisicus6
Thank you muchly Ed. I'm fairly new to programming with DOS (I have some experience programming with The Elder Scrolls scripting language (which has some similarities with DOS, but not many)) which explains how I messed that bit up :shock:.