Page 1 of 1
Why is the directory not created ?
Posted: Tue May 21, 2024 9:30 pm
by hoangdiemtinh
I am new to PB.
My primary language is not US/UK. I am using Google Translate.
Code: Select all
Global.S myFOLDER = "D:\abc\def\gh"
CreateDirectory(myFOLDER)
Why is the directory not created ?
Re: Why is the directory not created ?
Posted: Tue May 21, 2024 9:34 pm
by AZJIO
D:\abc\def Does this path exist?
Re: Why is the directory not created ?
Posted: Tue May 21, 2024 9:40 pm
by hoangdiemtinh
AZJIO wrote: Tue May 21, 2024 9:34 pm
D:\abc\def Does this path exist?
If the path doesn't exist, is there any way to force the directory to be created ?
Re: Why is the directory not created ?
Posted: Tue May 21, 2024 9:43 pm
by idle
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
Re: Why is the directory not created ?
Posted: Tue May 21, 2024 10:07 pm
by AZJIO
Re: Why is the directory not created ?
Posted: Wed May 22, 2024 1:17 am
by hoangdiemtinh
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
The folder has been created.
By the way, I use IsDirectory(tpath) to evaluate the folder that has been successfully created, but I encountered an error message.
---------------------------------
Edit:
I found this.
https://www.purebasic.fr/english/viewtopic.php?t=68073
Re: Why is the directory not created ?
Posted: Wed May 22, 2024 1:29 am
by BarryG
hoangdiemtinh wrote: Tue May 21, 2024 9:30 pmWhy is the directory not created ?
Because nested folders aren't supported. This is mentioned in the manual.
For Windows, you can do it like this:
Code: Select all
SHCreateDirectory_(0,"D:\abc\def\gh")
Re: Why is the directory not created ?
Posted: Wed May 22, 2024 1:32 am
by idle
Add a final test to verify the directory and replace path separators in place if needed
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)
Re: Why is the directory not created ?
Posted: Wed May 22, 2024 4:23 pm
by tua
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)
Re: Why is the directory not created ?
Posted: Wed May 22, 2024 8:50 pm
by idle
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)
I had no idea we had #NPS$

Re: Why is the directory not created ?
Posted: Wed May 22, 2024 9:51 pm
by AZJIO
On Linux, the #NPS$ character is a valid file/folder name character and should not be corrupted
Re: Why is the directory not created ?
Posted: Wed May 22, 2024 11:32 pm
by tua
Hehe, that's correct. However, putting a backslash in a unix filename should be punishable by 5 strokes with a keyboard ...
Re: Why is the directory not created ?
Posted: Thu May 23, 2024 5:31 pm
by Piero
On Mac (BSD) if you drag something on Terminal you get backslashes for spaces etc., but that's not the best way:
Code: Select all
Procedure.s QuotePath(str$)
ProcedureReturn "'" + ReplaceString(str$, "'", "'\''") + "'"
EndProcedure
Re: Why is the directory not created ?
Posted: Fri May 24, 2024 11:29 pm
by eJan
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\")