views:

143

answers:

3

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?

+3  A: 

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?

Mason Wheeler
@Mason: Whoops. It's not drag and drop at all. Drag and Drop does work fine. It's clicking on the file when my application is associated with it. I'll edit my question to reflect this.
lkessler
+1  A: 

i use the CmdLineHelper unit, from here.

glob
@glob: Thanks for pointing out that utility. I was going to try it, but then François' answer solved my problem.
lkessler
+7  A: 

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")]
François
@François: That was it! I was doing this: RegistrySoftwareClasses.WriteString('Behold.ged\shell\open\command', '',extractfilepath(Application.Exename) + 'behold.exe %1' and I needed "" around the %1.
lkessler
Do you know how I can get multiple files to open in one instance of the program doing this? See: http://stackoverflow.com/questions/3605903/how-can-i-get-my-file-association-to-open-multiple-files-in-a-single-program-inst
lkessler