Create a path if not exists

Posted on 2016-11-03
Last Modified: 2016-11-04
How can I manage to force create a full path in Delphi?

Sample: D:\Part1\Part2\Part3\Part4\

Function willl check if path exists. If not exists it have to create the path.
I think about a function that will check each folder whether it exists and if not it will create it until it reach the top folder.

I know how to do this in C++ but how to manage this in Delphi?
Question by:Ingo Foerster

Expert Comment

by:Evan Cutler
ID: 41872837
two options:



if not DirectoryExists('c:\test') then

Author Comment

by:Ingo Foerster
ID: 41872920
ok, I understand. But because I will not work on the file system with the function, I will work on a own file tree I wanted to now how to read each folder out of the path in Delphi

In C++ I use:

DWORD ChkCreateDir(const std::wstring &path)
    std::wstring wildCard = path;
    wchar_t acDir[MAX_PATH];
    wchar_t* pc = NULL;
    DWORD dwAtt = -1;

	//Check if the path contains Backslash
	if ( wildCard[wildCard.size() - 1] == L'\\' )
		wildCard = wildCard.substr( 0, wildCard.size( ) - 1 );

    dwAtt = GetFileAttributes (wildCard.c_str());

			return (dwAtt);

		//return (dwAtt);
		//return (dwAtt);

    wcscpy(acDir, wildCard.c_str());

    pc = wcsrchr(acDir, L'\\');

		*pc = (wchar_t) 0;
		return (0xffffffff);

    dwAtt = ChkCreateDir(acDir);

		if(!CreateDirectory(wildCard.c_str(), NULL))
			return GetLastError();

		return (dwAtt);

    return (0);

Open in new window

I think I have to write a own ForceDirectory function because it is not a common file tree.
LVL 22

Accepted Solution

Ferruccio Accalai earned 500 total points
ID: 41873246
As Evan suggested Sysutils.ForceDirectories does exactly what you're asking for

This example comes from Embarcadero Docwiki
procedure TForm1.Button1Click(Sender: TObject);
  Dir: string;
  if SysUtils.ForceDirectories(Dir) then
    Label1.Caption := Dir + ' was created'

Open in new window

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

LVL 33

Expert Comment

ID: 41873597
I will not work on the file system with the function, I will work on a own file tree I wanted to now how to read each folder out of the path in Delphi
does that mean you actually don't want to create the folders as askd in the op?

if you only want to parse the path, you may use TStrings DelimitedText

procedure Split(Delimiter: Char; Str: string; ListOfStrings: TStrings) ;
   ListOfStrings.Delimiter       := Delimiter;
   ListOfStrings.StrictDelimiter := True; 
   ListOfStrings.DelimitedText   := Str;

Open in new window

you will find this and other code samples if you search for Delphi and DelimitedText.

LVL 37

Expert Comment

by:Geert Gruwez
ID: 41873602
you have an own tree structure for directory paths on a windows system which does not follow the windows filing system ?

and how does windows find your files then ?

Author Comment

by:Ingo Foerster
ID: 41873703
Please understand that Windows is just a small piece of the world. There are some open file systems for disks as example, like UDF. For this UDF authoring I need to write a virtual file sytsme (tree) to place the files well and work with sorting feature and to follow some special specifications.
So this is not a Windows nor macOS or Linux, it is a virtual file system where windows special functions will not work.

However, I have found that the ForceDirectories code is open source and I was able to rewrite the code in the case I need. Just replaced with CreateDir and DirExists of my own file system objects and it work well.

Author Closing Comment

by:Ingo Foerster
ID: 41873723
ForceDirectories was right because I was able to find this in opensource and rewrite the function to my needs.

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Arduino EDI - Programming language 3 103
Tviruailstringtree sort multi columns on header click 1 74
Safe conversion? 4 72
update joined tables 2 55
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question