How to create a folder on the C drive if it does not exist and create a file in it if it does not exist. If they are - just open for writing.

    2 answers 2

    There is only a question of fantasy

    Tries to create directory: CreateDir(Path):boolean;

    Trying to create a directory just do not remember exactly what is different: MkDir(Path):boolean;

    And this type takes a crowbar and creates if the above listed did not work well, and as far as I remember, it can create recursive paths (until all the directories from the path exist): ForceDirectories(Path):boolean;

    • You can still reinvent the wheel and use the console - Walfter
    • SPar: = '/ K MD' + Path; ShellExecute (Handle, nil, 'cmd.exe', PChar (SPar), nil, SW_SHOW); Yuzaya ShellApi - Walfter
    • Thank you very much! - Program_Casual

    It is more appropriate to use classes with static methods of the System.IOUtils module:

     TDirectory.Exists('full_path_to_dir'); TDirectory.CreateDirectory('path_to_dir'); 

    Example of use on the issue:

     if not TDirectory.Exists('C:\Folder') then TDirectory.CreateDirectory('C:\Folder'); if not TFile.Exists('C:\Folder\file.txt') then TFile.Create('C:\Folder\file.txt'); 

    TFile.Create returns a TFileStream, with which you can continue to work further.

    There are many other useful things.