My primary language is not US/UK. I am using Google Translate.
Code: Select all
Global.S myFOLDER = "D:\abc\def\gh"
CreateDirectory(myFOLDER)
Code: Select all
Global.S myFOLDER = "D:\abc\def\gh"
CreateDirectory(myFOLDER)
If the path doesn't exist, is there any way to force the directory to be created ?
Code: Select all
Procedure CreateDirectoryEX(path.s)
Protected ct,tpath.s
ct = CountString(path,#PS$) +1
For a = 1 To ct
tpath + StringField(path,a,#PS$) + #PS$
If FileSize(tpath)<> -2
CreateDirectory(tpath)
EndIf
Next
EndProcedure
The folder has been created.idle wrote: Tue May 21, 2024 9:43 pm you need to test the whole path "D:\abc" and "D:\abc\def" ...
Code: Select all
Procedure CreateDirectoryEX(path.s) Protected ct,tpath.s ct = CountString(path,#PS$) +1 For a = 1 To ct tpath + StringField(path,a,#PS$) + #PS$ If FileSize(tpath)<> -2 CreateDirectory(tpath) EndIf Next EndProcedure
Because nested folders aren't supported. This is mentioned in the manual.
Code: Select all
SHCreateDirectory_(0,"D:\abc\def\gh")
Code: Select all
Procedure CreateDirectoryEX(path.s)
Protected ct,tpath.s
CompilerIf #PB_OS_Windows
ReplaceString(path,"/","\",#PB_String_InPlace)
CompilerElse
ReplaceString(path,"\","/",#PB_String_InPlace)
CompilerEndIf
ct = CountString(path,#PS$) +1
For a = 1 To ct
tpath + StringField(path,a,#PS$) + #PS$
If FileSize(tpath)<> -2
If CreateDirectory(tpath) = 0
ProcedureReturn 0
EndIf
EndIf
Next
If FileSize(path) = -2
ProcedureReturn #True
EndIf
EndProcedure
Global dir.s = "D:/test/abc\def\"
Debug CreateDirectoryEX(dir)
Code: Select all
Procedure CreateDirectoryEX(path.s)
Protected i, tpath.s, ct
ReplaceString(path, #NPS$, #PS$, #PB_String_InPlace)
ct = CountString(path, #PS$) + 1
For i = 1 To ct
tpath + StringField(path, i, #PS$) + #PS$
If FileSize(tpath) <> -2 And CreateDirectory(tpath) = 0
ProcedureReturn #False
EndIf
Next
ProcedureReturn Bool(FileSize(path) = -2)
EndProcedure
Global dir.s = "D:/test/abc\def\l00"
Debug CreateDirectoryEX(dir)
I had no idea we had #NPS$tua wrote: Wed May 22, 2024 4:23 pm Or even more succinctly (without having to check the OS):
Code: Select all
Procedure CreateDirectoryEX(path.s) Protected i, tpath.s, ct ReplaceString(path, #NPS$, #PS$, #PB_String_InPlace) ct = CountString(path, #PS$) + 1 For i = 1 To ct tpath + StringField(path, i, #PS$) + #PS$ If FileSize(tpath) <> -2 And CreateDirectory(tpath) = 0 ProcedureReturn #False EndIf Next ProcedureReturn Bool(FileSize(path) = -2) EndProcedure Global dir.s = "D:/test/abc\def\l00" Debug CreateDirectoryEX(dir)
Code: Select all
Procedure.s QuotePath(str$)
ProcedureReturn "'" + ReplaceString(str$, "'", "'\''") + "'"
EndProcedure
Code: Select all
; Bisonte: http://www.purebasic.fr/english/viewtopic.php?p=496867#p496867
Prototype proto_msdpe(Path.p-Ascii)
Procedure _MSDPE(Path.s)
ProcedureReturn MakeSureDirectoryPathExists_(Path)
EndProcedure
Global MakeSureDirectoryPathExists.proto_msdpe = @_MSDPE()
Debug MakeSureDirectoryPathExists("C:\a\b\c\d\")