Multi level subdirectory creation

Share your advanced PureBasic knowledge/code with the community.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Multi level subdirectory creation

Post by BackupUser »

Code updated For 5.20+

Restored from previous forum. Originally posted by Fangbeast.

The experts all have their ways of doing things but nonetheless, here is my quickie for making multi level subdirectories.

Always pass it a fully qualified path which includes the drive name, the path and the filename. Modify it to suit yourself but I use this as part of a file "DIFF" compare and copy routine.

Code: Select all

filestring.s = "E:\level 1\level 2\level 3\level 4\level 5\humongous file.exe"

Procedure.s MakeDirectory(filestring.s)

  filename.s  = GetFilePart(filestring)
  directory.s = GetPathPart(filestring)
  drivename.s = Left(directory, 3)
  directory.s = Mid(directory, 4, Len(directory) - 3)
  make.s      = drivename

  While FindString(directory,"\", 1) <> 0
    position = FindString(directory,"\", 1)
    temp.s = Left(directory, position -1)
    directory = Mid(directory, position + 1, Len(directory) - position)
    make + temp + "\"
    CreateDirectory(make)
  Wend

  ProcedureReturn make

EndProcedure

filepath.s = MakeDirectory(filestring)
Just a quick explanation...I returned the filepath created because I copy files to that location and I didn't want to have a routine working out paths twice..one to make it, two to work it out again:):)

Hope it helps someone, it did me :):)

We are Dyslexic of Borg, prepare to have your ass laminated!
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Justin.

Using stringfield() is a lot easier:

Code: Select all

;Multilevel directory creation
;dir$    : directory to create with or without ending backslash
;Returns : directory created with ending backslash
procedure.s CreateDirectoryRec(dir$)
  i=1
  repeat
    st$=stringfield(dir$,i,"\")
    if st$""
      fdir$=fdir$ + st$ + "\"
      ;CreateDirectory_(fdir$,#null) ;windows api version
      CreateDirectory(fdir$)
    endif
    i+1
  until st$=""
  procedurereturn fdir$
endprocedure

BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Fangbeast.
Originally posted by Justin

Using stringfield() is a lot easier:
Maybe so, but it never occurred to me and I always use the first idea which works so I don't spend days debugging my results :):) (And I make some great ones!!!)

ROFL

We are Dyslexic of Borg, prepare to have your ass laminated!
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by fred.

Nice tip !

Fred - AlphaSND
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Justin.

I like more this one, same as before but will produce a smaller exe :)

Code: Select all

procedure.s CreateDirectoryRec2(dir$)
  if right(dir$,1)"\" : dir$=dir$ + "\" : endif
  repeat
    p=findstring(dir$,"\",p+1)
    if p0
      fdir$=left(dir$,p)
      CreateDirectory_(fdir$,#null)
    endif
  until p=0
  procedurereturn dir$
endprocedure
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Fangbeast.

I'll stick with my method or I will never get any sleep!!! (grin)

We are Dyslexic of Borg, prepare to have your ass laminated!
Post Reply