How Can I Find a Folder Name?

So i know what the folders directory is, but its name will be different on each computer. I just need to get the folder name and put it into a string. Its size will be the same also (give or take 1 character possibly).
Last edited on
If this is for windows, the correct aproach is to use GetModuleFileName() followed by PathRemoveFileSpec().
http://msdn.microsoft.com/en-us/library/windows/desktop/ms683197%28v=vs.85%29.aspx
http://msdn.microsoft.com/en-us/library/windows/desktop/bb773748%28v=vs.85%29.aspx

This is a sample program from MSDN:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <windows.h>
#include <iostream.h>
#include "Shlwapi.h"

void main( void )
{
// Path to include file spec.
char buffer_1[ ] = "C:\\TEST\\sample.txt"; 
char *lpStr1;
lpStr1 = buffer_1;

// Print the path with the file spec.
cout << "The path with file spec is          : " << lpStr1 << endl;

// Call to "PathRemoveFileSpec".
PathRemoveFileSpec(lpStr1);

// Print the path without the file spec.
cout << "\nThe path without file spec is       : " << lpStr1 << endl;
}



OUTPUT:
==================
The path with file spec is : C:\TEST\sample.txt

The path without file spec is : C:\TEST


For *nix I think you can use getcwd(), but I am not sure if this correctly returns executable directory.
What do you mean by "folders directory"? "folder" is another name for "directory", in Windows-speak, so you know what the folder's folder is (or the directory's directory).

Is it an app specific folder you're talking about, which the user has selected. If so, you need to store the info somewhere.

Andy

GetCurrentDirectory() -- the Win32 equivalent to getcwd() -- returns the current working folder. This only the same as the exe path if you start the application from the folder where the exe is found (or if the exe uses SetCurrentDirectory() to set the working folder)

e.g.

cd c:\temp
c:\windows\system32\notepad


=> exe folder path is c:\windows\system32
=> working folder = c:\temp
Topic archived. No new replies allowed.