Kokonotsu
Kokonotsu

Reputation: 189

ForceDirectories returns False

I am using the ForceDirectories function like this:

ForceDirectories('C:/Path/To/Dir');

And it returns False and no directories at all are created. GetLastError returns 0. I am running the program with Administrative rights.

If I do

ForceDirectories('C:/Path');
ForceDirectories('C:/Path/To');
ForceDirectories('C:/Path/To/Dir');

Each call succeeds and the directories are created. However, this voids the usefulness of the ForceDirectories function. Does anyone know why it behaves this way? (I'm looking at you David)

Upvotes: 3

Views: 1969

Answers (2)

Deltics
Deltics

Reputation: 23052

Change your path delimiter to that which is correct for your platform (Win32) and all will be good:

  ForceDirectories('c:\Path\To\Dir');

To make the code portable across platforms (in preparation for some future time when this may be relevant to your Delphi code) you could:

  s := 'c:/Path/To/Dir';  // << example

  s := StringReplace(s, '/', PathDelim, [rfReplaceAll]);
  s := StringReplace(s, '\', PathDelim, [rfReplaceAll]);
  ForceDirectories(s);

This could be improved for re-use (only search/replace the symbol which is no = PathDelim) but demonstrates the principle.

Upvotes: 11

Kokonotsu
Kokonotsu

Reputation: 189

Apparently ForceDirectories only likes \'s, not /'s... Stupid problem solved.

Upvotes: 5

Related Questions