How to pass an argument to a Windows Scheduled Task with spaces in it

182,700

Solution 1

schtasks.exe /create /SC WEEKLY /D SUN /SD 11/12/2015 /ST 12:00:00 /TN "taskname" /TR "'c:\program files(x86)\task.exe' Arguments"

Note the use of ' in the path of a file to be run.

Solution 2

I've worked with scheduled tasks and you generally put the arguments in its own text input box. This means that you point the action to the program/script field points to the exe and the "Add Arguments" field should have all of the parameters. (source)

Blog image

I believe this behavior was added to prevent spaces in the file path to the exe causing problems.

I do this all of the time with PowerShell scripts. Here is an example:

  • Program/script: powershell.exe
  • Add arguments: -command "& 'C:\HSD - Copy\logoffstudents.ps1' " -NonInteractive
  • Start in: Blank

Solution 3

In this case, you could work around the problem by passing your path parameter in 8.3 format.

You can discover the 8.3 format for your path by opening a command prompt and issuing the command dir /x in the root of your drive.

You should see an entry similar to

11/04/2011  12:10    <DIR>          PROGRA~1     Program Files

for your Program Files directory.

Then change directory to Program Files with cd "Program Files" followed by cd xyz and issue dir /x again to find the 8.3 format name for "The Interface", and so on.

Your final path for the example you gave would look something like:

C:\PROGRA~1\XYZ\THEINT~1\FOLDER~1

Solution 4

One way you could accomplish this is using powershell from the command line.

Add this code to a file called MyModule.psm1.

