Enclosing Folder(s)

Share your advanced PureBasic knowledge/code with the community.
User avatar
Piero
Addict
Addict
Posts: 1044
Joined: Sat Apr 29, 2023 6:04 pm
Location: Italy

Enclosing Folder(s)

Post by Piero »

Code: Select all

; Multilevel Enclosing Folder (can optionally set Current Directory)
Procedure.s UpperDir(path$, level = 1, setcurrdir = #False)
   If FileSize(path$) = -1 : ProcedureReturn "" : EndIf
   Protected i, OLDcurrdir$ = GetCurrentDirectory()
   path$ = GetPathPart(path$) ; in case of file
   For i = 1 To level : path$ + ".." + #PS$ : Next
   SetCurrentDirectory(path$)
   path$ = GetCurrentDirectory()
   If Not setcurrdir : SetCurrentDirectory(OLDcurrdir$) : EndIf
   ProcedureReturn path$
EndProcedure

Debug UpperDir(GetHomeDirectory()) +~"\n\n------\n"
; Mac: /Users/
; Linux: /home/
; Win: C:\Users\
TEST.s = ProgramFilename()
Debug~"Testing:\n"+TEST+~"\n"
UpDir.s=UpperDir(TEST) : Dir.s=GetPathPart(TEST)
if UpDir<>Dir And UpDir
   Debug "Directory List:"
   While 1
      UpDir=UpperDir(TEST,i+1)
      If s.s<>UpDir
         Debug UpDir : s=UpDir
      Else : Break : EndIf
      i+1
   Wend
   Debug "Levels: "+i
Else
   Debug "SAME or INVALID!"
EndIf