Solved

Create a path if not exists

Posted on 2016-11-03
7
37 Views
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?
0
Comment
Question by:Ingo Foerster
7 Comments
 
LVL 9

Expert Comment

by:Evan Cutler
Comment Utility
two options:


ForceDirectories('D:\Part1\Part2\Part3\Part4\');


or

if not DirectoryExists('c:\test') then
begin
CreateDir('C:\test');
end;
1
 

Author Comment

by:Ingo Foerster
Comment Utility
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());

	if(dwAtt != INVALID_FILE_ATTRIBUTES){
		if((dwAtt & FILE_ATTRIBUTE_DIRECTORY) == FILE_ATTRIBUTE_DIRECTORY)
			return (dwAtt);
	}

    //if(FILE_ATTRIBUTE_DIRECTORY == dwAtt)
		//return (dwAtt);
    //if(dwAtt & FILE_ATTRIBUTE_DIRECTORY)
		//return (dwAtt);

    wcscpy(acDir, wildCard.c_str());

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

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

    dwAtt = ChkCreateDir(acDir);

    if((dwAtt & FILE_ATTRIBUTE_DIRECTORY) == FILE_ATTRIBUTE_DIRECTORY)
    {
		if(!CreateDirectory(wildCard.c_str(), NULL))
		{
			//ErrorExit(L"ChkCreateDir",L""); 
			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.
0
 
LVL 22

Accepted Solution

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

This example comes from Embarcadero Docwiki
procedure TForm1.Button1Click(Sender: TObject);
var
  Dir: string;
begin
  Dir := 'C:\APPS\SALES\LOCAL';
  if SysUtils.ForceDirectories(Dir) then
    Label1.Caption := Dir + ' was created'
end;

Open in new window

0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 32

Expert Comment

by:sarabande
Comment Utility
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) ;
begin
   ListOfStrings.Clear;
   ListOfStrings.Delimiter       := Delimiter;
   ListOfStrings.StrictDelimiter := True; 
   ListOfStrings.DelimitedText   := Str;
end;

Open in new window


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

Sara
0
 
LVL 36

Expert Comment

by:Geert Gruwez
Comment Utility
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 ?
0
 

Author Comment

by:Ingo Foerster
Comment Utility
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.
0
 

Author Closing Comment

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

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
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.

728 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now