Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Can I Handle Parameters With Spaces in Delphi?

My program accepts input file names either as command line parameters or in a drag and drop operation or in Explorer by clicking on filenames with an extension that is associated with my program.

The command line and drag and drop work fine, but it is clicking on the filenames in Explorer that causes problems when the filepaths of the files clicked on have spaces in them, e.g.:

c:\temp\file one.txt
c:\my directory\filetwo.txt
c:\my directory\file three.txt

then, the ParamStr function gives me back:

ParamStr(1):  c:\temp\file
ParamStr(2):  one.txt
ParamStr(3):  c:\my
ParamStr(4):  directory\filetwo.txt
ParamStr(5):  c:\my
ParamStr(6):  directory\file
ParamStr(7):  three.txt

How can I best reconstitute these back into the three filenames that I need?

like image 788
lkessler Avatar asked Aug 31 '10 00:08

lkessler


2 Answers

It might be your shell file association that does not include the pair of "".

Like these ones for opening:

"C:\Program Files\WinRAR\WinRAR.exe" "%1"  

or with DDE message:

[open("%1")]
like image 89
Francesca Avatar answered Sep 19 '22 22:09

Francesca


Command-line parameters with spaces in them, such as filenames, should be quoted. This makes the param parser realize that it's supposed to keep them together. If the user's not quoting the filename, it's operator error.

If a drag-and-drop system is doing this, on the other hand, then you've got a bug in your drag-and-drop library and you need to talk to whoever created it. I'm a bit confused, though, as to why drag-and-drop operations are messing with ParamStr. That should only be set by the params passed to your program at the moment it's invoked, not once it's up and running. Maybe I'm missing something?

like image 34
Mason Wheeler Avatar answered Sep 19 '22 22:09

Mason Wheeler