Seite 1 von 1

ForceDirectories Neu

Verfasst: 07.01.2011 15:08
von ts-soft

Code: Alles auswählen

;======= MakeSureDirectoryPathExists replacement =====================
;  Author:                Thomas Schulz (ts-soft)
;  Date:                  January 07, 2011
;  Target OS:             Windows
;  Target Compiler:       Requires PureBasic 4.xx
;=====================================================================
; Unterschied zu oben genannter API:
; 
; Unicode kompatibel
; relative Pfade zum Programverzeichnis oder zum aktuellem Verzeichnis
; Umgebungsvariablen werden expandiert
;=====================================================================

Procedure ForceDirectories(dir.s, rel2program = #False)
  Protected.i Index = 2, count, i, length
  Protected.s actDir, dir2

  If Not dir : ProcedureReturn #False : EndIf
  length = ExpandEnvironmentStrings_(dir, @dir2, 0)
  If length
    dir2 = Space(length)
    If ExpandEnvironmentStrings_(dir, @dir2, length)
      dir = dir2
    EndIf
  EndIf
  dir = ReplaceString(dir, "/", "\")
  If Right(dir, 1) <> "\" : dir + "\" : EndIf
  If FileSize(dir) = -2 : ProcedureReturn #True : EndIf
  If Left(dir, 1) = "\"
    dir = Right(dir, Len(dir) -1)
  EndIf
  If Not Right(StringField(dir, 1, "\"), 1) = ":"
    If rel2program
      dir = GetPathPart(ProgramFilename()) + dir
    Else
      dir = GetCurrentDirectory() + dir
    EndIf
    If FileSize(dir) = -2 : ProcedureReturn #True : EndIf
  EndIf
  actDir = StringField(dir, 1, "\") + "\"
  count = CountString(dir, "\")
  For i = Index To count
    actDir + StringField(dir, i, "\") + "\"
    If FileSize(actDir) = -2 : Continue : EndIf
    CreateDirectory(actDir)
  Next
  If FileSize(dir) = -2 : ProcedureReturn #True : EndIf
EndProcedure

ForceDirectories("c:\aaa\bbb")
ForceDirectories("settings", 1)
ForceDirectories("settings")
ForceDirectories("%APPDATA%\ts-soft\goodgame")
Ich hoffe mal, es kann jemand gebrauchen.

Gruß
Thomas