GettingStarted
GettingStarted

Reputation: 7625

Please explain this bat file

For /f "skip=2 tokens=2,4 delims=:()" %%G in ('powercfg -list') do (
 Powercfg -setAcValueIndex %%G sub_buttons sButtonAction 0
 if "%%H" == " *" Powercfg -setActive %%G
 )

Can someone please explain this to me?

I know it's changing the powercfg for a Windows PC. but dont know what it means.

Upvotes: 0

Views: 687

Answers (1)

Thomas Dickey
Thomas Dickey

Reputation: 54603

The tokens=2,4 option of the for command assigns the variables %G, %H. Quoting from the documentation:

tokens=x,y,m-n
Specifies which tokens from each line are to be passed to the for body for each iteration. As a result, additional variable names are allocated. The m-n form is a range, specifying the mth through the nth tokens. If the last character in the tokens= string is an asterisk (*), an additional variable is allocated and receives the remaining text on the line after the last token that is parsed.

The script reads a report from powercfg and looks for certain information, sets one of the profiles to active. As suggested, taking it apart step-by-step would be a good exercise.

Here's a picture showing someone's power configuration:

enter image description here

By using colon and parentheses for delimiters, the parser ignores those (treats them as a gap), so the script would pick (allowing for column-counts...) the one ending with * ("High performance" in the picture). Your picture may differ, of course.

Further reading:

  • for - Runs a specified command for each file in a set of files.
  • powercfg - control power settings and configure computers to default to Hibernate or Standby modes

Upvotes: 1

Related Questions