Get Current Directory

Share your advanced PureBasic knowledge/code with the community.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Paul.

Anyone looking for a way to find the current working directory, here's what I use (result is returned in "curdir$") ...

Code: Select all

*buffer = GlobalAlloc_(#GMEM_FIXED | #GMEM_ZEROINIT, 10000)
GetCurrentDirectory_(64,*buffer)
curdir$=peekS(*buffer)
GlobalFree_(*buffer)
If anyone has a better way of doing this, let me know. To be honest, I don't quite understand how this works... I did a lot of guessing.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Mr.Skunk.

*buffer if a pointer to a memory block which you alloc using GlobalAlloc_.
The memory you alloc is filled with zero values(#GMEM_ZEROINIT), because when you alloc memory it does not initialise the memory, just reserving place for you. So the memory you alloc can contain random values.
10000 is the number of bytes you want to allocate.

GetCurrentDirectory_ if the API call to get the directory where the application was launched.
64 is the length of the buffer you want for that string. If the length of the string GetCurrentDirectory have to return is longer than the length you specify, the string is not returned.
In your example you put 10000 bytes to the buffer and 64 bytes to the length of the string. The two values normally have to be the same (not a real problem as you free the buffer at the end of your listing).

If you need more explanations, feel free to ask

Mr Skunk
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Paul.

Thanks for the explanation Skunk. It makes sense to me now.
One question... how would I determine how many bytes I should reserve?
For example, how many bytes would the string C:\TEST need?

I guess if I'm getting good results with 64, I could also change the 10000 to 64 so they match.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Mr.Skunk.

If the space is not large enough, the GetCurrentDirectory return the size you should use for your buffer instead of the string.
So if your buffer has value in it, means that this value must be the buffer size, and you have to delete your old buffer, create a new one with the right size and recall the GetCurentDirectory.
But if you free your buffer immediatly after retrieving the current directory, you can also specify a buffer large enough (1000 think is a big value), to avoid length errors.

c:\test will need 8 Bytes : 7 Bytes for the string a 1 Byte for the '0'(zero) to end the string

Mr Skunk

Edited by - mr.skunk on 20 June 2001 14:57:23
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by TronDoc.

please see my "GetCurDir" topic..
..I really think this needs to be
a part of PureBASIC so it can be
used in all OSes and
not worked-around.
{along with KillDir()}

even Linux has cwd and pwd
in the various shells....
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by horst.
Anyone looking for a way to find the current working directory, here's what I use (result is returned in "curdir$") ...

Code: Select all

*buffer = GlobalAlloc_(#GMEM_FIXED | #GMEM_ZEROINIT, 10000)
GetCurrentDirectory_(64,*buffer)
curdir$=peekS(*buffer)
GlobalFree_(*buffer)
If anyone has a better way of doing this, let me know. To be honest, I don't quite understand how this works... I did a lot of guessing.

What's wrong with this one:

currdir.s = space(256)
GetCurrentDirectory_(256,currdir)
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Paul.

Nothing wrong with it at all...
but at the time this was written, it was before anyone built the Space() or Space$() command. :)

(it was also good to learn about pointers in PB)
Post Reply