BarryG wrote: Sat Nov 23, 2024 1:31 amI wouldn't use GetCurrentDirectory() and/or SetCurrentDirectory() because that changes how your app runs, and you may just want to get a parent folder without changing the current directory used by your app.
My proc restores current dir by default (but you can also set it)
It can be useful to get the .app dir, at least on Mac (I just edited it to show how)
GetCurrentDirectory() returns the path of the calling process, for example if started from a batch file or another process. See below for proof. You can't 100% rely on it to return the exe's actual folder - you need ProgramFilename() for that.
Kiffi wrote: Sat Nov 23, 2024 2:30 amGetCurrentDirectory() returns the path from which your programme was called
No, it actually returns the path of the calling process, for example if started from a batch file or another process. See below for proof. You can't 100% rely on it to return the exe's actual folder - you need ProgramFilename() for that.
@Piero: Your code fails on Windows PCs for the same reason I showed in my screenshot above (it outputs C: instead of the correct D:). I know you said your code works on Mac, but it's definitely NOT cross-platform code (if anyone is hoping it is).
BarryG wrote: Sat Nov 23, 2024 6:42 am
@Piero: Your code fails on Windows PCs for the same reason I showed in my screenshot above (it outputs C: instead of the correct D:). I know you said your code works on Mac, but it's definitely NOT cross-platform code (if anyone is hoping it is).
Just tested on windows 10 and it works! Procedure UpperDir(path$, level = 1, setcurrdir.b = #False) posted above
Piero wrote: Sat Nov 23, 2024 7:07 amJust tested on windows 10 and it works!
No, it doesn't. You didn't test it like how I said. Here's your code that I used, and the result below. You can clearly see the MessageRequester is NOT showing "D:" as the upper folder - it's showing "C:" by mistake.
Procedure.s UpperDir(path$, level = 1, setcurrdir.b = #False)
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
MessageRequester("",UpperDir(GetCurrentDirectory()))
Yep; the problem was the path returned by GetCurrentDirectory in some non-standard situations, NOT my UpperDir procedure
I was about to sue you for harassment
Anyway, UpperDir also uses Get/SetCurrentDirectory internally; that makes the ".." magic happen
Procedure.s UpperDir(path$, level = 1, setcurrdir.b = #False)
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 GetCurrentDirectory()
Debug UpperDir(GetCurrentDirectory()) ; Does NOT change Current Directory
Debug GetCurrentDirectory()
Debug UpperDir(GetCurrentDirectory(), 2, 1) ; Goes up 2 "levels" and Changes Current Directory (1 = true)
Debug GetCurrentDirectory()
; Example usage: get .app folder (Mac)
Debug ".app folder on Mac: " + UpperDir(ProgramFilename(), 3)
; For Windows, Linux and Mac-console programs, you can use:
; GetPathPart(ProgramFilename())
Last edited by Piero on Sat Nov 23, 2024 10:38 am, edited 3 times in total.