Why is the directory not created ?

Just starting out? Need help? Post your questions and find answers here.
hoangdiemtinh
User
User
Posts: 97
Joined: Wed Nov 16, 2022 1:51 pm

Why is the directory not created ?

Post 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 ?
PC: Windows 10 x64, 8GB RAM. PB ver: 6.x
--
I love PB5 vs PB6 :)
AZJIO
Addict
Addict
Posts: 2141
Joined: Sun May 14, 2017 1:48 am

Re: Why is the directory not created ?

Post by AZJIO »

D:\abc\def Does this path exist?
hoangdiemtinh
User
User
Posts: 97
Joined: Wed Nov 16, 2022 1:51 pm

Re: Why is the directory not created ?

Post 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 ?
PC: Windows 10 x64, 8GB RAM. PB ver: 6.x
--
I love PB5 vs PB6 :)
User avatar
idle
Always Here
Always Here
Posts: 5836
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: Why is the directory not created ?

Post 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   

hoangdiemtinh
User
User
Posts: 97
Joined: Wed Nov 16, 2022 1:51 pm

Re: Why is the directory not created ?

Post 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
PC: Windows 10 x64, 8GB RAM. PB ver: 6.x
--
I love PB5 vs PB6 :)
BarryG
Addict
Addict
Posts: 4123
Joined: Thu Apr 18, 2019 8:17 am

Re: Why is the directory not created ?

Post 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")
User avatar
idle
Always Here
Always Here
Posts: 5836
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: Why is the directory not created ?

Post 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) 

tua
User
User
Posts: 68
Joined: Sun Jul 23, 2023 8:49 pm
Location: BC, Canada

Re: Why is the directory not created ?

Post 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)
User avatar
idle
Always Here
Always Here
Posts: 5836
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: Why is the directory not created ?

Post 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$ :D
AZJIO
Addict
Addict
Posts: 2141
Joined: Sun May 14, 2017 1:48 am

Re: Why is the directory not created ?

Post by AZJIO »

On Linux, the #NPS$ character is a valid file/folder name character and should not be corrupted
tua
User
User
Posts: 68
Joined: Sun Jul 23, 2023 8:49 pm
Location: BC, Canada

Re: Why is the directory not created ?

Post by tua »

Hehe, that's correct. However, putting a backslash in a unix filename should be punishable by 5 strokes with a keyboard ...
User avatar
Piero
Addict
Addict
Posts: 864
Joined: Sat Apr 29, 2023 6:04 pm
Location: Italy

Re: Why is the directory not created ?

Post 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
eJan
Enthusiast
Enthusiast
Posts: 366
Joined: Sun May 21, 2006 11:22 pm
Location: Sankt Veit am Flaum

Re: Why is the directory not created ?

Post 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\")
Image
Post Reply