Page 2 of 2

Re: Create Full Path Procedure

Posted: Thu Mar 12, 2026 5:14 am
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.

Re: Create Full Path Procedure

Posted: Thu Mar 12, 2026 7:15 am
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

Re: Create Full Path Procedure

Posted: Thu Mar 12, 2026 8:40 am
by AZJIO

Re: Create Full Path Procedure

Posted: Thu Mar 12, 2026 11:22 am
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.

Re: Create Full Path Procedure

Posted: Thu Mar 12, 2026 3:05 pm
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