locked
How to pass file names with spaces is parameters? RRS feed

  • Question

  • I must have read 20 threads on handling spaces in file names, and still do not have a solution.  I've used ampersands, backticks, combinations of double and single quotes, and still can't get the simple script shown below to work with paths/filenames which have spaces.  The PS script below calls the ImageMagick "convert" command.  But when I call the script like this:

    IM-append.ps1 C:\Users\bernie\Photo Staging\file1.jpg, C:\Users\bernie\Photo Staging\file2.jpg

    It chokes on the space in the "Photo Staging" directory.  Note that the file names need to be comma separated.  How can I pass these file names/paths with spaces?

    IM-append.ps1:

    param(
    [  string[]]$fnames
    )

    convert $fnames -append merged-vertical.jpg

    Thursday, February 2, 2017 3:13 AM

Answers

  • Easy ...

    IM-append.ps1 'C:\Users\bernie\Photo Staging\file1.jpg', 'C:\Users\bernie\Photo Staging\file2.jpg'


    \_(ツ)_/

    Thursday, February 2, 2017 4:32 AM

All replies

  • Easy ...

    IM-append.ps1 'C:\Users\bernie\Photo Staging\file1.jpg', 'C:\Users\bernie\Photo Staging\file2.jpg'


    \_(ツ)_/

    Thursday, February 2, 2017 4:32 AM
  • you can do it as JRV suggested or alternate method is to backtick before every space in folderpath

    PS C:\Users> get-item "H:\Sachin\My` PST","H:\Sachin\New` folder"

    Kindly check and mark this reply as answer if helpful

    • Proposed as answer by SachinWaghmare Thursday, February 2, 2017 5:05 AM
    Thursday, February 2, 2017 5:04 AM
  • you can do it as JRV suggested or alternate method is to backtick before every space in folderpath

    PS C:\Users> get-item "H:\Sachin\My` PST","H:\Sachin\New` folder"

    Kindly check and mark this reply as answer if helpful

    Absolutely unnecessary when a string is quoted.  A space escapes  only to a space.

    D:\scripts> function test{param([string[]]$files)$files}
    D:\scripts> test 'xx x','yyy zzz'
    xx x
    yyy zzz

    In unquoted string the escape is useful but quoting the strings is safer.


    \_(ツ)_/



    • Edited by jrv Thursday, February 2, 2017 5:14 AM
    Thursday, February 2, 2017 5:08 AM
  • Thanks, I found out after I posted the message that enclosing each file name in double quotes works too.
    Friday, February 3, 2017 2:49 AM
  • Thanks, I found out after I posted the message that enclosing each file name in double quotes works too.

    Learn to not use double quotes unless you know they are required. Use single quote in this case and in most cases.

    \_(ツ)_/

    Friday, February 3, 2017 3:00 AM