It is currently April 19th, 2024, 11:51 am

Question about RunCommand

Get help with creating, editing & fixing problems with skins
Wim
Posts: 69
Joined: September 22nd, 2010, 8:30 pm

Question about RunCommand

Post by Wim »

A question about RunCommand:

If i run a command from the CommandPrompt (cmd.exe):

Code: Select all

set %_month=%date:~6,2% && echo %_month% > sometestfile.txt
it will write the correct value to the textfile:

Code: Select all

02
If i run this same code from RunCommand:

Code: Select all

Parameter=set %_month=%date:~6,2% && echo %_month% > sometestfile.txt
it will write simply the name of the variable to the textfile:

Code: Select all

%_month%
What am i doing wrong here?

Note: Date is the function in cmd.exe that returns the date in "local" format.

Code: Select all

%date:~6,2%
gives the number of the current month.
User avatar
Brian
Developer
Posts: 2679
Joined: November 24th, 2011, 1:42 am
Location: Utah

Re: Question about RunCommand

Post by Brian »

This type of problem happens a lot when writing batch scripts. It's called delayed expansion.

Basically the command processor replaces environment variables before the command is executed. Since you are setting a variable with the first command, the second command still uses the "initial" value of the variable (which didn't exist when executed). Since you are combining both commands into one, the use of the variable set by the first command isn't being used in the second command.

When you open the command prompt (cmd.exe), I am betting that it is enabling delayed expansion by default, which is why it works.


There are 3 ways to fix this depending on your overall goals.
  1. The easiest solution would be to just utilize the OutputFile option and forget about setting any temporary environment variable.

    Code: Select all

    Parameter=echo %date:~6,2%
    OutputFile=sometestfile.txt
    
    This also has the advantage of using the output of the command as the string value of the measure so you can display it in your skins.
    Space
  2. Use the call command. Normally this command is used to call other batch scripts, but you can use it to call a "subroutine". This might not work for other cases, so this method isn't fully recommended.

    Code: Select all

    Parameter=set _month=%date:~6,2% && call echo %_month% > sometestfile.txt
    
  3. The proper solution is actually to enable delayed expansion as a command line switch to the cmd process - and subsequently to use the delayed expansion syntax ( ! instead of % for variables).

    Code: Select all

    Program=""%ComSpec%" /U /V:ON /C"
    Parameter=set _month=%date:~6,2% && echo !_month! > sometestfile.txt
    
    Note the use of the /V:ON command line switch, and also the ! for variables. Also, note the order of the switches, the /C needs to be last.

    This is also a good time to mention the quotes on the Program option. Rainmeter strips the leading and trailing quotes, so we need to make sure we surround the program path properly.


In batch files, you would just use the SetLocal EnableDelayedExpansion command.

-Brian
Wim
Posts: 69
Joined: September 22nd, 2010, 8:30 pm

Re: Question about RunCommand

Post by Wim »

Thanks Brian. This really is a great help

Thanks again!

-Wim