CreateDirectory() recursively or CreatePath()

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
User avatar
NicTheQuick
Addict
Addict
Posts: 1504
Joined: Sun Jun 22, 2003 7:43 pm
Location: Germany, Saarbrücken
Contact:

Re: CreateDirectory() recursively or CreatePath()

Post by NicTheQuick »

skywalk wrote:Yes, that makes sense. I am merely trying to maintain compatibility with Windows transfers between Linux servers.
The biggest issues I have with common directories between Linux and Windows are the characters ":", "?" and the double quotation mark. I use them quite often. Also it is annoying that Windows does not care about lower and upper case characters.
The english grammar is freeware, you can use it freely - But it's not Open Source, i.e. you can not change it or publish it in altered way.
User avatar
skywalk
Addict
Addict
Posts: 4211
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: CreateDirectory() recursively or CreatePath()

Post by skywalk »

My approach is KISS. The filenames just have to be unique without so much information conveyed. The contents are what matters.
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
AZJIO
Addict
Addict
Posts: 2143
Joined: Sun May 14, 2017 1:48 am

Re: CreateDirectory() recursively or CreatePath()

Post by AZJIO »

I made an improvement to the ForceDirectories function

Code: Select all

Procedure ForceDirectories(Dir.s)
	Static tmpDir.s, Init
	Protected result

	If Asc(Dir)
		If Not Init
			tmpDir = Dir
			Init = 1
		EndIf
		Dir = RTrim(Dir, #PS$)
		If FileSize(Dir) = -2 Or (Len(Dir) < 3) Or GetPathPart(Dir) = Dir
			If FileSize(tmpDir) = -2
				result = -1
			EndIf
			tmpDir = ""
			Init = 0
			ProcedureReturn result
		EndIf
		ForceDirectories(GetPathPart(Dir))
		ProcedureReturn CreateDirectory(Dir)
	Else
		ProcedureReturn 0
	EndIf
EndProcedure
1. #PS$ makes it cross-platform
2. If FileSize(Dir) = -2 moved to the beginning, since most people use it as a criterion
3. Dir = RTrim(Dir, #PS$) - eliminated the character check and immediately trimmed it so as not to check it twice
4. Asc(Dir) is faster than Len(Dir)
5. result = -1 ; additional information about the fact that the folder already exists without using CreateDirectory(Dir)
Post Reply