Paths and Filenames

This section details the functions and structures used for manipulating file paths and filenames in the Windows operating system. Understanding how to correctly construct, parse, and manipulate paths is crucial for robust file operations.

Core Concepts

Windows uses a hierarchical file system structure. Paths are strings that identify the location of a file or directory within this structure. Filenames refer to the actual name of the file or directory.

Path Delimiters

The primary path delimiter in Windows is the backslash (\). However, many APIs also accept the forward slash (/) for compatibility and convenience.

Drive Letters

Drives are typically identified by a drive letter followed by a colon (e.g., C:). Network shares are represented using UNC (Universal Naming Convention) paths, starting with two backslashes (e.g., \\ServerName\ShareName).

Key Functions

Manipulating Paths

Working with Filenames

Important Structures

WIN32_FIND_DATA

This structure is used by functions like FindFirstFile and FindNextFile to retrieve information about files and directories, including their names.

Member Description
dwFileAttributes File attributes.
cAlternateFileName The 8.3 filename.
cFileName The filename or directory name.

Best Practices


PathCombine

Combines two path components into a single path. This function is safer than manual string concatenation because it handles path delimiters correctly.


    LPTSTR PathCombine(
      LPTSTR  lpszDest,
      LPCTSTR lpszDir,
      LPCTSTR lpszFile
    );
        

Parameters:

Return Value: A pointer to the combined path string if successful, or NULL otherwise.

PathGetExtension

Retrieves the file extension from a path string.


    LPTSTR PathGetExtension(
      LPCTSTR pszPath
    );
        

Parameters:

Return Value: A pointer to the extension string (including the period) or an empty string if no extension is found.

PathGetFileName

Retrieves the filename and extension from a path string.


    LPTSTR PathGetFileName(
      LPCTSTR pszPath
    );
        

Parameters:

Return Value: A pointer to the filename string. If the path ends with a backslash, it returns a pointer to the last component before the backslash.

PathIsRelative

Determines if a path string is relative.


    BOOL PathIsRelative(
      LPCTSTR pszPath
    );
        

Parameters:

Return Value: TRUE if the path is relative, FALSE otherwise.

PathCanonicalize

Creates an absolute, canonical path from a given path. This function resolves relative path components (like "." and "..") and simplifies the path.


    BOOL PathCanonicalize(
      LPTSTR lpszCanonicalPath,
      LPCTSTR lpszPath
    );
        

Parameters:

Return Value: TRUE if successful, FALSE otherwise.

GetLongPathName

Retrieves the long path name for the specified path. Long path names can exceed the 260-character limit of the traditional MAX_PATH.


    DWORD GetLongPathName(
      LPCTSTR lpszShortPath,
      LPTSTR  lpszLongPath,
      DWORD   cchBuffer
    );
        

Parameters:

Return Value: The length of the string copied to lpszLongPath, in characters, not including the terminating null character. If the buffer is too small, it returns the required buffer size.

GetShortPathName

Retrieves the short (8.3) path name for the specified path.


    DWORD GetShortPathName(
      LPCTSTR lpszLongPath,
      LPTSTR  lpszShortPath,
      DWORD   cchBuffer
    );
        

Parameters:

Return Value: The length of the string copied to lpszShortPath, in characters, not including the terminating null character. If the buffer is too small, it returns the required buffer size.