none
quoted arguments containing spaces are split, how to prevent it?

    Question

  • Hi,

    im trying to supply multiple filenames to a powershell script but they get split at the spaces even tho they are quoted.

    C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe "X:\abc.ps1" "Y:\my dir\1.tmp" "Y:\your dir\2.tmp"

    unfortunately i get 4 args:
    Y:\my
    dir\1.tmp
    Y:\your
    dir\2.tmp

    i've searched and the only thing that worked was using single-quotes
    C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe "X:\abc.ps1" 'Y:\my dir\1.tmp' 'Y:\your dir\2.tmp'

    but it is another application that starts the powershell and it has the double-quotes around the filenames fixed so it will always be

    .... "Y:\my dir\1.tmp" "Y:\your dir\2.tmp"

    is there a way to make it work with these quotes?

    Thanks

    Saturday, December 24, 2011 9:44 PM

Answers

  • Hello Huajun,

    unfortunately its not the perfect solution as there might be 1 to 9 arguments passed depending on how many files i have selected. Therefore i have beed using "%FILEPATHS%" which would only use single-quotes.

    But using...
    "args.ps1" "'%FILEPATH1%'" "'%FILEPATH2%'" "'%FILEPATH3%'" "'%FILEPATH4%'" "'%FILEPATH5%'" "'%FILEPATH6%'" "'%FILEPATH7%'" "'%FILEPATH8%'" "'%FILEPATH9%'"
    ...it can work, i just need to filter when the argument is only " or empty.

    Thanks

    passing filepaths as arguments should be quite a standard task and its sad how you have to work around that.


    Hello ThorstenK,

    What about this one? You still need to use -join operator.

    Workaround is necessary, if we find something doesn't work.

    Best Regards,
    Huajun Gu

    Code in script:

    $newArgs = "`"$($args -join '" "')`""
    $newArgs

     


    After I watched the video "Richard St. John's 8 secrets of success" on TED.com, I learned this: "Being good at your job is not enough, you should be damn good at it."
    Wednesday, December 28, 2011 12:58 AM

All replies

  • So can you clarify?  You're saying you have another app that needs to write the command?  Can you have another script that then edits the script to rewrite it?
    Saturday, December 24, 2011 11:05 PM
    Moderator
  • Hi Marco and thanks

    i'm using FileMenu Tools from http://www.lopesoft.com/en/fmtools/info.html to create a custom context menu in the windows explorer.

    It passes "file1" "file2" ... as parameters and they can contain spaces

    making the executed command:
    C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe "X:\abc.ps1" "file1" "file2"


    i just noticed that doing the same manually in a already running PowerShell session it works.

    PS C:\>  ."X:\abc.ps1" "Y:\my dir\1.tmp" "Y:\your dir\2.tmp"
    then i get the 2 args i want:
    Y:\my dir\1.tmp
    Y:\your dir\2.tmp

    • Edited by ThorstenK Sunday, December 25, 2011 10:17 PM
    Sunday, December 25, 2011 8:08 PM
  • So that's the thing...  You're not doing the same.  Passing stuff via powershell.exe is not the same as once you're running commands in the console.  Have you consulted the vendor?
    Monday, December 26, 2011 1:04 AM
    Moderator
  • this is a pure Windows+PowerShell issue no matter if i execute that in a cmd.exe or with another program.

    easy to reproduce with a single-line ps1

    ---E:\temp\args.ps1 content:
    $args

    in a cmd.exe executing the following:

    C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe "E:\temp\ar
    gs.ps1" "Y:\my dir\1.tmp" "Y:\your dir\2.tmp"
    result = 4 args
    Y:\my
    dir\1.tmp
    Y:\your
    dir\2.tmp

    it seems as if the quotes are stripped when calling powershell.exe and therefore the arguments are split. this is the issue and i'd like to find a way in keeping the quotes arguments.

     

    Now in PowerShell:

    PS C:\> ."E:\temp\args.ps1" "Y:\my dir\1.tmp" "Y:\your dir\2.tmp"
    result = 2 args
    Y:\my dir\1.tmp
    Y:\your dir\2.tmp

    as it is already within powershell the quotes stay and preserve the arguments

    Monday, December 26, 2011 11:19 AM
  • As variant you may using both quotes.

    powershell.exe -noprofile "E:\temp\args.ps1" "'Y:\my dir\1.tmp'" "'Y:\your dir\2.tmp'"

    Monday, December 26, 2011 1:38 PM
  • hi kazun,

    unfortunately i can't, as i wrote the arguments are appended by another app in fixed format.

    ->so it will always be
    .... "Y:\my dir\1.tmp" "Y:\your dir\2.tmp"

    hopefully i dont need a wrapper .cmd script to insert the doublequotes before calling the ps1

    • Edited by ThorstenK Monday, December 26, 2011 6:59 PM
    Monday, December 26, 2011 6:04 PM
  • maybe try to escape the other set?
     
    "`"hello`""
     

    Justin Rich
    http://jrich523.wordpress.com
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Tuesday, December 27, 2011 1:35 PM
  • Hello ThorstenK,

    I downloaded the software which you mentioned, and I tried to accomplish your goal. Here is my test result, is it okay for you?

    Best Regards,
    Huajun Gu

     

     


    After I watched the video "Richard St. John's 8 secrets of success" on TED.com, I learned this: "Being good at your job is not enough, you should be damn good at it."
    Tuesday, December 27, 2011 1:49 PM
  • Hello Huajun,

    unfortunately its not the perfect solution as there might be 1 to 9 arguments passed depending on how many files i have selected. Therefore i have beed using "%FILEPATHS%" which would only use single-quotes.

    But using...
    "args.ps1" "'%FILEPATH1%'" "'%FILEPATH2%'" "'%FILEPATH3%'" "'%FILEPATH4%'" "'%FILEPATH5%'" "'%FILEPATH6%'" "'%FILEPATH7%'" "'%FILEPATH8%'" "'%FILEPATH9%'"
    ...it can work, i just need to filter when the argument is only " or empty.

    Thanks

    passing filepaths as arguments should be quite a standard task and its sad how you have to work around that.


    • Edited by ThorstenK Tuesday, December 27, 2011 8:47 PM
    Tuesday, December 27, 2011 8:46 PM
  • What happens if you do this:

    C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -file "E:\temp\ar
    gs.ps1" "Y:\my dir\1.tmp" "Y:\your dir\2.tmp"

    Tuesday, December 27, 2011 9:35 PM
  • Hello Huajun,

    unfortunately its not the perfect solution as there might be 1 to 9 arguments passed depending on how many files i have selected. Therefore i have beed using "%FILEPATHS%" which would only use single-quotes.

    But using...
    "args.ps1" "'%FILEPATH1%'" "'%FILEPATH2%'" "'%FILEPATH3%'" "'%FILEPATH4%'" "'%FILEPATH5%'" "'%FILEPATH6%'" "'%FILEPATH7%'" "'%FILEPATH8%'" "'%FILEPATH9%'"
    ...it can work, i just need to filter when the argument is only " or empty.

    Thanks

    passing filepaths as arguments should be quite a standard task and its sad how you have to work around that.


    Hello ThorstenK,

    What about this one? You still need to use -join operator.

    Workaround is necessary, if we find something doesn't work.

    Best Regards,
    Huajun Gu

    Code in script:

    $newArgs = "`"$($args -join '" "')`""
    $newArgs

     


    After I watched the video "Richard St. John's 8 secrets of success" on TED.com, I learned this: "Being good at your job is not enough, you should be damn good at it."
    Wednesday, December 28, 2011 12:58 AM
  • That is the solution I use for joining arguments send by FileMenu tools, in the called script:

    set arg=%1
    :ArgJoinStart
        shift
        if "%1"=="" goto ArgJoinEnd
        set "arg=%arg% %1"
    goto ArgJoinStart
    :ArgJoinEnd

    echo "%arg%"

    Sunday, January 12, 2014 10:49 PM