# Solved: powercfg - missing operand? huh



## scrfix (May 3, 2009)

```
@echo off
For /F "Tokens=*" %%A In ('powercfg -getactivescheme^|Findstr /I /C:": "') Do Set /A _ActiveScheme=%%B
powercfg -SETACVALUEINDEX %_ActiveScheme% SUB_NONE CONSOLELOCK 0
powercfg -SETDCVALUEINDEX %_ActiveScheme% SUB_NONE CONSOLELOCK 0
```
The code:

```
'powercfg -getactivescheme
```
returns to me: Power Scheme GUID: 49ef8fc0-bb7f-488e-b6a0-f1fc77ec649b (Dell Recommended)

This will be different for every computer. All I want is the: *49ef8fc0-bb7f-488e-b6a0-f1fc77ec649b*

I thought the above code says to run the command powercfg -getactivescheme, look at everything that is returned to me and then find the string ": " within what is returned and then take the item directly after that and store it into a variable which would be the above however it returns back to me missing operand. Huh?

I am going to try the skip option Jerry showed me yesterday but am I overthinking this?
Am I writing this incorrectly?
Why is this not working?

Thanks,


----------



## TheOutcaste (Aug 8, 2007)

*Tokens=** means it will only get one token, *%%A*. This will be the entire line as you've not specified any delimiters.
There will never be a *%%B* value with these options.
Also, the */A* switch for Set means Set will deal with the data as a number. If it's a text string, the variable will be set to 0.
A GUID is a Hexadecimal number, and Hex numbers must start with 0x or you'll get an error that it's not a valid number.
If it's mixed text and numbers, and has any math symbols, you can get unexpected results, or errors like missing operand or divide by zero.
In this case, since *%%B* doesn't exist, the variable is not defined.

Use this instead:

```
For /F "Tokens=4 Delims=: " %%A In ('powercfg -getactivescheme') Do Set _ActiveScheme=%%A
```
The *powercfg -getactivescheme* command only generates one line of output, so *Findstr* isn't needed to select one line out of many.
This uses space and *:* as delimiters, so the line will be parsed like this (I added the equals sign, it's not part of the output):


=Power
=Scheme
=GUID
=49ef8fc0-bb7f-488e-b6a0-f1fc77ec649b
=(Dell
=Recommended)
You want # 4, so specify Tokens=4
If you just use *:* as a delimiter, you'll get this:


=Power Scheme GUID
= 49ef8fc0-bb7f-488e-b6a0-f1fc77ec649b (Dell Recommended)
Note the leading space on the GUID. This might cause problems depending on what you are doing with the value, plus you have to remove the text after the GUID.

EDIT: Correction, using just *:* parses the line into just two parts, not three

Jerry


----------



## Squashman (Apr 4, 2003)

I see that is a Vista only option.
What are you using this batch file for?


----------



## scrfix (May 3, 2009)

Jerry,

Utilizing that batch, it says : was unexpected at this time.

Squashman,

I am utilizing it to turn on or off the password option for that screen.
I already know how t o perform this in XP. In Vista however I need the active profile name in order to accomplish this task.


----------



## TheOutcaste (Aug 8, 2007)

Opps, I swapped the order of the delims when I typed it. That's what I get for not copying and pasting. If you want a space as a delimiter, it has to be last, so that should be "Tokens=4 Delims=: ". I'll correct it above.


----------



## scrfix (May 3, 2009)

I actually took out the Delims= :" and it worked perfectly. I found that out while my wife was driving so this is the first chance I have had to report it.

The code below works perfectly without spaces.

To anybody utilizing this code. The below is ONLY compatible with Vista and possibly Windows 7. It has not been tested with Windows 7 but since that is just an upgrade to Vista I am sure it will work.


```
For /F "Tokens=4 Delims= " %%A In ('powercfg -getactivescheme') Do Set _ActiveScheme=%%A
```


----------



## TheOutcaste (Aug 8, 2007)

True. Without the colon as a delimiter, it becomes part of the GUID token. Removing the Delims= part completely means it will use the defaults of tab and space, so you can leave it out or specify the space. Some commands do use a tab in their output (at least they used to) so that can sometimes affect how the line is parsed. Can't tell from the Command Prompt if a tab was used.


----------



## scrfix (May 3, 2009)

That's a good point.

I can set powercfg -getactivescheme>power.txt or something to the similar and check it that way.

They are all spaces, at least in Vista. I have not tested Win 7.


----------



## scrfix (May 3, 2009)

Marked this unsolved only because I have another question on this. I am utilizing an IF statement to tell what OS I am on, Vista or XP. The FOR statement I would like to put within the IF statement however it keeps telling me invalid parameters.

The code for turning controlling the item below is different on both XP and Vista.

The code with all of the extras removed not to confuse the situation here looks something to the following:


```
If %OS% == Vista (
For /F "Tokens=4 Delims= " %%A In ('powercfg -getactivescheme') Do Set _ActiveScheme=%%A
powercfg -SETACVALUEINDEX %_ActiveScheme% SUB_NONE CONSOLELOCK 0
powercfg -SETDCVALUEINDEX %_ActiveScheme% SUB_NONE CONSOLELOCK 0
Goto _Wherever
)
```
The vista code is giving me the errors because of the FOR statement. Is there something that I need to add or is it a physical impossiblity that a FOR statment can go within an IF statment?

Thanks,


----------



## scrfix (May 3, 2009)

I figured out a different way of accomplishing this. I put the FOR statement under a different label and then just sent it there. After the IF statement I put a second label skipping over the FOR statement in case the IF statement was false.

This works.


----------



## TheOutcaste (Aug 8, 2007)

You need to use Delayed Expansion. Remember, the If statement is one long line, so when variables are expanded for the line, _ActiveScheme has not yet been set by the For loop.
Your powercfg command is missing the _ActiveScheme parameter because of that, so the command becomes this:



```
powercfg -SETACVALUEINDEX    SUB_NONE CONSOLELOCK 0
```
This will work:

```
SetLocal EnableDelayedExpansion
If %OS% == Vista (
For /F "Tokens=4 Delims= " %%A In ('powercfg -getactivescheme') Do Set _ActiveScheme=%%A
powercfg -SETACVALUEINDEX !_ActiveScheme! SUB_NONE CONSOLELOCK 0
powercfg -SETDCVALUEINDEX !_ActiveScheme! SUB_NONE CONSOLELOCK 0
Goto _Wherever
)
```


----------

