Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SCHTASKS, Variables, & Escaping

Status
Not open for further replies.

DJX995

IS-IT--Management
Jan 12, 2009
20
US
I working on a small script here and I'm having trouble getting it to escape correctly.
Here's what I'm dealing with:
Code:
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...
	ECHO:
	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:
Code:
-c \"%strShutdownComment%\"
I can't seem to get it to escape correctly.
Any suggestions?

 
Have another look at where you put your double quotes.
%strShutdownComment%\ is not enclosed.
" -f" is and
"%strShutdownPath%%strShutdownProgram% -s -t 30 -c \" is.


Cogito eggo sum – I think, therefore I am a waffle.
 
Fixed:

Code:
SETLOCAL ENABLEDELAYEDEXPANSION

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 Task Scheduler Found, Executing Command...
	ECHO:
	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"
	EXIT
) ELSE (
	ECHO Task Scheduler NOT Found, Ending Script...
	EXIT
)

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top