For now CreateDirectory() works only if all parent path exists already, i.e. following code will not create folder "D:\1\2" if "D:\1" not created already:
; Walks through given path and creates directories on the way
; Path$ full folder path
; RETURN: 1 on success, 0 if failed to create directory
Procedure CreatePath (Path$)
Protected a, s$, t$
Path$ = ReplaceString(Path$, "/", "\")
Repeat
a + 1
t$ = StringField(Path$, a, "\")
If t$
s$ + t$ + "\"
If FileSize(s$) = -1 ; doesn't exists
If Not CreateDirectory(s$)
ProcedureReturn #false ; failed to recreate path
EndIf
EndIf
Else
Break
EndIf
Until s$ = ""
ProcedureReturn #true
EndProcedure
; usage
CreatePath("D:\1\2")
;==================================================================
;
; Author: ts-soft
; Date: March 5th, 2010
; Explain:
; modified version from IBSoftware (CodeArchiv)
; on vista and above check the Request for "User mode" or "Administrator mode" in compileroptions
; (no virtualisation!)
;==================================================================
Procedure ForceDirectories(Dir.s)
Static tmpDir.s, Init
Protected result
If Len(Dir) = 0
ProcedureReturn #False
Else
If Not Init
tmpDir = Dir
Init = #True
EndIf
If (Right(Dir, 1) = "\")
Dir = Left(Dir, Len(Dir) - 1)
EndIf
If (Len(Dir) < 3) Or FileSize(Dir) = -2 Or GetPathPart(Dir) = Dir
If FileSize(tmpDir) = -2
result = #True
EndIf
tmpDir = "" : Init = #False
ProcedureReturn result
EndIf
ForceDirectories(GetPathPart(Dir))
ProcedureReturn CreateDirectory(Dir)
EndIf
EndProcedure
Debug ForceDirectories("Z:\aaa\bbb\") ; returns true if directory created
- It was too lonely at the top.
System : PB 6.10 LTS (x64) and Win Pro 11 (x64)
Hardware: AMD Ryzen 9 5900X w/64 gigs Ram, AMD RX 6950 XT Graphics w/16gigs Mem
Procedure.i CreateDirectoryEx(DirectoryName.s, FileAttribute = #PB_Default) ; Erstellt Verzeichnis, inklusive Übergeordnete
Protected i, c, tmp.s
If Right(DirectoryName, 1) = #PS$
DirectoryName = Left(DirectoryName, Len(DirectoryName) -1)
EndIf
c = CountString(DirectoryName, #PS$) + 1
For i = 1 To c
tmp + StringField(DirectoryName, i, #PS$)
If FileSize(tmp) <> -2
CreateDirectory(tmp)
EndIf
tmp + #PS$
Next
If FileAttribute <> #PB_Default
CompilerIf #PB_Compiler_OS = #PB_OS_Windows
SetFileAttributes_(DirectoryName, FileAttribute)
CompilerElse
SetFileAttributes(DirectoryName, FileAttribute)
CompilerEndIf
EndIf
If FileSize(DirectoryName) = -2
ProcedureReturn #True
EndIf
EndProcedure
Last edited by Bisonte on Fri Jan 08, 2021 10:39 am, edited 1 time in total.
PureBasic 6.10 LTS (Windows x86/x64) | Windows10 Pro x64 | Asus TUF X570 Gaming Plus | R9 5900X | 64GB RAM | GeForce RTX 3080 TI iChill X4 | HAF XF Evo | build by vannicom
English is not my native language... (I often use DeepL to translate my texts.)