Search code examples
batch-filexcopywindows-task-scheduler

SchTask with XCOPY command


I am creating an installation script for our web panel software, but I don't do too many batch scripts. I need to keep this all in the same file (can't seperate it out) so how exactly can I do this?

SchTasks /Create /SC DAILY /TN "%ip%%port%-PB" /TR "XCOPY /D /E /C /R /K /Y "%userfilespath%\pb\*.*" "C:\PBScreens\%ip%%port%\"" /ST 00:00

I am assuming I can't put the source and target directories in quotation marks since the /TR switch is in quotations. Can I put it in '' or how do I do this?

Thanks!


Solution

  • The problem is with quotes and spaces in the /TR (taskrun) parameter of schtasks.

    You need to enclose the complete taskrun parameter in quotes and escape all the quotes inside it with backslashes (\).

    Carefully test this

    set taskname="%ip%-%port%-PB"
    set taskrun="XCOPY /D /E /C /R /K /Y \"%userpath%\pb\*.*\" \"%destpath%\%ip%%port%\\"" 
    SchTasks /Create /SC DAILY /TN %taskname% /TR %taskrun% /ST 00:00:00