program path$ or default dir$?

Just starting out? Need help? Post your questions and find answers here.
mikeschn
User
User
Posts: 11
Joined: Sat Jun 30, 2012 12:22 am

program path$ or default dir$?

Post by mikeschn »

I am writing a program called material.exe

if I drop it in a directory called c:\material

Is there a command that'll put that directory into a variable with out having to hard code it into the program or putting it into a config file?

ie. program_path$ = ???

or default_dir$ = ???

Thanks,

Mike...
infratec
Always Here
Always Here
Posts: 7583
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: program path$ or default dir$?

Post by infratec »

Hi,

Code: Select all

Debug GetPathPart(ProgramFilename())
Debug GetCurrentDirectory()
Debug GetTemporaryDirectory()
Debug GetHomeDirectory()
Choose the one you need :wink:

Bernd
mikeschn
User
User
Posts: 11
Joined: Sat Jun 30, 2012 12:22 am

Re: program path$ or default dir$?

Post by mikeschn »

GetCurrentDirectory() is the one I needed.

Thanks

Mike...
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Re: program path$ or default dir$?

Post by ts-soft »

mikeschn wrote:GetCurrentDirectory() is the one I needed.
Better use: GetPathPart(ProgramFilename())
This one is always the path to you're exe.
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
Little John
Addict
Addict
Posts: 4777
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: program path$ or default dir$?

Post by Little John »

Only

Code: Select all

GetPathPart(ProgramFilename())
reliably yields the directory for which mikeschn has asked in his first post.
mikeschn wrote:GetCurrentDirectory() is the one I needed.
Then you didn't describe your requirements properly in your first post.
mikeschn
User
User
Posts: 11
Joined: Sat Jun 30, 2012 12:22 am

Re: program path$ or default dir$?

Post by mikeschn »

I guess I'd better use

Code: Select all

GetPathPart(ProgramFilename()
since I do need the path to my exe file...

Thanks again!

Mike...
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Re: program path$ or default dir$?

Post by PB »

It's a false belief that GetCurrentDirectory() always returns the path of your exe.
It can change under certain specific circumstances. Use ProgramFilename() instead.
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
mikeschn
User
User
Posts: 11
Joined: Sat Jun 30, 2012 12:22 am

Re: program path$ or default dir$?

Post by mikeschn »

I just changed my program from

Code: Select all

CreateFile(4,GetCurrentDirectory()+struct_Filename$)
to

Code: Select all

CreateFile(4,GetPathPart(ProgramFilename())+struct_Filename$)
Now I find it drops the file in c:\users\Mike\AppData\Local

instead of c:\users\mike\documents\myprogram

I am not running from the .exe file. I am running from the Purebasic IDE.

Any thoughts why this is happening?

Thanks,

Mike...
User avatar
Demivec
Addict
Addict
Posts: 4260
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: program path$ or default dir$?

Post by Demivec »

mikeschn wrote:I just changed my program from

Code: Select all

CreateFile(4,GetCurrentDirectory()+struct_Filename$)
to

Code: Select all

CreateFile(4,GetPathPart(ProgramFilename())+struct_Filename$)
Now I find it drops the file in c:\users\Mike\AppData\Local

instead of c:\users\mike\documents\myprogram

I am not running from the .exe file. I am running from the Purebasic IDE.

Any thoughts why this is happening?
It happens because you are running a temporary file created by the IDE. Guess where the IDE's temp file was stored?

This will happen until you create an executible to run from a different location. If you already know this location you can use a predefined path to it while you are debugging the file and then replace it with 'GetPathPart(ProgramFilename())' just before you create the executible.
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Re: program path$ or default dir$?

Post by PB »

Demivec is right. Running from the IDE will give a different result to a standalone exe.

Here's how I handle it in my apps. This sets the global variables correctly no matter
if I run it with F5 from the IDE, or as a standalone executable when my app is done.
Just stick it at the top of your source and you're set; no further changes needed.

Code: Select all

Global appname$=ProgramFilename(),appdir$=GetPathPart(appname$)

CompilerIf #PB_Compiler_Debugger
  appdir$=#PB_Compiler_FilePath
  appname$=appdir$+"MyAppName.exe"
CompilerEndIf

Global appexe$=LCase(GetFilePart(appname$))
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
Post Reply