Page 2 of 2
Re: Move back one directory
Posted: Sat Nov 23, 2024 2:11 am
by Piero
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)
Re: Move back one directory
Posted: Sat Nov 23, 2024 2:30 am
by Kiffi
Piero wrote: Sat Nov 23, 2024 2:11 amIt can be useful to get the .app dir
No, it doesn't.
GetCurrentDirectory() returns the path from which your programme was called.
Code: Select all
MessageRequester("", GetCurrentDirectory())
When I run the programm from "C:\Code\PureBasic\":
When I run the programm from "C:\Code\":

Re: Move back one directory
Posted: Sat Nov 23, 2024 4:07 am
by Piero
Kiffi wrote: Sat Nov 23, 2024 2:30 am
Piero wrote: Sat Nov 23, 2024 2:11 amIt can be useful to get the .app dir
No, it doesn't.
I think that the path to the .exe directory (cannot test on windows atm) must be something simpler, like:
The above also works for mac console programs (NOT .app packages) so should work for linux/win progs too
In case, try changing the level parameter of my
procedure
Re: Move back one directory
Posted: Sat Nov 23, 2024 6:26 am
by BarryG
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.
Code: Select all
MessageRequester("", GetCurrentDirectory()) ; NEVER do this to get your exe's folder!

Re: Move back one directory
Posted: Sat Nov 23, 2024 6:30 am
by DarkDragon
BarryG wrote: Sat Nov 23, 2024 6:26 am
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.
Code: Select all
MessageRequester("", GetCurrentDirectory()) ; NEVER do this to get your exe's folder!
Just as Kiffi said and showed

.
Re: Move back one directory
Posted: Sat Nov 23, 2024 6:32 am
by BarryG
Yeah, just edited my post when I realised, but you replied while I was editing. That's what I get for making speech balloons in my image.

Re: Move back one directory
Posted: Sat Nov 23, 2024 6:42 am
by BarryG
@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).
IT WORKS!
Posted: Sat Nov 23, 2024 7:07 am
by Piero
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
viewtopic.php?p=631136#p631136 GetCurrentDirectory() is used as parameter of UpperDir(), but could be any path
viewtopic.php?p=631141#p631141 GetPathPart(ProgramFilename())
Re: IT WORKS!
Posted: Sat Nov 23, 2024 7:54 am
by BarryG
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.
Code: Select all
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()))

Re: IT WORKS!
Posted: Sat Nov 23, 2024 8:13 am
by Piero
BarryG wrote: Sat Nov 23, 2024 7:54 am
Piero wrote: Sat Nov 23, 2024 7:07 amJust tested on windows 10 and it works!

No, it doesn't.
GetCurrentDirectory() is used as parameter of UpperDir(), but could be any path (I just added above)
Re: Move back one directory
Posted: Sat Nov 23, 2024 8:17 am
by BarryG
Okay, but in your original code you used this example, which is what I was saying was incorrect because it's unreliable (per my screenshot above):
Code: Select all
Debug UpperDir(GetCurrentDirectory())
Your code works fine if you DON'T use GetCurrentDirectory() as a parameter.
Re: Move back one directory
Posted: Sat Nov 23, 2024 9:40 am
by Piero
BarryG wrote: Sat Nov 23, 2024 8:17 amOkay
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
Code: Select all
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())
Re: Move back one directory
Posted: Sat Nov 23, 2024 9:43 am
by BarryG
Piero wrote: Sat Nov 23, 2024 9:40 amI was about to sue you for harassment

Haha, all good.
