Page 1 of 1

program path$ or default dir$?

Posted: Sat Jun 22, 2013 5:50 pm
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...

Re: program path$ or default dir$?

Posted: Sat Jun 22, 2013 5:58 pm
by infratec
Hi,

Code: Select all

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

Bernd

Re: program path$ or default dir$?

Posted: Sat Jun 22, 2013 6:25 pm
by mikeschn
GetCurrentDirectory() is the one I needed.

Thanks

Mike...

Re: program path$ or default dir$?

Posted: Sat Jun 22, 2013 6:29 pm
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.

Re: program path$ or default dir$?

Posted: Sat Jun 22, 2013 6:40 pm
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.

Re: program path$ or default dir$?

Posted: Sat Jun 22, 2013 8:32 pm
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...

Re: program path$ or default dir$?

Posted: Sun Jun 23, 2013 2:32 am
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.

Re: program path$ or default dir$?

Posted: Tue Jun 25, 2013 12:37 am
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...

Re: program path$ or default dir$?

Posted: Tue Jun 25, 2013 1:32 am
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.

Re: program path$ or default dir$?

Posted: Tue Jun 25, 2013 9:33 am
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$))