GetCurrentDirectory() - cross-platform

Share your advanced PureBasic knowledge/code with the community.
Straker
Enthusiast
Enthusiast
Posts: 701
Joined: Wed Apr 13, 2005 10:45 pm
Location: Idaho, USA

GetCurrentDirectory() - cross-platform

Post by Straker »

Code updated For 5.20+ (same As GetCurrentDirectory())

I take no credit for this. This is ts-soft's code that I found in the Feature Requests forum, but too many people are still looking for it.

Code: Select all

Procedure.s GetCurrentDir()
  Protected CurDir.s
  CurDir.s = Space(2048)
  CompilerIf #PB_Compiler_OS = #PB_OS_Linux
    getcwd_(CurDir, 2048)
  CompilerElse
    GetCurrentDirectory_(2048,CurDir)
  CompilerEndIf
  ProcedureReturn CurDir
EndProcedure

Debug GetCurrentDir()
The original thread is here.
User avatar
Paul
PureBasic Expert
PureBasic Expert
Posts: 1285
Joined: Fri Apr 25, 2003 4:34 pm
Location: Canada
Contact:

Post by Paul »

One year earlier than above code...
viewtopic.php?t=13904&highlight=getcwd
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Post by PB »

The FAQ says to use this:

Code: Select all

a$=Space(999) : GetModuleFileName_(0,@a$,999) : appdir$=GetPathPart(a$)
Why? Because you're definitely getting the correct path to the exe's folder.

I had problems using GetCurrentDirectory when booting up: my app was set
to run at startup and using GetCurrentDirectory was returning the wrong path.
As such, I've used the FAQ method ever since, with 100% success. FYI.

Also, don't forget that GetCurrentDirectory will CHANGE if your app calls the
SetCurrentDirectory command! Another reason to use the FAQ method instead.
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
Straker
Enthusiast
Enthusiast
Posts: 701
Joined: Wed Apr 13, 2005 10:45 pm
Location: Idaho, USA

Post by Straker »

@Paul. Thanks - but I was merely trying to make this entry in the Tips forum where it belongs.

@PB - You are right. This should not be confused with a "GetExeDirectory" or "GetApplicationPath" type function. This gets the currently set directory in the OS, even if your EXE is in a different directory. This is still a valid function. This posting was not intended to answer the FAQ you reference:

How do I know which folder my app was run from?
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Post by PB »

> This posting was not intended to answer the FAQ

Sorry, my bad.
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
Dare2
Moderator
Moderator
Posts: 3321
Joined: Sat Dec 27, 2003 3:55 am
Location: Great Southern Land

Post by Dare2 »

hehe. I fell in the GetCurrentDirectory hole in front of *too many* users with a new install. :oops: So a heads-up is useful!

However, that is a good tip.

It could also be used for the Application folder (using the *nix version of GetModuleFileName, whatever that is) or for a lot of things, actually.

Thanks.
@}--`--,-- A rose by any other name ..
Post Reply