Create Full Path Procedure

Share your advanced PureBasic knowledge/code with the community.
Randy Walker
Addict
Addict
Posts: 1281
Joined: Sun Jul 25, 2004 4:21 pm
Location: USoA
Contact:

Re: Create Full Path Procedure

Post by Randy Walker »

Bisonte wrote: Wed Mar 11, 2026 11:33 pm in a crossplatform way from TS-Soft (German Forum) modified by me a little... :

Code: Select all

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
Thanks for posting Bisonte! I was hoping the thread title might draw in a cross platform solution. Not that I need one (I don't) but just to help complete the full path concept within this thread for all to see.
- - - - - - - - - - - - - - - -
Randy
I *never* claimed to be a programmer.
Nothing is faster, more stable, or easier to maintain than code that doesn’t exist. :mrgreen:
User avatar
Piero
Addict
Addict
Posts: 1240
Joined: Sat Apr 29, 2023 6:04 pm
Location: Italy

Re: Create Full Path Procedure

Post by Piero »

Code: Select all

Procedure.s MakePath(FullPath$)
   Protected Path$, cs, i
   FullPath$ = RTrim(FullPath$, #PS$)
   cs = CountString(FullPath$, #PS$) + 1
   For i = 1 To cs
      Path$ + StringField(FullPath$, i, #PS$) + #PS$
      If FileSize(Path$) <> -2 And Not CreateDirectory(Path$)
         ProcedureReturn Path$ ; Error Folder
      EndIf
   Next
EndProcedure ; returns "" if no error occurred
BarryG
Addict
Addict
Posts: 4344
Joined: Thu Apr 18, 2019 8:17 am

Re: Create Full Path Procedure

Post by BarryG »

ebs wrote: Wed Mar 11, 2026 4:50 pmI use MakeSureDirectoryPathExists for the same purpose
This doesn't work with the latest PureBasic because it's ANSI only (not Unicode). That's why I use SHCreateDirectory_() instead.

Code: Select all

MakeSureDirectoryPathExists_("d:\1\2\3\") ; No dirs created.
Axolotl
Addict
Addict
Posts: 957
Joined: Wed Dec 31, 2008 3:36 pm

Re: Create Full Path Procedure

Post by Axolotl »

If you like recursion, or at least don't mind it, then you can also do it this way:

Code: Select all

Procedure EnsureDirectory(Directory$)  ; BOOL .. returns TRUE if directory exists, (already or created)  
  If Directory$                                                   : Debug #PB_Compiler_Procedure+"("+Directory$+")", 9 
    ;> here it doesn't matter if trailing #PS$ or not 
    If FileSize(Directory$) = #PB_FileSize_Directory 
      ProcedureReturn #True ; directory exists (already) 
    Else 
      If EnsureDirectory(GetPathPart(RTrim(Directory$, #PS$))) ; recursive call with parent directory 
        ;> CreateDirectory() .. returns nonzero if the operation was successful or zero if it failed. 
        ProcedureReturn CreateDirectory(Directory$)  
      EndIf 
    EndIf 
  EndIf 
  ProcedureReturn #False  ; clean code => defined return value 
EndProcedure 
Just because it worked doesn't mean it works.
PureBasic 6.04 (x86) and <latest stable version and current alpha/beta> (x64) on Windows 11 Home. Now started with Linux (VM: Ubuntu 22.04).
Post Reply