CreateDirectory() with flag #PB_FileSystem_Recursive

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
uwekel
Enthusiast
Enthusiast
Posts: 740
Joined: Sat Dec 03, 2011 5:54 pm
Location: Oldenburg (Germany)

CreateDirectory() with flag #PB_FileSystem_Recursive

Post by uwekel »

Hi,

it would be nice to have a flag for the CreateDirectory() function for creating all folders for the given path if the #PB_FileSystem_Recursive flag would be set.

Thx, Uwe
PB 5.70 LTS (x64) - Debian Testing, Gnome 3.30.2
User avatar
TI-994A
Addict
Addict
Posts: 2512
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: CreateDirectory() with flag #PB_FileSystem_Recursive

Post by TI-994A »

uwekel wrote:...a flag for the CreateDirectory() function for creating all folders for the given path if the #PB_FileSystem_Recursive flag would be set.
+1

In the meantime, here's a routine that does just that:

Code: Select all

Procedure CreateDirectories(fullPath.s)
  fullPath = ReplaceString(fullPath, "\", "/")
  
  If Right(fullPath, 1) <> "/"
    fullPath + "/"
  EndIf
  
  depth = CountString(fullPath, "/")
  Dim folder.s(depth)
  
  For i = 1 To depth
    folder(i) = StringField(fullPath, i, "/")
  Next
  
  For i = 1 To depth        
    If i = 1
      currentFolder$ = folder(i)
      If CountString(folder(i), ":")
        currentFolder$ + "/" + folder(i + 1)
        i + 1 
      EndIf
    Else      
      currentFolder$ + "/" + folder(i)        
    EndIf    
    If FileSize(currentFolder$) <> -2
      If CreateDirectory(currentFolder$) = 0
        Break
      Else
        Debug currentFolder$ + " created!"
      EndIf        
    EndIf      
  Next  
  
EndProcedure

CreateDirectories("d:\Fantaisie\PureBasic\64bit\examples\advanced\graphics\bitmaps")
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel :D
davido
Addict
Addict
Posts: 1890
Joined: Fri Nov 09, 2012 11:04 pm
Location: Uttoxeter, UK

Re: CreateDirectory() with flag #PB_FileSystem_Recursive

Post by davido »

+1
DE AA EB
IdeasVacuum
Always Here
Always Here
Posts: 6425
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: CreateDirectory() with flag #PB_FileSystem_Recursive

Post by IdeasVacuum »

+1
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
User avatar
Kiffi
Addict
Addict
Posts: 1353
Joined: Tue Mar 02, 2004 1:20 pm
Location: Amphibios 9

Re: CreateDirectory() with flag #PB_FileSystem_Recursive

Post by Kiffi »

that would be quite helpful -> +1

Thanks in advance & Greetings ... Peter
Hygge
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

Re: CreateDirectory() with flag #PB_FileSystem_Recursive

Post by Mistrel »

There is also a native Win32 command which does this:

https://msdn.microsoft.com/en-us/librar ... s.85).aspx
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Re: CreateDirectory() with flag #PB_FileSystem_Recursive

Post by Dude »

Mistrel, MakeSureDirectoryPathExists_() doesn't work with Unicode, which PureBasic now is. However, we can use SHCreateDirectory_() instead:

Code: Select all

dirtree$="d:\aa\bb\cc\"
Debug MakeSureDirectoryPathExists_(dirtree$) ; Shows 1, but didn't work.
Debug SHCreateDirectory_(0,dirtree$) ; Shows 0, but did create the tree.
Or here's some short cross-platform code that I used to use:

Code: Select all

Procedure CreateDirectoryTree(dir$)
  ok=1
  If Right(dir$,1)<>"\" : dir$+"\" : EndIf
  If FileSize(dir$)<>-2
    For a=1 To CountString(dir$,"\")
      tmp$+StringField(dir$,a,"\")+"\"
      If FileSize(tmp$)=-1
        If CreateDirectory(tmp$)=0
          ok=0
          Break
        EndIf
      EndIf
    Next
  EndIf
  ProcedureReturn ok
EndProcedure
User avatar
RSBasic
Moderator
Moderator
Posts: 1218
Joined: Thu Dec 31, 2009 11:05 pm
Location: Gernsbach (Germany)
Contact:

Re: CreateDirectory() with flag #PB_FileSystem_Recursive

Post by RSBasic »

+1
Image
Image
Cyllceaux
Enthusiast
Enthusiast
Posts: 458
Joined: Mon Jun 23, 2014 1:18 pm
Contact:

Re: CreateDirectory() with flag #PB_FileSystem_Recursive

Post by Cyllceaux »

+1
Post Reply