Find directory of "My Documents" or local equivalent

I need to specify a subdirectory relative to the "My documents" path. I'd like to do that with hardcoding the path. Initially I tried
Type DirPath = '%HOMEDRIVE%%HOMEPATH%\My Documents\Admin\logs\' ;
Well, I get "illegal filename" when using the %HOMEDRIVE% stuff. I cant use the GetEnviromentVariable (as recomended in many answers on EE) as this is Delphi5 and it isnt in help (or is there a unit that isnt covered in Help?) so I assume that is D6 or above.
Also, what is the trick to get the name of "My Documents" ? It is "Dokumenter" on Danish Windows as an example (and I did discover it in the registry one time long ago) The whole path for MyDocuments would do nicely (and thus ellminate the need for HOMEDRIVE/PATH)
Delphi 5, running on Windows XP
Who is Participating?
ZhaawZSoftware DeveloperCommented:
Not sure about Delphi5, but there are a lot of things that are not in the help of Delphi 6 Personal :)

unit Unit1;


  Windows, Messages, Classes, Controls, Forms, Dialogs, ShlObj;

  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);

  Form1: TForm1;


{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
  path : array [0..255] of char;
SHGetSpecialFolderPath(0, @path, CSIDL_PERSONAL, false);

MerijnBSr. Software EngineerCommented:
ZhaawZ, why do you provide the same solution as dinilud?
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

ZhaawZSoftware DeveloperCommented:
It's not really the same. It may seem similar, but different functions are used. I think it's good to know that there are more than 1 ways to get the result, isn't it?

I'm sorry if I offended you with that - I didn't want to...

The sollution you gave uses SHGetSpecialFolderLocation() AND SHGetPathFromIDList() functions to get the past, while it's possible to use one function instead - SHGetSpecialFolderPath().
ZhaawZSoftware DeveloperCommented:
... to get the path*, while ...
MsquareAuthor Commented:
Thanks to both. I choose the latter version as it seems shorter. Also had to adapt the answer to get it all tied up. It works now.
I'll split this 1:2 (if I can)
MerijnBSr. Software EngineerCommented:
ZhaawZ, I never suggested a solution.
It just seems too happen so often on EE that people give a duplicate answer just to get the points. It's very annoying.
ZhaawZSoftware DeveloperCommented:
Sorry, didn't notice that sollution was from dinilud, not from you (I was too sleepy probably).

About the answer - as I said, it's not the same...
MsquareAuthor Commented:
Well, not part of this question ... but I just note that the "FAQ/Rulebook" says the process in EE is to receive several answers and the questioner gets to choose the best one. :-)
If you want to discuss if the answers are too close to each other ... (do it somewhere else on this site?)

I do have a microscopic followup ... how do I find more "goodies" in ShlObj ? OK, ok, I looked in the source for the unit, and it clearly requires a Windows SDK or suchlike. Never mind, I'll ask in a seperate question if/when I have the need/time to go down that path.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.