Move back one directory

Just starting out? Need help? Post your questions and find answers here.
User avatar
Piero
Addict
Addict
Posts: 1040
Joined: Sat Apr 29, 2023 6:04 pm
Location: Italy

Re: Move back one directory

Post 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)
User avatar
Kiffi
Addict
Addict
Posts: 1509
Joined: Tue Mar 02, 2004 1:20 pm
Location: Amphibios 9

Re: Move back one directory

Post 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\":
Image

When I run the programm from "C:\Code\":
Image
Hygge
User avatar
Piero
Addict
Addict
Posts: 1040
Joined: Sat Apr 29, 2023 6:04 pm
Location: Italy

Re: Move back one directory

Post 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:

Code: Select all

GetPathPart(ProgramFilename())
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
Last edited by Piero on Sat Nov 23, 2024 6:47 am, edited 1 time in total.
BarryG
Addict
Addict
Posts: 4219
Joined: Thu Apr 18, 2019 8:17 am

Re: Move back one directory

Post 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!

Image
Last edited by BarryG on Sat Nov 23, 2024 6:31 am, edited 1 time in total.
DarkDragon
Addict
Addict
Posts: 2347
Joined: Mon Jun 02, 2003 9:16 am
Location: Germany
Contact:

Re: Move back one directory

Post 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!

Image
Just as Kiffi said and showed 😉.
bye,
Daniel
BarryG
Addict
Addict
Posts: 4219
Joined: Thu Apr 18, 2019 8:17 am

Re: Move back one directory

Post 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. :lol:
BarryG
Addict
Addict
Posts: 4219
Joined: Thu Apr 18, 2019 8:17 am

Re: Move back one directory

Post 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).
User avatar
Piero
Addict
Addict
Posts: 1040
Joined: Sat Apr 29, 2023 6:04 pm
Location: Italy

IT WORKS!

Post 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! :x
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())
Last edited by Piero on Sat Nov 23, 2024 8:12 am, edited 1 time in total.
BarryG
Addict
Addict
Posts: 4219
Joined: Thu Apr 18, 2019 8:17 am

Re: IT WORKS!

Post by BarryG »

Piero wrote: Sat Nov 23, 2024 7:07 amJust tested on windows 10 and it works! :x
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()))

Image
User avatar
Piero
Addict
Addict
Posts: 1040
Joined: Sat Apr 29, 2023 6:04 pm
Location: Italy

Re: IT WORKS!

Post 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! :x
No, it doesn't.
GetCurrentDirectory() is used as parameter of UpperDir(), but could be any path (I just added above)
BarryG
Addict
Addict
Posts: 4219
Joined: Thu Apr 18, 2019 8:17 am

Re: Move back one directory

Post 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.
User avatar
Piero
Addict
Addict
Posts: 1040
Joined: Sat Apr 29, 2023 6:04 pm
Location: Italy

Re: Move back one directory

Post 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 :D

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())
Last edited by Piero on Sat Nov 23, 2024 10:38 am, edited 3 times in total.
BarryG
Addict
Addict
Posts: 4219
Joined: Thu Apr 18, 2019 8:17 am

Re: Move back one directory

Post by BarryG »

Piero wrote: Sat Nov 23, 2024 9:40 amI was about to sue you for harassment :D
Haha, all good. :)
Post Reply