Search code examples
batch-filecmdcommand-linewindows-task-scheduler

How to handle the spaces in a command which is set in the Task Scheduler in batch file?


I am trying to set a task schedule to execute the command which installs the Python packages. However, the path of packages contains some space. I tried many way but could not get a good solution.

The batch file

@echo on
SCHTASKS /delete /tn "test" /f
SCHTASKS /create /tn "test" /sc ONCE /ST 00:00 /RL HIGHEST /tr "cmd.exe /k pip install "C:\test data\Python-Installer\numpy-1.19.5-cp36-cp36m-win32.whl" "C:\test data\Python-Installer\opencv_python-4.5.4.60-cp36-cp36m-win32.whl""
SCHTASKS /run /tn "test"
pause

The result:

C:\Windows\system32>SCHTASKS /delete /tn "test" /f
ERROR: The system cannot find the file specified.

C:\Windows\system32>SCHTASKS /create /tn "test" /sc ONCE /ST 00:00 /RL HIGHEST /tr "cmd.exe /k pip install "C:\test data\Python-Installer\numpy-1.19.5-cp36-cp36m-win32.whl" "C:\test data\Python-Installer\opencv_python-4.5.4.60-cp36-cp36m-win32.whl""
ERROR: Invalid argument/option - 'data\Python-Installer\numpy-1.19.5-cp36-cp36m-win32.whl C:\test'.
Type "SCHTASKS /CREATE /?" for usage.

C:\Windows\system32>SCHTASKS /run /tn "test"
ERROR: The system cannot find the file specified.

C:\Windows\system32>pause
Press any key to continue . . .

Please help me to "kill" the spaces in the code.

Any help is approciated


Solution

  • As a follow up to my comments, here's my interpretation of the documentated quoting methodology, based around the correct cmd.exe parameters and arguments, and your submitted commands.

    Echo On
    %SystemRoot%\System32\schtasks.exe /Delete /TN "test" /F
    %SystemRoot%\System32\schtasks.exe /Create /TN "test" /TR "%SystemRoot%\System32\cmd.exe /D /S /K \"@\"%ProgramFiles(x86)%\Python36-32\Scripts\pip.exe\" install \"C:\test data\Python-Installer\numpy-1.19.5-cp36-cp36m-win32.whl\" \"C:\test data\Python-Installer\opencv_python-4.5.4.60-cp36-cp36m-win32.whl\"\"" /SC ONCE /ST 00:00
    %SystemRoot%\System32\schtasks.exe /Run /TN "test"
    Pause
    

    You will note, that I have deliberately removed the /RL HIGHEST parameter and argument. I see no reason why you would be installing as SYSTEM, and in doing so, having a task with cmd.exe's /K option running in the background.