Recent

Author Topic: Filenames with spaces in them passed as paramters  (Read 7941 times)

Mike.Cornflake

  • Hero Member
  • *****
  • Posts: 1260
Filenames with spaces in them passed as paramters
« on: June 21, 2014, 05:11:23 pm »
How would a command line program under linux expect a filename with spaces in it to be passed.

Under windows, double quotes do the job "<filename>", and the function I've found that does this well is AnsiQuotedStr.

Will this work under Linux?
Lazarus Trunk/FPC Trunk on Windows [7, 10]
  Have you tried searching this forum or the wiki?:   http://wiki.lazarus.freepascal.org/Alternative_Main_Page
  BOOKS! (Free and otherwise): http://wiki.lazarus.freepascal.org/Pascal_and_Lazarus_Books_and_Magazines

Leledumbo

  • Hero Member
  • *****
  • Posts: 8757
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Filenames with spaces in them passed as paramters
« Reply #1 on: June 21, 2014, 05:30:58 pm »
I'd say that's shell specific, but at least bash does it the same.

Mike.Cornflake

  • Hero Member
  • *****
  • Posts: 1260
Re: Filenames with spaces in them passed as paramters
« Reply #2 on: June 21, 2014, 07:03:09 pm »
I'd say that's shell specific, but at least bash does it the same.
Thanks for this.

If it is shell specific, don't suppose we've got a function somewhere that handles that already?  If not, then I'll stick double quoting.  Bash will cover most users nowadays...

I've already confirmed that StrToCmdParam doesn't do what I think it would...
Lazarus Trunk/FPC Trunk on Windows [7, 10]
  Have you tried searching this forum or the wiki?:   http://wiki.lazarus.freepascal.org/Alternative_Main_Page
  BOOKS! (Free and otherwise): http://wiki.lazarus.freepascal.org/Pascal_and_Lazarus_Books_and_Magazines

hinst

  • Sr. Member
  • ****
  • Posts: 303
Re: Filenames with spaces in them passed as paramters
« Reply #3 on: June 21, 2014, 08:34:23 pm »
How would a command line program under linux expect a filename with spaces in it to be passed.
Unsurprisingly, it should expect it surrounded by double quotes
But it depends on the program, of course. Most command line utilities understand this syntax, but it is possible that some old _or_ rare _or_ no longer maintained programs can have problems with file paths containing spaces
If you want to pass a file path to program, then just put double quotes around it. It is not necessary for filepaths without spaces, but it does not hurt either
Too late to escape fate

Mike.Cornflake

  • Hero Member
  • *****
  • Posts: 1260
Re: Filenames with spaces in them passed as paramters
« Reply #4 on: June 21, 2014, 09:17:14 pm »
Unsurprisingly, it should expect it surrounded by double quotes
...
It is not necessary for filepaths without spaces, but it does not hurt either
Many thanks hinst, appreciate it.

AnsiQuotedStr already takes care of not wrapping the name with " if no spaces, so that's good.
Lazarus Trunk/FPC Trunk on Windows [7, 10]
  Have you tried searching this forum or the wiki?:   http://wiki.lazarus.freepascal.org/Alternative_Main_Page
  BOOKS! (Free and otherwise): http://wiki.lazarus.freepascal.org/Pascal_and_Lazarus_Books_and_Magazines

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11447
  • FPC developer.
Re: Filenames with spaces in them passed as paramters
« Reply #5 on: June 23, 2014, 05:25:18 pm »
Quotes group arguments in bash, not filenames. If filenames are a separate argument  (like in -o "filename"), then it doesn't matter

A common exception is if you parameters of the form -ofilename.ext.  On Windows then you do -o"filename with space" while on *nix you write "-ofilename with space"
« Last Edit: June 23, 2014, 07:12:03 pm by marcov »

Mike.Cornflake

  • Hero Member
  • *****
  • Posts: 1260
Re: Filenames with spaces in them passed as paramters
« Reply #6 on: June 23, 2014, 05:29:36 pm »
Quote
On Windows then you do -o"filename with space" while on *nix you write "-o filename with space"

Good to know.  My particular use case is mplayer, and for that the filename is a separate argument.  I've now had a chance to test on Linux, and can confirm just double quotes around the filename for mplayer worked as expected.

Many thanks.
Lazarus Trunk/FPC Trunk on Windows [7, 10]
  Have you tried searching this forum or the wiki?:   http://wiki.lazarus.freepascal.org/Alternative_Main_Page
  BOOKS! (Free and otherwise): http://wiki.lazarus.freepascal.org/Pascal_and_Lazarus_Books_and_Magazines

bylaardt

  • Sr. Member
  • ****
  • Posts: 309
Re: Filenames with spaces in them passed as paramters
« Reply #7 on: July 05, 2014, 08:52:19 pm »
sometimes you must (or can) use parameters with the escape char "\" before the space, something like that:

my\ file\ name.ext

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11447
  • FPC developer.
Re: Filenames with spaces in them passed as paramters
« Reply #8 on: July 06, 2014, 05:23:17 pm »
sometimes you must (or can) use parameters with the escape char "\" before the space, something like that:

my\ file\ name.ext

For most shells that is possible, and double \\ to form a \.

Note that when you execute on system level, without executing a new shell, no escaping whatsoever is allowed.

bylaardt

  • Sr. Member
  • ****
  • Posts: 309
Re: Filenames with spaces in them passed as paramters
« Reply #9 on: July 06, 2014, 08:17:15 pm »
what you mean with "system level"?
everytime i use \ it works to me!
have you a example when it doesn't works?
maybe i must reevaluate my applications...

Leledumbo

  • Hero Member
  • *****
  • Posts: 8757
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Filenames with spaces in them passed as paramters
« Reply #10 on: July 07, 2014, 12:20:36 pm »
what you mean with "system level"?
everytime i use \ it works to me!
have you a example when it doesn't works?
maybe i must reevaluate my applications...
System level = not through any shell

 

TinyPortal © 2005-2018