Page 1 of 1

File creation - Current directory problem

Posted: Wed Nov 10, 2010 4:56 pm
by Lebostein
Hello,

this code generates two small files:

Code: Select all

CreatePreferences("x.ini")
PreferenceGroup("test")
WritePreferenceString("name", "PB")
ClosePreferences()

CreateFile(0, "x.txt")
WriteString(0, "Hello")
CloseFile(0)
If I compile and run this code with IDE, the files in the same directory created as the source code. If I create an executable in the same directory an run this app, then I found the files in root directory of my system. What is wrong?

Re: File creation - Current directory problem

Posted: Wed Nov 10, 2010 5:07 pm
by ts-soft
you have to use a path like: GetPathPart(ProgramFilename()) or SetCurrentDirectory(GetPathPart(ProgramFilename()))
This is the same on all OS to make sure you use the right path.

Re: File creation - Current directory problem

Posted: Thu Nov 11, 2010 10:56 am
by Lebostein
OK. In directory

/Users/Lebostein/DATEN/PureBasic/TomyTex

I have two files

> Setting.ini
> TomyTex.pb


If I run TomyTex.pb with IDE, the program can open the ini file with simple OpenPreferences("Setting.ini") without problems. If I check the execution path with ProgramFilename() I get

/tmp/PureBasic0.app/Contents/MacOS/PureBasic0

Why my program finds the ini file? The execution path is an other!

However: If I generate an app an run this app, I get

/Users/Lebostein/DATEN/PureBasic/TomyTex/TomyTex.app/Contents/MacOS/TomyTex

with ProgramFilename(). The right path this time, but the program don't find the ini file...

Re: File creation - Current directory problem

Posted: Thu Nov 11, 2010 11:09 am
by ts-soft
Set the compileroptions to create exe in sourcedir! or you have self to difference exe and compile from IDE. the same problem on all OS.

greetings
thomas

Re: File creation - Current directory problem

Posted: Thu Nov 11, 2010 11:24 am
by Lebostein
ts-soft wrote:the same problem on all OS.
No!

With windows does not care if I

1) run source with IDE and exe creation in temp dir
2) run source with IDE and exe creation in source dir
3) run compiled exe in source dir

In all cases the program can open the ini file in source directory with simple OpenPreferences("Setings.ini"). In all cases the current directory = source directory.

With Mac OS, only 1) and 2) works (Your hint with the compiler options is not necessary). With 3), the current directory = root directory.

Is this a Mac specific problem/peculiarity, that a started app select the root directory as current directory (then I will find a workaround for Mac version of my program)? Or is this a problem of apps generated with PB only?

Re: File creation - Current directory problem

Posted: Thu Nov 11, 2010 11:48 am
by ts-soft
if you start a program from windows-shell (cmd), you have the same problem, currentdirectory is not set. only the most filemanager sets this on windows, but is not sure, the user start it with a filemanager. if you start from ide, the ide set the currentdirectory!
in your finish application you should always use the path from ProgramFileName(), on all OS!
(better not put settings in the same dir as program!)

greetings
Thomas

Re: File creation - Current directory problem

Posted: Thu Nov 11, 2010 12:15 pm
by Lebostein
Hm... you could have right.

But I have not seen a PB source code in my life (not in forum, not in example folder and not in code archiv), in which the current path was set...

Re: File creation - Current directory problem

Posted: Thu Nov 11, 2010 12:26 pm
by ts-soft
Lebostein wrote:Hm... you could have right.

But I have not seen a PB source code in my life (not in forum, not in example folder and not in code archiv), in which the current path was set...
i have posted a hint on this more than 10x in german-board :wink:
i use it in my applications!

Re: File creation - Current directory problem

Posted: Thu Nov 11, 2010 1:40 pm
by Lebostein
OK.

Fred should change the behaviour of ProgramFilename() on Mac. Now I get the full path to the binary:

/Users/Lebostein/PureBasic/Application/Application.app/Contents/MacOS/Application

What we need for a platform independet behaviour is

/Users/Lebostein/PureBasic/Application/Application.app

Workaround (not save):

Code: Select all

exe.s = ProgramFilename()
CompilerIf #PB_Compiler_OS = #PB_OS_MacOS: exe = Left(exe, FindString(exe, "/Contents/MacOS/", 1) - 1): CompilerEndIf
SetCurrentDirectory(GetPathPart(exe))

Re: File creation - Current directory problem

Posted: Thu Nov 11, 2010 10:28 pm
by michel51
Lebostein wrote:OK. In directory

/Users/Lebostein/DATEN/PureBasic/TomyTex

I have two files

> Setting.ini
> TomyTex.pb


If I run TomyTex.pb with IDE, the program can open the ini file with simple OpenPreferences("Setting.ini") without problems. If I check the execution path with ProgramFilename() I get

/tmp/PureBasic0.app/Contents/MacOS/PureBasic0

Why my program finds the ini file? The execution path is an other!

However: If I generate an app an run this app, I get

/Users/Lebostein/DATEN/PureBasic/TomyTex/TomyTex.app/Contents/MacOS/TomyTex

with ProgramFilename(). The right path this time, but the program don't find the ini file...
That's right, because the ini-file is not within the application-folder.
Try this: a right click on the compiled file (TomyTex.app) will open a menu list. There click on 'show contents' ("Paketinhalte zeigen"). In this folder open the the folder "contents". Copy your ini-file here in. Or into the MacOs folder.

Additional files are not automatically copied into the contents folder while compiling to .app :!:

Hope, it helps.

Re: File creation - Current directory problem

Posted: Fri Nov 12, 2010 4:42 am
by WilliamL
Hey michel51, how's it going?

You're right the files are in the Contents folder and if you have to update your files you can make a tool. I made a tool to re-save my (modified) plist over the default that is created every time the app is created.

@Lebostein

If you are having problems finding your files when you run from the IDE and from the app then you can define your path with an If/EndIf using the #PB_EditorCreateExecutable constant value. (the path will be different) There is a thread about how to do this ( http://www.purebasic.fr/english/viewtop ... 41&start=0 ) but I seem to remember that it didn't work quite right for me so I have my own routine.

Re: File creation - Current directory problem

Posted: Fri Nov 12, 2010 11:28 am
by PB
@Lebostein: Why put yourself through all this hassle and pain?
Make your code open the files using GetPathPart() for your app
as ts-soft said, and be done with it. You're just making artificial
problems for yourself at the moment.

And never use SetCurrentDirectory() because from what I've read
in the past, it can actually affect other apps. So they say.

Re: File creation - Current directory problem

Posted: Fri Nov 12, 2010 1:34 pm
by jamirokwai
WilliamL wrote:Hey michel51, how's it going?

You're right the files are in the Contents folder and if you have to update your files you can make a tool. I made a tool to re-save my (modified) plist over the default that is created every time the app is created.
Hi WilliamL,

are you willing to share this tools code??? :-)

Re: File creation - Current directory problem

Posted: Fri Nov 12, 2010 7:04 pm
by WilliamL
@pb - :?:

@jamirokwai
If you are asking me how to make a tool then I have to say that I do not. I asked in this thread http://www.purebasic.fr/english/viewtop ... hilit=tool how to automatically save my modified plist over the default plist when I created an executable. I had made a little program, with preferences, a window, and an event loop that copied the plist and/or resources to the newly created app. freak responded that I could just make the code a tool (in the IDE). As you can tell from my post I don't quite understand why it works but it appears that if you have code that does something, in this case copy one file over another one, you can make it a tool.