Create Folder-Tree

Share your advanced PureBasic knowledge/code with the community.
jamirokwai
Enthusiast
Enthusiast
Posts: 798
Joined: Tue May 20, 2008 2:12 am
Location: Cologne, Germany
Contact:

Create Folder-Tree

Post by jamirokwai »

Hi there,

as CreateDirectory does not create a folder tree, I hacked a small function to do this...
Should be self explanatory.

Code: Select all

Procedure   Create_FolderTree(dest$)
  If Right(dest$,1) <> "/" : dest$ + "/" : EndIf
  If FileSize(dest$) = -1
    Temp$ = ""
    For i = 1 To CountString(dest$,"/")
      temp$ + StringField(dest$,i,"/") + "/"
      If FileSize(Temp$) = -1
        CreateDirectory(Temp$)
      EndIf
    Next i
  EndIf 
EndProcedure
Last edited by jamirokwai on Tue Apr 17, 2012 11:17 am, edited 1 time in total.
Regards,
JamiroKwai
User avatar
Guimauve
Enthusiast
Enthusiast
Posts: 742
Joined: Wed Oct 22, 2003 2:51 am
Location: Canada

Re: Create Folder-Tree

Post by Guimauve »

Hello,

This a cross-platform solution :

Code: Select all

; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; Project name : BuiltPathDirectory
; File Name : BuiltPathDirectory.pb
; File version: 1.1.0
; Programming : OK
; Programmed by : Guimauve
; Date : 29-04-2008
; Last Update : 15-04-2012
; PureBasic code : 4.60
; Platform : Windows, Linux, MacOS X
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

Procedure.b BuiltPathDirectory(Path.s)
  
  CompilerSelect #PB_Compiler_OS
      
    CompilerCase #PB_OS_Windows 
      PathSep.s = "\"
      
    CompilerCase #PB_OS_Linux
      PathSep.s = "/"
      
    CompilerCase #PB_OS_MacOS
      PathSep.s = "/"
      
  CompilerEndSelect
  
  DirectoryQty = CountString(Path, PathSep) + 1
  
  For Index = 1 To DirectoryQty
    
    Directory.s = Directory + StringField(Path, Index, PathSep) + PathSep
    
    If FileSize(Directory) = -1
      CreateDirectory(Directory)
    EndIf 
    
  Next
  
  If FileSize(Directory) = -2
    Success.b = #True
  Else
    Success = #False
  EndIf
  
  ProcedureReturn Success
EndProcedure

; <<<<<<<<<<<<<<<<<<<<<<<
; <<<<< END OF FILE <<<<<
; <<<<<<<<<<<<<<<<<<<<<<<
Best regards
Guimauve
USCode
Addict
Addict
Posts: 924
Joined: Wed Mar 24, 2004 11:04 pm
Location: Seattle

Re: Create Folder-Tree

Post by USCode »

Doesn't Windows accept both / and \ as path delimiters ?
User avatar
HeX0R
Addict
Addict
Posts: 1205
Joined: Mon Sep 20, 2004 7:12 am
Location: Hell

Re: Create Folder-Tree

Post by HeX0R »

jamirokwai wrote:Hi there,
Should be self explanatory.
If this was your goal, you shouldn't use german variablenames. (And where does zielort$ come from?)
jamirokwai
Enthusiast
Enthusiast
Posts: 798
Joined: Tue May 20, 2008 2:12 am
Location: Cologne, Germany
Contact:

Re: Create Folder-Tree

Post by jamirokwai »

HeX0R wrote:
jamirokwai wrote:Hi there,
Should be self explanatory.
If this was your goal, you shouldn't use german variablenames. (And where does zielort$ come from?)
Oh, yeah :-)

For your convenience, changed to dest$ ...
And thanks for the hint, regarding the error with zielort$ ...
Regards,
JamiroKwai
Post Reply