$TASK_STATE_UNKNOWN   = 0;
$TASK_STATE_DISABLED  = 1;
$TASK_STATE_QUEUED    = 2;
$TASK_STATE_READY     = 3;
$TASK_STATE_RUNNING   = 4;
Function Run-Task(
        [ValidateNotNullOrEmpty()][string]
        [Parameter(Mandatory=$true, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]
        $ComputerName, 
        [ValidateNotNullOrEmpty()][string]
        [Parameter(Mandatory=$true, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]
        $Foldername, 
        [ValidateNotNullOrEmpty()][string]
        [Parameter(Mandatory=$true, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]
        $Taskname, 
        [int] $maxwait = 0, 
        [string[]]
        [Parameter(Mandatory=$false, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]
        $TaskParameters = $null
    ){
    $TaskScheduler = New-Object -ComObject Schedule.Service
    $TaskScheduler.Connect($ComputerName)
    $ScheduledTaskFolder = $TaskScheduler.GetFolder($Foldername)
    $ScheduledTask = $ScheduledTaskFolder.GetTask($TaskName)

    if(-not $ScheduledTask) {
        return $Null
    }

    $ScheduledTask.Enabled = $True
    $ScheduledTask.Run($TaskParameters)

    if($maxwait -gt 0){
        $seconds = 5
        $i = 0;
        Start-Sleep -Seconds $seconds
        while ($ScheduledTask.State -eq $TASK_STATE_RUNNING)
        {
            if(($i * $seconds) -gt $maxwait) { 
                break; 
            } 
            Start-Sleep -Seconds $seconds        
            $i++;
        }
    }
    return $ScheduledTask
}

Export-ModuleMember -Variable "TASK_STATE*"
Export-ModuleMember -Function "Run-*"

Then from the command line OR a ps1 file you could run:

Import-Module $(Get-Item .\MyModule.psm1 | Resolve-Path -Relative) -DisableNameChecking -Force

$task = Run-Task -ComputerName "$env:COMPUTERNAME" -Taskname "Foo" -Foldername "\" -TaskParameters "test", "Tim C", $(Get-Date -format G)

Each respective item in the taskparameters array would get passed in as $(Arg0), $(Arg1), and $(Arg2).

Solution 5

I had a similar problem with VLC, which I was using on Windows XP. The trick is to enclose the argument of the cmd command in double quotes.

Here is an example of what I used (scheduling a recording at 15:00):

at 15:00 cmd /c ""C:\Programmi\VideoLAN\VLC\vlc.exe dvb-t://frequency=698000000 :program=4006 :run-time=5 --sout "C:\Documents and Settings\UserName\Documents\Video\VLC\test.mpg"""

Note the use of double quotes just after /c and at the end of the command (after .mpg). The argument with spaces in this case is "C:\Documents and Settings\..."

Share:
182,700

Related videos on Youtube

Rodney
Author by

Rodney

Updated on September 18, 2022

Comments

  • Rodney
    Rodney almost 2 years

    I need to set up a Windows Scheduled Task. It accepts 1 parameter/argument which is a path and can contain spaces. My Scheduled task does not work - it "breaks" the parameter up at the first space.

    If I run it in the Command Prompt I can just wrap the argument in " " and it works fine, however, this does not work in the Scheduled Task UI.

    e.g. C:\Program Files\xyz\FTP File Transfer\FTPFileTransferTask.exe "C:\Program Files\xyz\The Interface\Folder Path"

    I have tried wrapping the argument with " " ' ' [ ] () and have tried filling in the spaces with %20, ~1 etc. with no luck.

    I know of one solution to make a bat file and use " " around my argument but I don't want to add more complexity.

    I tried it on Windows 7 and Windows 2008 Server and both failed. There seems to be no discussions on this?

    • William Jackson
      William Jackson about 13 years
      Are you putting the argument in the Program/script section or the Add arguments (optional) section when you edit the Scheduled Task?
    • ajax992
      ajax992 about 13 years
      It would be helpful if you specified which program you're using exactly, as the correct wrapping of arguments is at the discretion of the program and not Scheduled Taks. WinSCP, for example, expects double quotes (""..."") when you have to nest quotes.
    • kreemoweet
      kreemoweet over 12 years
      It's pretty unclear as to 1) what is failing, the task or your .exe, and 2)exactly what you entered and where in the TaskSched UI. Could it be that where TaskSched asks for a command (full path to executable), you are trying to give it a command-line (very different thing)?
    • tumchaaditya
      tumchaaditya over 10 years
      Why against batch file? It makes things so simple! Or you can shoot for powershell script if you are feeling adventurous..
  • Rodney
    Rodney about 13 years
    ps - So is this is bug in the Windows Scheduled Task app? Spaces are very common!
  • P. A. Monsaille
    P. A. Monsaille about 13 years
    A quick test on windows 7 works for me. Can you walk us through the steps you took to set up the task, as there as various ways. Thanks for the edit there Gareth, looks much nicer.
  • Rodney
    Rodney about 13 years
    So the task runs ok with this formatting, but then my .NET program (which accepts the path as an arg string) does not uncompress the path from the 8.3 format. So perhaps it is a programming question - how to handle 8.3 paths?
  • Rodney
    Rodney about 13 years
    Thanks, but the problem is that one of my paramaters IS a file path (and has a space in it). So in your example 100 will work, but what if you wanted to pass "C:\Start Folder"?
  • Doltknuckle
    Doltknuckle about 13 years
    I just use quotes in my programs and it works. The ampersand is only required with powershell. This symbol is the CALL operator and allows me to bring up a powershell command. In most cases, quotes is all you need. At this point, you might want to look into contacting the creator of the exe to see if they support scheduled tasks. I have run into a few rare programs that just refuse to run as a scheduled task. I think there are subtle differences on how the parameters are passed that can cause problems. Sorry I can't help more.
  • Rodney
    Rodney about 13 years
    Thanks Doltknuckle - I know I could also do it with a .bat file (and use "" around the param (like you do in the Powershell script. I am sure it is a bug in the Windows Task editor UI... (I am the creator of the .exe ;) - It works fine through a test harness and from the command prompt but not through the Windows UI...
  • Doltknuckle
    Doltknuckle about 13 years
    If you made the exe, this may be a question for stackoverflow. I have a feeling that you may need to modify your parameter handling when this exe is used with scheduled task. One suggestion is to have your exe log the parameters received to a file so you can see what is being passed. It would at least allow you to see if the scheduled task parameters are the same as the command line parameters.
  • Chibueze Opata
    Chibueze Opata about 11 years
    I know this us old, but did you try hyphen (-) ?
  • carlin.scott
    carlin.scott over 7 years
    I managed to gleam the solution from reading this answer but Aman Mehra's answer is clearer about the solution. Using a single quote inside the double quotes is the solution to the question and the reason it works is that Windows removes the out double quotes when parsing the arguments but leaves the inner single quotes intact for the executable.
  • philmaweb
    philmaweb over 2 years
    It's 2022 and this answer is still very much relevant. Had to try lots of quoting to no avail. Until this answer saved my night. Very much appreciated.