[Lazarus] How to create a directory from a path string? (cross-platform needed).

Sven Barth pascaldragon at googlemail.com
Wed Mar 16 09:39:38 CET 2011


Am 15.03.2011 17:48, schrieb Peter Williams:
> Hi Sven,
>
>  > Date: Tue, 15 Mar 2011 16:02:55 +0100
>  > From: pascaldragon at googlemail.com
>  > To: lazarus at lists.lazarus.freepascal.org
>  > Subject: Re: [Lazarus] Unable to rewrite .ini file which exists...
> error unable to create .ini file. (need to rewrite it).
>  >
>  > Am 15.03.2011 15:51, schrieb Peter Williams:
>  > > I just tried calling Str1 := GetAppConfigDir(true); ShowMessage(Str1);
>  > >
>  > > and my app is called Color_Life.exe and it returns
>  > > "C:\ProgramData\Color_Life\". Since the folder "C:\ProgramData\" exists
>  > > and it's subfolder "\Color_Life\" does NOT exist, then does this mean
>  > > that I need to create it myself, or will my call to one of the
>  > > "MemIniFiles" procedures create the folder for me?
>  >
>  > You need to create the path yourself. As I said: those functions just
>  > return a string. What you do with it, is up to you.
>
> var
> str1 : string;
> begin
> str1 := GetAppConfigDir( true );
> ShowMessage( Str1 );
> // note: for Fred.exe returns C:\ProgramData\Fred\
> if not DirectoryExists(Str1) then
> // Create Directory Str1
> // call to 'mkdir' ??? what about on Linux??? and other platforms?
>
> What is the cross-platform way for me to create the directory in Str1
> (as returned by GetAppConfigDir) ?
>
> I would assume that I need to ShellExecute a command for 'mkdir' for
> Windows, correct? How about for Linux and Macs, etc?
>
> I found this page:
>
> http://wiki.lazarus.freepascal.org/Executing_External_Programs
>
> But I have to admit that I was totally lost in what it was trying to
> explain.
>
> An example, please?

While the others already sent you the correct solution 
("ForceDirectories") I just wanted to note, that FPC abstracts all those 
file and directory related functions for you. There are for example the 
very low level functions mkdir and rmdir that are part of the System unit.

See here: http://www.freepascal.org/docs-html/rtl/system/mkdir.html

Then there are the functions defined in SysUtils like CreateDir, 
ForceDirectories and RemoveDir.

See here: http://www.freepascal.org/docs-html/rtl/sysutils/createdir.html

And recent Delphi versions also include a higher level by introducing 
advanced record types that handle all this:
http://docwiki.embarcadero.com/VCL/en/IOUtils.TDirectory

Regards,
Sven




More information about the Lazarus mailing list