SCHTASKS, Variables, & Escaping

October 9, 2009 at 11:31:42
Specs: N/A
I working on a small script here and I'm having trouble getting it to escape correctly.Here's what I'm dealing with:

SET strShutdownPath=%SystemRoot%\System32\
SET strShutdownProgram=shutdown.exe
SET strShutdownComment=A shutdown has been initiated. The system will shutdown in thirty (30) seconds.
IF EXIST "%SystemRoot%\System32\schtasks.exe" (
ECHO Program Found, Executing Command...
SCHTASKS /Create /RU "SYSTEM" /SC DAILY /TN "Computer Shutdown" /TR "%strShutdownPath%%strShutdownProgram% -s -t 30 -c \"%strShutdownComment%\" -f" /ST "23:00:00" /SD "01/01/2001"
) ELSE (
ECHO Program NOT Found, Ending Script...

The part I'm having escaping issues with is the shutdown comment:

-c \"%strShutdownComment%\"

I can't seem to get it to escape correctly.
Any suggestions?

See More: SCHTASKS, Variables, & Escaping

Report •

October 10, 2009 at 11:49:29
Before I actually answer your question, I just want to say I love how you prepend every variable name with "str." It's like you don't realize strings are the only variables in CMD. :3

Two options:
1 - Change strShutdownComment to not have parentheses
2 - Add setlocal ENABLEDELAYEDEXPANSION to the start of your script, and change %strShutdownComment% to !strShutdownComment!

Report •

October 10, 2009 at 15:54:13
The "DelayedExpansion" is what I was looking for.

What you don't realize is that I very frequently switch from VBScript to MS-BASIC (and vice-versa). So the reason I have the prefix "str" is because the last time I wrote a script, it was in VBScript.

Actually, I began writing this in VBScript but I found out that there are so many limitations when creating Scheduled Tasks via WMI, so I just moved what I had to MS-BASIC.

Doesn't hurt anyway.

Report •

October 10, 2009 at 16:08:31
Batch Script, you mean? Unless you were talking about the predecessor of Visual Basic? (I also dislike the practice in VBScript, but at least it does serve a purpose. VBS only works in Variants.)

Report •

Related Solutions

Ask Question