How can I determine which folder the user was browsing when my program was invoked from the “sent to” menu?

StackOverflow https://stackoverflow.com/questions/10969562

Question

I put a shortcut to my application in SendTo. Now I can select some files in Explorer and send them to my application. But how can I get the path where the files are? My program is supposed to create a new file in the parent directory that's common to all the received file names.

For example, if I have these files:

C:\one.txt
C:\1\

I select the file one.txt and the directory 1 in Explorer. How can I find out that the starting directory for these files is C:\?

I know I can use ParamStr() to get files' paths, but what then? I could try to get common directory for all the files passed to my application, but if I select in C:\ directories 1 and 2 and these directories look like this:

C:\1\4\5\one.txt
C:\1\4\two.txt

Then the starting directory is C:\1\4.

Was it helpful?

Solution

Prior to Windows 7, all files selected in Explorer always reside in the same directory, so it's easy to know which directory the user was browsing. It's the same as the directory of any one file. Use ExtractFilePath for that.

As of Windows 7, though, Explorer lets you put directories into groups called libraries. All the files from all the folders are displayed together in a single view. Users can select files from multiple directories and send them all to your program at once. The view doesn't represent any real directory on the disk, so the question asked here is meaningless.

As an alternative, you could decide use the ancestor directory common to all the files sent to your program, but that won't tell you much. For one thing, if the files reside on multiple drives, the common directory will be the empty string. The directory you calculate also might not be writable by the current account, even if the directories of one or more of the selected files are.

It will probably be easier to just use the directory of the first file you receive, or even to display a UI that asks the user what directory to use in the cases you can't determine it automatically. (Maybe you could pre-populate the result with the first directory, so the user doesn't have to do anything but approve your suggestion in the common case.)

OTHER TIPS

I think I will just take all the filenames passed to my program via ParamStr and use this function: http://delphi.about.com/od/delphichallengesexercises/qt/delphi-extract-base-path-challenge.htm to find common base path which should be the path where the SendTo was invoked.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top