• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 261
  • Last Modified:

How to get long directory & file name?

FindFirstFile() only returns a long file name, without directory.

As to GetFullPathName()

DWORD GetFullPathName(
LPCTSTR lpFileName,
DWORD nBufferLength,
LPTSTR lpBuffer, // address of path buffer
LPTSTR *lpFilePart // address of filename in path
);

I find if lpFileName has 8.3 format filename, lpbuffer & lpFilePart have; if lpFileName has long filename, they have long filenames too.

So how to get long directory & file name?

P.S. At first, I searched in PAQ. I found the qestion, but there were no real solutions.
0
henry3
Asked:
henry3
  • 3
  • 2
1 Solution
 
WynCommented:
>>FindFirstFile() only returns a long file name, without directory.

Who tell you this?
FindFirstFile also return the 8.3 format file name in its
Here excerpt :
WIN32_FILE_DATE:
If a file has a long filename, the complete name appears in the cFileName field, and the 8.3 format truncated version of the name appears in the cAlternateFileName field. Otherwise, cAlternateFileName is empty. As an alternative, you can use the GetShortPathName function to find the 8.3 format version of a filename.

Thus ,you can use this to always obtain short name:

if(strlen(w32fd.cAlternateFileName)==0)
shortname=w32fd.cFileName;
else
shortname=w32fd.cAlternateFileName;

Regards
Wyn


 
0
 
henry3Author Commented:
What? Do you understand me?
I want to get LONG file name, and including LONG full path name, instead of short one.

>>FindFirstFile() only returns a long file name, without directory.
>Who tell you this?
Can you get the long directory name?
It only returns a FILE name.
e.g.
"c:\program file\1234567890.txt"
cFileName = "1234567890.txt"
I have a "c:\progra~1\123456~1.txt" string, I wanna get long one above.
0
 
abdijCommented:
Hi,
 Hope this is what you wanted.
The Code here first finds a text file from the programm files\\addremeove directory (you can change it to vary (FindNextFile() etc) to get the long file name. Then i use the get long directory path including the file name using the GetFullPathNa,e() function.

The key to get the long file name is       strcpy(szLongFileName,lpFindFileData->cFileName);

The GetFullPathName() automatically gives the long path+file name.

      HANDLE                        hFile                  = NULL;
      LPWIN32_FIND_DATA      lpFindFileData      = new WIN32_FIND_DATA();
      LPTSTR                        lpFileName            = new char[MAX_PATH];
      DWORD                        dwError                  = 0;
      char                        szLongFileName[MAX_SIZE];
      char                        szLongDirPath[MAX_SIZE];
      char                        szFilePart[MAX_SIZE];

      strcpy(lpFileName,"d:\\Program Files\\AddRemove\\*.txt");

      hFile  = FindFirstFile(lpFileName,lpFindFileData);
      if( (hFile = INVALID_HANDLE_VALUE) && ((dwError = GetLastError()) !=ERROR_SUCCESS))
      {
            AfxMessageBox("Error");
      }

      strcpy(szLongFileName,lpFindFileData->cFileName);

      dwError = GetFullPathName((LPCTSTR) szLongFileName,(DWORD)MAX_SIZE, (LPTSTR)szLongDirPath, (LPTSTR*)&szFilePart);

      if(lpFindFileData)
            delete lpFindFileData;
      if(lpFileName)
            delete lpFileName;

Bye for now.
Feel free to query
Abdij
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
abdijCommented:
Hi if you want the demo project feel free to ask. My mailid is abdij_b@hotmail.com.

Sorry for thr Typo mistakes in the answer. Please bear.

Abdij
0
 
henry3Author Commented:
I'll e-mail you to further discussion.
Thanks.
0
 
abdijCommented:
Hi,
 You are welcome.
Bye
Abdij
abdij_b@hotmail.com
0

Featured Post

Take Control of Web Hosting For Your Clients

As a web developer or IT admin, successfully managing multiple client accounts can be challenging. In this webinar we will look at the tools provided by Media Temple and Plesk to make managing your clients’ hosting easier.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now