Current Directory

Everything else that doesn't fall into one of the other PB categories.
Polo
Addict
Addict
Posts: 2422
Joined: Tue May 06, 2003 5:07 pm
Location: UK

Current Directory

Post by Polo »

Hi !
Is it possible to get the current directory without using api ? Something which would be usable on linux and on windows ? I'm not sure if a PB function exist for this, i haven't see it :(
Thanks !
dracflamloc
Addict
Addict
Posts: 1648
Joined: Mon Sep 20, 2004 3:52 pm
Contact:

Post by dracflamloc »

Well I'm not sure what you are using the current directory for, but to access files int he current directory you can open them such as this: "./example.txt"
Polo
Addict
Addict
Posts: 2422
Joined: Tue May 06, 2003 5:07 pm
Location: UK

Post by Polo »

Wops :) It's working, thanks ! (haven't tested it on windows, but i guess it's ok :))
olejr
Enthusiast
Enthusiast
Posts: 152
Joined: Sun Jul 11, 2004 7:48 pm
Location: Lillehammer, No(r)way
Contact:

Post by olejr »

As dracflamloc said i'm not sure why you need to,
and I don't know about anything that is included in PB..
But it's not hard to do it with API either:

Drop this into your code, and it work on both Linux and Windows.. (Not Amiga, but you didn't ask for it so..)

Code: Select all

; Procedure that return CurrentDir as String
Procedure.s GetCurrentDirectory()
 currentdir.s = Space(255) ; <- 255 is defined as #MAX_PATH in Window, not Linux, oh well.

 CompilerIf #PB_Compiler_OS = #PB_OS_Linux
   getcwd_(@currendir, 255)
   If Right(currentdir, 1) <>"/" : currentdir + "/" : EndIf
 CompilerElse
   GetCurrentDirectory_(255, @CD)
   If Right(currentdir,1)<>"\" : currentdir + "\" : EndIf
 CompilerEndIf
 ProcedureReturn currentdir
EndProcedure
Polo
Addict
Addict
Posts: 2422
Joined: Tue May 06, 2003 5:07 pm
Location: UK

Post by Polo »

Thanks Olejr !! I'll use this if Dracflamloc's trick don't work on linux, but it's working on windows, so... :)
BTW, its for using with :
RunProgram

when using parameters, we must put the current directory (strange ??), but puting "./" on windows is working well and is probably the fast way, and i guess it works with linux too, i'll test tomorrow :)
olejr
Enthusiast
Enthusiast
Posts: 152
Joined: Sun Jul 11, 2004 7:48 pm
Location: Lillehammer, No(r)way
Contact:

Post by olejr »

Yes the "./" should also work on Linux..

Little tip:
You can also use "../" to get to the parent dir..
So if you need to go 2 dir's back and into some other it's like:
"../../path-to-what-ever"
Post Reply