
This section provides the definitions for path functions.
LPTSTR PathAddBackslash(
LPCTSTR lpszPath
);
Adds a backslash to the end of a string to create the correct syntax for a path. If the source path already has a trailing backslash, no backslash will be added.
Example:
#include <windows.h>
#include <iostream.h>
#include "Shlwapi.h"
void main( void )
{
// String for path name without backslash.
char buffer_1[] = "C:\\dir_name\\dir_name\\file_name";
char *lpStr1;
lpStr1 = buffer_1;
// String for path name with backslash.
char buffer_2[] = "C:\\dir_name\\dir_name\\file_name\\";
char *lpStr2;
lpStr2 = buffer_2;
cout << "The original path string 1 is " << lpStr1 << endl;
cout << "The modified path string 1 is "
<< PathAddBackslash(lpStr1) << lpStr1 << endl;
cout << "\nThe original path string 2 is " << lpStr2 << endl;
cout << "The modified path string 2 is "
<< PathAddBackslash(lpStr2) << lpStr2 << endl;
}
OUTPUT:
---------
The original path string 1 is C:\dir_name\dir_name\file_name
The modified path string 1 is C:\dir_name\dir_name\file_name\
The original path string 2 is C:\dir_name\dir_name\file_name\
The modified path string 2 is C:\dir_name\dir_name\file_name\
BOOL PathAddExtension(
LPTSTR pszPath,
LPCTSTR pszExtension
);
Adds a file extension to a string.
If there is already a file extension present, no extension will be added. If the path is a null string, the result will be the file extension only.
Example:
#include <windows.h>
#include <iostream.h>
#include "Shlwapi.h"
void main( void )
{
// String for path name without file extension.
char buffer_1[] = "file";
char *lpStr1;
lpStr1 = buffer_1;
// String for path name with file extension.
char buffer_2[] = "file.doc";
char *lpStr2;
lpStr2 = buffer_2;
// String for extension name.
char F_Ext[] = ".txt";
char *lpStr3;
lpStr3 = F_Ext;
// Null string as path.
char N_String[] = "\0";
char *lpStr4;
lpStr4 = N_String;
// Path 1 without the file extension.
cout << "The original path string 1 is " << lpStr1 << endl;
int ret_1 = PathAddExtension(lpStr1,lpStr3);
cout << "The modified path string 1 is " << lpStr1 << endl;
// Path 2 with the file extension already there.
cout << "The original path string 2 is " << lpStr2 << endl;
int ret_2 = PathAddExtension(lpStr2,lpStr3);
cout << "The modified path string 2 is " << lpStr2<< endl;
// Path 3 null string as a path.
int ret_3 = PathAddExtension(lpStr4,lpStr3);
cout << "The return value is " << ret_3<< endl;
cout << "The modified path on a null string is " << lpStr4<< endl;
int ret_4 = PathAddExtension("sample","");
cout << "The return value is " << ret_4<< endl;
if(ret_4 != 1)
{cout << "The PathAddExtension failed" << endl;}
else {cout << "The PathAddExtension succeeded" << endl;}
}
OUTPUT:
-----------------------
The original path string 1 is file
The modified path string 1 is file.txt
The original path string 2 is file.doc
The modified path string 2 is file.doc
The return value is 1
The modified path on a null string is .txt
The return value is 1
The PathAddExtension succeeded
BOOL PathAppend(
LPTSTR pPath,
LPCTSTR pMore
);
Appends one path to the end of another.
Example:
#include <windows.h>
#include <iostream.h>
#include "Shlwapi.h"
void main( void )
{
// String for path name.
char buffer_1[] = "name_1\\name_2";
char *lpStr1;
lpStr1 = buffer_1;
// String of what is being added.
char buffer_2[] = "name_3";
char *lpStr2;
lpStr2 = buffer_2;
cout << "The original path string is " << lpStr1 << endl;
cout << "The part to append to end is " << lpStr2 << endl;
bool ret = PathAppend(lpStr1,lpStr2);
cout << "The appended path string is " << lpStr1 << endl;
}
OUTPUT:
---------
The original path string is name_1\name_2
The part to append to end is name_3
The appended path string is name_1\name_2\name_3
LPTSTR PathBuildRoot(
LPTSTR szRoot,
int iDrive
);
Creates a root path from a given drive number.
Example:
#include <windows.h>
#include <iostream.h>
#include "Shlwapi.h"
void main( void )
{
// String for root name
char buffer_1[4];
char *lpStr1;
lpStr1 = buffer_1;
// Generate a root path based on the drive number (0/25)
// "Example: 0=A: to 25=Z:"
cout << "The root path for 0 is " << PathBuildRoot(lpStr1,0) << endl;
cout << "The root path for 1 is " << PathBuildRoot(lpStr1,1) << endl;
cout << "The root path for 25 is " << PathBuildRoot(lpStr1,25) << endl;
}
OUTPUT:
---------------
The root path for 0 is A:\
The root path for 1 is B:\
The root path for 25 is Z:\
BOOL PathCanonicalize(
LPTSTR lpszDst,
LPCTSTR lpszSrc
);
Canonicalizes a path.
This function allows the user to specify what to remove from a path by inserting special character sequences into the path. The ".." sequence indicates to remove the path part from the current position to the previous path part. The "." sequence indicates to skip over the next path part to the following path part. The root part of the path cannot be removed.
Example:
#include <windows.h>
#include <iostream.h>
#include "Shlwapi.h"
void main( void )
{
// Path_1 destination buffer.
char buffer_1[] = "JustABufferToHoldTheCanonicalizedPathForAnExample";
char *lpStr1;
lpStr1 = buffer_1;
// Path_2 to be Canonicalized.
char buffer_2[] = "A:\\name_1\\.\\name_2\\..\\name_3";
char *lpStr2;
lpStr2 = buffer_2;
// Path_3 to be Canonicalized.
char buffer_3[] = "A:\\name_1\\..\\name_2\\.\\name_3";
char *lpStr3;
lpStr3 = buffer_3;
// Path_4 to be Canonicalized.
char buffer_4[] = "A:\\name_1\\name_2\\.\\name_3\\..\\name_4";
char *lpStr4;
lpStr4 = buffer_4;
// Path_5 to be Canonicalized.
char buffer_5[] = "A:\\name_1\\.\\name_2\\.\\name_3\\..\\name_4\\..";
char *lpStr5;
lpStr5 = buffer_5;
// Path_6 to be Canonicalized.
char buffer_6[] = "C:\\..";
char *lpStr6;
lpStr6 = buffer_6;
cout << "The un-canonicalized path 2 is : " << lpStr2
<< "\nThe return value is : "
<< PathCanonicalize(lpStr1,lpStr2)
<< "\nThe canonicalized path 1 is : " << lpStr1 << endl;
cout << "\nThe un-canonicalized path 3 is : " << lpStr3
<< "\nThe return value is : "
<< PathCanonicalize(lpStr1,lpStr3)
<< "\nThe canonicalized path 1 is : " << lpStr1 << endl;
cout << "\nThe un-canonicalized path 4 is : " << lpStr4
<< "\nThe return value is : "
<< PathCanonicalize(lpStr1,lpStr4)
<< "\nThe canonicalized path 1 is : " << lpStr1 << endl;
cout << "\nThe un-canonicalized path 5 is : " << lpStr5
<< "\nThe return value is : "
<< PathCanonicalize(lpStr1,lpStr5)
<< "\nThe canonicalized path 1 is : " << lpStr1 << endl;
cout << "\nThe un-canonicalized path 6 is : " << lpStr6
<< "\nThe return value is : "
<< PathCanonicalize(lpStr1,lpStr6)
<< "\nThe canonicalized path 1 is : " << lpStr1 << endl;
}
OUTPUT:
---------
The un-canonicalized path 2 is : A:\name_1\.\name_2\..\name_3
The return value is : 1
The canonicalized path 1 is : A:\name_1\name_3
The un-canonicalized path 3 is : A:\name_1\..\name_2\.\name_3
The return value is : 1
The canonicalized path 1 is : A:\name_2\name_3
The un-canonicalized path 4 is : A:\name_1\name_2\.\name_3\..\name_4
The return value is : 1
The canonicalized path 1 is : A:\name_1\name_2\name_4
The un-canonicalized path 5 is : A:\name_1\.\name_2\.\name_3\..\name_4\..
The return value is : 1
The canonicalized path 1 is : A:\name_1\name_2
The un-canonicalized path 6 is : C:\..
The return value is : 1
The canonicalized path 1 is : C:\
PathCombine(
LPTSTR lpszDest,
LPCTSTR lpszDir,
LPCTSTR lpszFile
);
Concatenates two strings that represent properly formed paths into one path, as well as any relative path pieces.
The directory path should be in the form of A:,B:, ..., Z:. The file path should be in a correct form that represents the file part of the path. The file path must not be null, and if it ends with a backslash, the backslash will be maintained.
Example:
#include <windows.h>
#include <iostream.h>
#include "Shlwapi.h"
void main( void )
{
// Buffer to hold combined path.
char buffer_1[] = "";
char *lpStr1;
lpStr1 = buffer_1;
// String for balance of path name.
char buffer_2[] = "One\\Two\\Three";
char *lpStr2;
lpStr2 = buffer_2;
// String for directory name.
char buffer_3[] = "C:";
char *lpStr3;
lpStr3 = buffer_3;
cout << "The file path to be combined is "
<< lpStr2<< endl;
cout << "The directory name path is "
<< lpStr3<< endl;
cout << "The combined path is "
<< PathCombine(lpStr1,lpStr3,lpStr2) << endl;
}
------------
INPUT:
------------
Path for directory part: "C:"
Path for file part: "One\Two\Three"
------------
OUTPUT:
------------
The file path to be combined is One\Two\Three
The directory name path is C:
The combined path is C:\One\Two\Three
BOOL PathCompactPath(
HDC hDC,
LPTSTR lpszPath,
UINT dx
);
Truncates a file path to fit within a given pixel width by replacing path components with ellipses.
This function uses the font currently selected in hDC to calculate the width of the text. This function will not compact the path beyond the base file name preceded by ellipses.
Example:
#include <windows.h>
#include <iostream.h>
#include "Shlwapi.h"
HDC hdc; /* display DC handle for current font metrics */
void main( void )
{
// String path name 1.
char buffer_1[] = "C:\\path1\\path2\\sample.txt";
char *lpStr1;
lpStr1 = buffer_1;
// String path name 2.
char buffer_2[] = "C:\\path1\\path2\\sample.txt";
char *lpStr2;
lpStr2 = buffer_2;
// String path name 3.
char buffer_3[] = "C:\\path1\\path2\\sample.txt";
char *lpStr3;
lpStr3 = buffer_3;
// String path name 4.
char buffer_4[] = "C:\\path1\\path2\\sample.txt";
char *lpStr4;
lpStr4 = buffer_4;
// Variable to get the return from "PathCompactPath".
int retval;
cout << "The un-truncated path is " << lpStr1 << endl;
retval = PathCompactPath(hdc,lpStr1,125);
cout << "The truncated path at 125 pixels is : " << lpStr1 << endl;
retval = PathCompactPath(hdc,lpStr2,120);
cout << "The truncated path at 120 pixels is : " << lpStr2 << endl;
retval = PathCompactPath(hdc,lpStr3,110);
cout << "The truncated path at 110 pixels is : " << lpStr3 << endl;
retval = PathCompactPath(hdc,lpStr4,25);
cout << "The truncated path at 25 pixels is : " << lpStr4 << endl;
}
OUTPUT:
===========
The un-truncated path is C:\path1\path2\sample.txt
The truncated path at 125 pixels is : C:\path1\...\sample.txt
The truncated path at 120 pixels is : C:\pat...\sample.txt
The truncated path at 110 pixels is : C:\p...\sample.txt
The truncated path at 25 pixels is : ...\sample.txt
BOOL PathCompactPathEx(
LPTSTR pszOut,
LPCTSTR pszSrc,
UINT cchMax,
DWORD dwFlags
);
Truncates a path to fit within a certain number of characters by replacing path components with ellipses.
int PathCommonPrefix(
LPCTSTR pszFile1,
LPCTSTR pszFile2,
LPTSTR pszPath
);
Compares two paths to determine if they share a common prefix. A prefix is one of these types: "C:\\", ".", "..", "..\\".
Example:
#include <windows.h>
#include <iostream.h>
#include "Shlwapi.h"
void main( void )
{
// String path name 1.
char buffer_1[] = "C:\\win\\desktop\\temp.txt";
char *lpStr1;
lpStr1 = buffer_1;
// String path name 2.
char buffer_2[] = "c:\\win\\tray\\sample.txt";
char *lpStr2;
lpStr2 = buffer_2;
// String path out buffer.
char buffer_3[] = "abcdef";
char *lpStr3;
lpStr3 = buffer_3;
// Variable to get the return.
// from "PathCommonPrefix"
int retval;
retval = PathCommonPrefix(lpStr1,lpStr2,lpStr3);
cout << "The contents of String 1: " << lpStr1 << endl;
cout << "The contents of String 2: " << lpStr2 << endl;
cout << "The length of the output buffer is : "
<< retval << endl;
cout << "The contents of Output buffer: " << lpStr3 << endl;
}
OUTPUT:
-----------
The contents of String 1: C:\win\desktop\temp.txt
The contents of String 2: c:\win\tray\sample.txt
The length of the output buffer is : 6
The contents of Output buffer: C:\win
BOOL PathFileExists(
LPCTSTR lpszPath
);
Determines if a file exists.
This function will test for the validity of the file and path.
Example:
#include <windows.h>
#include <iostream.h>
#include "Shlwapi.h"
void main( void )
{
// Valid file path name (file is there).
char buffer_1[] = "C:\\TEST\\file.txt";
char *lpStr1;
lpStr1 = buffer_1;
// Invalid file path name (file is not there).
char buffer_2[] = "C:\\TEST\\file.doc";
char *lpStr2;
lpStr2 = buffer_2;
// Return value from "PathFileExists".
int retval;
// Search for the presence of a file with a true result.
retval = PathFileExists(lpStr1);
if(retval == 1)
{
cout << "Search for the file path of : " << lpStr1 << endl;
cout << "The file requested \"" << lpStr1 << "\" is a valid file" << endl;
cout << "The return from function is : " << retval << endl;
}
else{
cout << "\nThe file requested " << lpStr1 << " is not a valid file" << endl;
cout << "The return from function is : " << retval << endl;
}
// Search for the presence of a file with a false result.
retval = PathFileExists(lpStr2);
if(retval == 1)
{
cout << "\nThe file requested " << lpStr2 << "is a valid file" << endl;
cout << "Search for the file path of : " << lpStr2 << endl;
cout << "The return from function is : " << retval << endl;
}
else{
cout << "\nThe file requested \"" << lpStr2 << "\" is not a valid file" << endl;
cout << "The return from function is : " << retval << endl;
}
}
OUTPUT
==============
Search for the file path of : C:\TEST\file.txt
The file requested "C:\TEST\file.txt" is a valid file
The return from function is : 1
The file requested "C:\TEST\file.doc" is not a valid file
The return from function is : 0
LPTSTR PathFindExtension(
LPCTSTR pPath
);
Searches a path for an extension.
LPTSTR PathFindFileName(
LPCTSTR pPath
);
Searches a path for a file name.
Example:
#include <windows.h>
#include <iostream.h>
#include "Shlwapi.h"
void main( void )
{
// Path that contains a file part.
char buffer_1[] = "c:\\path\\file";
char *lpStr1;
lpStr1 = buffer_1;
cout << "Search for the file in path " << lpStr1
<< "\nReturns the file part of the path \""
<< PathFindFileName(lpStr1) << "\"" << endl;
cout << "\nSearch for the file in path \"c:\\path\"" << endl;
cout << "Returns the file part of the path \""
<< PathFindFileName("c:\\path") << "\"" << endl;
cout << "\nSearch for the file in path \"c:\\path\\\"" << endl;
cout << "Returns the file part of the path \""
<< PathFindFileName("c:\\path\\") << "\"" << endl;
cout << "\nSearch for the file in path \"c:\\\"" << endl;
cout << "Returns the file part of the path \""
<< PathFindFileName("c:\\") << "\"" << endl;
cout << "\nSearch for the file in path \"c:\"" << endl;
cout << "Returns the file part of the path \""
<< PathFindFileName("c:") << "\"" << endl;
cout << "\nSearch for the file in path \"path\"" << endl;
cout << "Returns the file part of the path \""
<< PathFindFileName("path") << "\"" << endl;
}
OUTPUT:
==========
Search for the file in path c:\path\file
Returns the file part of the path "file"
Search for the file in path "c:\path"
Returns the file part of the path "path"
Search for the file in path "c:\path\"
Returns the file part of the path "path\"
Search for the file in path "c:\"
Returns the file part of the path "c:\"
Search for the file in path "c:"
Returns the file part of the path "c:"
Search for the file in path "path"
Returns the file part of the path "path"
LPTSTR PathFindNextComponent(
LPCTSTR pszPath
);
Parses a path for the next path component. Paths are delimited by backslashes or by the NULL at the end of the path.
Example:
#include <windows.h>
#include <iostream.h>
#include "Shlwapi.h"
void main( void )
{
// Path to find the next part.
char buffer_1[] = "c:\\path1\\path2\\test";
char *lpStr1;
lpStr1 = buffer_1;
cout << "Search a path for the next path component "
<< "after the root " << lpStr1 << endl;
cout << "Return the next path component: \""
<< PathFindNextComponent(lpStr1) << "\"" << endl;
cout << "\nSearch a path for the next path component "
<< "after the root \"c:\\path1\\path2\"" << endl;
cout << "Return the next path component: \""
<< PathFindNextComponent("c:\\path1\\path2") << "\"" << endl;
cout << "\nSearch a path for the next path component "
<< "after the root \"c:\\path1\"" << endl;
cout << "Return the next path component: \""
<< PathFindNextComponent("c:\\path1") << "\"" << endl;
}
OUTPUT:
===========
Search a path for the next path component after the root c:\path1\path2\test
Return the next path component: "path1\path2\test"
Search a path for the next path component after the root "c:\path1\path2"
Return the next path component: "path1\path2"
Search a path for the next path component after the root "c:\path1"
Return the next path component: "path1"
BOOL PathFindOnPath(
LPTSTR pszFile,
LPCTSTR * ppszOtherDirs
);
Finds a file within a given path.
LPTSTR PathGetArgs(
LPCTSTR pszPath
);
Finds the command line arguments within a given path.
Example:
#include <windows.h>
#include <iostream.h>
#include "Shlwapi.h"
void main( void )
{
// Path_1 to search for file arguments (2-Args):
char buffer_1[] = "test.exe temp.txt sample.doc";
char *lpStr1;
lpStr1 = buffer_1;
// Path_2 to search for file arguments (3-Args):
char buffer_2[] = "test.exe 1 2 3";
char *lpStr2;
lpStr2 = buffer_2;
// Path_3 to search for file arguments (3-Args):
char buffer_3[] = "test.exe sample All 15";
char *lpStr3;
lpStr3 = buffer_3;
// Path_4 to search for file arguments (zero args):
char buffer_4[] = "test.exe";
char *lpStr4;
lpStr4 = buffer_4;
cout << "The path passed to the function was : " << lpStr1 <<
"\nThe arg(s)found in path 1 were : " << PathGetArgs(lpStr1) << endl;
cout << "\nThe path passed to the function was : " << lpStr2 <<
"\nThe arg(s)found in path 2 were : " << PathGetArgs(lpStr2) << endl;
cout << "\nThe path passed to the function was : " << lpStr3 <<
"\nThe arg(s)found in path 3 were : " << PathGetArgs(lpStr3) << endl;
cout << "\nThe path passed to the function was : " << lpStr4 <<
"\nThe arg(s)found in path 4 were : " << PathGetArgs(lpStr4) << endl;
}
OUTPUT:
===========
The path passed to the function was : test.exe temp.txt sample.doc
The arg(s)found in path 1 were : temp.txt sample.doc
The path passed to the function was : test.exe 1 2 3
The arg(s)found in path 2 were : 1 2 3
The path passed to the function was : test.exe sample All 15
The arg(s)found in path 3 were : sample All 15
The path passed to the function was : test.exe
The arg(s)found in path 4 were :
===========
UINT PathGetCharType(
TUCHAR ch
);
Determines the type of character with respect to a path.
| GCT_INVALID | The character is not valid in a path. |
| GCT_LFNCHAR | The character is valid in a long file name. |
| GCT_SEPARATOR | The character is a path separator. |
| GCT_SHORTCHAR | The character is valid in a short (8.3) file name. |
| GCT_WILD | The character is a wildcard character. |
int PathGetDriveNumber(
LPCTSTR lpsz
);
Searches a path for a drive letter within the range of 'A' to 'Z' and returns the corresponding drive number.
Example:
#include <windows.h>
#include <iostream.h>
#include "Shlwapi.h"
void main( void )
{
// Paths to search for drive IDs.
char buffer_1[] = "A:\\TEST\\bar.txt";
char *lpStr1;
lpStr1 = buffer_1;
char buffer_2[] = "B:\\TEST\\bar.txt";
char *lpStr2;
lpStr2 = buffer_2;
char buffer_3[] = "Z:\\TEST\\bar.txt";
char *lpStr3;
lpStr3 = buffer_3;
char buffer_4[] = "%:\\TEST\\bar.txt";
char *lpStr4;
lpStr4 = buffer_4;
cout << "The path passed to the function was : " << lpStr1 <<
"\nThe path contains a drive identifier : " << PathGetDriveNumber(lpStr1) << endl;
cout << "The path passed to the function was : " << lpStr2 <<
"\nThe path contains a drive identifier : " << PathGetDriveNumber(lpStr2) << endl;
cout << "The path passed to the function was : " << lpStr3 <<
"\nThe path contains a drive identifier : " << PathGetDriveNumber(lpStr3) << endl;
cout << "The path passed to the function was : " << lpStr4 <<
"\nThe path contains a drive identifier : " << PathGetDriveNumber(lpStr4) << endl;
}
OUTPUT:
===========
The path passed to the function was : A:\TEST\bar.txt
The path contains a drive identifier : 0
The path passed to the function was : B:\TEST\bar.txt
The path contains a drive identifier : 1
The path passed to the function was : Z:\TEST\bar.txt
The path contains a drive identifier : 25
The path passed to the function was : %:\TEST\bar.txt
The path contains a drive identifier : -1
BOOL PathIsContentType(
LPCTSTR pszPath,
LPCTSTR pszContentType
);
Determines if a file's registered content type matches the specified content type. This function obtains the content type for the specified file type and compares that string with the pszContentType. The comparison is not case sensitive.
BOOL PathIsDirectory(
LPCTSTR pszPath
);
Verifies that a path is a valid directory.
BOOL PathIsFileSpec(
LPCTSTR lpszPath
);
Searches a path for any path delimiting characters (for example, ':' or '\' ). If there are no path delimiting characters present, the path is considered to be a File Spec path.
Example:
#include <windows.h>
#include <iostream.h>
#include "Shlwapi.h"
void main( void )
{
// Path 1 string.
char buffer_1[] = "sample";
char *lpStr1;
lpStr1 = buffer_1;
// Path 2 string.
char buffer_2[] = "C:\\TEST\\sample";
char *lpStr2;
lpStr2 = buffer_2;
//Return value from "PathIsFileSpec".
int retval;
// Test path 1 for file spec.
retval = PathIsFileSpec(lpStr1);
cout << "The path to verify file spec is : " << lpStr1 << endl;
cout << "PathIsFileSpec returns TRUE and is : " << retval << endl;
// Test path 2 for file spec.
retval = PathIsFileSpec(lpStr2);
cout << "\nThe path to verify file spec is : " << lpStr2 << endl;
cout << "PathIsFileSpec returns FALSE and is : " << retval << endl;
}
OUTPUT:
==============
The path to verify file spec is : sample
PathIsFileSpec returns TRUE and is : 1
The path to verify file spec is : C:\TEST\sample
PathIsFileSpec returns FALSE and is : 0
BOOL PathIsHTMLFile(
LPCTSTR pszFile
);
Determines if a file is an HTML file. The determination is made based on the content type that is registered for the file's extension.
BOOL PathIsPrefix(
IN LPCTSTR pszPrefix,
IN LPCTSTR pszPath
);
Searches a path to determine if it contains a valid prefix of the type passed by pszPrefix. A prefix is one of these types: "C:\\", ".", "..", "..\\".
Example:
#include <windows.h>
#include <iostream.h>
#include "Shlwapi.h"
void main( void )
{
// Path prefix 1.
char buffer_1[] = "C:\\";
char *lpStr1;
lpStr1 = buffer_1;
// Path prefix 2.
char buffer_2[] = "C:\\test\\Some\\sample";
char *lpStr2;
lpStr2 = buffer_2;
// Path prefix 3.
char buffer_3[] = "sample";
char *lpStr3;
lpStr3 = buffer_3;
// Search a path prefix with result true.
cout << "The contents of path 1 is : " << lpStr1
<< "\nThe contents of path 2 is : " << lpStr2
<< "\nThe return from the function is : "
<< PathIsPrefix(lpStr1,lpStr2)
<< "\nPathIsPrefix returns TRUE "
<< "\nand Path lpStr1 and lpStr2"
<< "\nhave the same prefix :" << endl;
// Search a path prefix with result false.
cout << "\nThe contents of path 1 is : " << lpStr1
<< "\nThe contents of path 3 is : " << lpStr3
<< "\nThe return from the function is : "
<< PathIsPrefix(lpStr1,lpStr3)
<< "\nPathIsPrefix returns FALSE "
<< "\nand Path lpStr1 and lpStr3"
<< "\nhave different prefixes :" << endl;
}
OUTPUT:
==============
The contents of path 1 is : C:\
The contents of path 2 is : C:\test\Some\sample
The return from the function is : 1
PathIsPrefix returns TRUE
and Path lpStr1 and lpStr2
have the same prefix :
The contents of path 1 is : C:\
The contents of path 3 is : sample
The return from the function is : 0
PathIsPrefix returns FALSE
and Path lpStr1 and lpStr3
have different prefixes :
BOOL PathIsRelative(
LPCTSTR lpszPath
);
Searches a path and determines if it is relative.
Example:
#include <windows.h>
#include <iostream.h>
#include "Shlwapi.h"
void main( void )
{
// String path name 1.
char buffer_1[] = "test.exe";
char *lpStr1;
lpStr1 = buffer_1;
// String path name 2.
char buffer_2[] = "C:\\test.exe";
char *lpStr2;
lpStr2 = buffer_2;
// Variable to get the return from "PathIsRelative".
int retval;
// Test case with path not absolute.
retval = PathIsRelative(lpStr1);
cout << "The return from function is :" << retval << endl;
cout << "The path is not absolute : " << lpStr1 << endl;
// Test case with path absolute.
retval = PathIsRelative(lpStr2);
cout << "\nThe return from function is :" << retval << endl;
cout << "The path is absolute : " << lpStr2 << endl;
}
OUTPUT:
==========
The return from function is :1
The path is not absolute : test.exe
The return from function is :0
The path is absolute : C:\test.exe
BOOL PathIsRoot(
LPCTSTR pPath
);
Parses a path to determine if a root directory path part exists.
Example:
#include <windows.h>
#include <iostream.h>
#include "Shlwapi.h"
void main( void )
{
// String path name 1.
char buffer_1[] = "C:\\";
char *lpStr1;
lpStr1 = buffer_1;
// String path name 2.
char buffer_2[] = "path\\file";
char *lpStr2;
lpStr2 = buffer_2;
// Variable to get the return from "PathIsRoot".
int retval;
// Test case with path not absolute.
retval = PathIsRoot(lpStr1);
cout << "The return from function is :" << retval << endl;
cout << "The path does contain a root part :" << lpStr1 << endl;
// Test case with path absolute.
retval = PathIsRoot(lpStr2);
cout << "The return from function is :" << retval << endl;
cout << "The path does not contain part :" << lpStr2 << endl;
}
OUTPUT:
============
The return from function is :1
The path does contain a root part :C:\
The return from function is :0
The path does not contain part :path\file
============
BOOL PathIsSameRoot(
LPCTSTR pszPath1,
LPCTSTR pszPath2
);
Compares two paths to determine if they have a common root component.
Example:
#include <windows.h>
#include <iostream.h>
#include "Shlwapi.h"
void main( void )
{
// String path name 1.
char buffer_1[] = "C:\\path1\\one";
char *lpStr1;
lpStr1 = buffer_1;
// String path name 2.
char buffer_2[] = "C:\\path2\\two";
char *lpStr2;
lpStr2 = buffer_2;
// Variable to get the
// return from "PathIsSameRoot".
int retval;
// Compare paths with same root.
retval = PathIsSameRoot(lpStr1,lpStr2);
cout << "The return from function is : "
<< retval << endl;
cout << "The contents of String 1: " << lpStr1
<< "\nThe contents of String 2: " << lpStr2
<< "\nThese both have the same root part" << endl;
//compare paths with different roots
retval = PathIsSameRoot(lpStr1,"E:\\acme\\three");
cout << "\nThe return from function is : "
<< retval << endl;
cout << "The contents of String 1: " << lpStr1
<< "\nThe contents of String 3: E:\\acme\\three"
<< "\nThese do not have the same root part" << endl;
}
OUTPUT:
==================
The return from function is : 1
The contents of String 1: C:\path1\one
The contents of String 2: C:\path2\two
These both have the same root part
The return from function is : 0
The contents of String 1: C:\path1\one
The contents of String 3: E:\acme\three
These do not have the same root part
BOOL PathIsSystemFolder(
LPTSTR pszPath,
DWORD dwAttrb
);
Determines if an existing folder contains the attributes that make it a system folder. Alternately indicates if certain attributes qualify a folder to be a system folder.
BOOL PathIsUNC(
LPCTSTR pszPath
);
Determines if the string is a valid UNC (universal naming convention) for a server and share path.
Example:
#include <windows.h>
#include <iostream.h>
#include "Shlwapi.h"
void main( void )
{
// String path name 1.
char buffer_1[] = "\\\\path1\\path2";
char *lpStr1;
lpStr1 = buffer_1;
// String path name 2.
char buffer_2[] = "\\\\path3";
char *lpStr2;
lpStr2 = buffer_2;
// String path name 3.
char buffer_3[] = "acme\\path4\\path5";
char *lpStr3;
lpStr3 = buffer_3;
// Variable to get the return
// from "PathIsUNC".
int retval;
// Test path name 1.
retval = PathIsUNC(lpStr1);
cout << "The contents of String 1: " << lpStr1
<< "\nThe return value from the function is " << retval << " = TRUE" << endl;
// Test path name 2.
retval = PathIsUNC(lpStr2);
cout << "The contents of String 2: " << lpStr2
<< "\nThe return value from the function is " << retval << " = TRUE" << endl;
// Test path name 3.
retval = PathIsUNC(lpStr3);
cout << "The contents of String 3: " << lpStr3
<< "\nThe return value from the function is " << retval << " = FALSE"<< endl;
}
OUTPUT:
==============
The contents of String 1: \\path1\path2
The return value from the function is 1 = TRUE
The contents of String 2: \\path3
The return value from the function is 1 = TRUE
The contents of String 3: acme\path4\path5
The return value from the function is 0 = FALSE
BOOL PathIsUNCServer(
LPCTSTR pszPath
);
Determines if a string is a valid UNC (universal naming convention) for a server path only.
Example:
#include <windows.h>
#include <iostream.h>
#include "Shlwapi.h"
void main( void )
{
// String path name 1.
char buffer_1[] = "\\\\path1";
char *lpStr1;
lpStr1 = buffer_1;
// String path name 2.
char buffer_2[] = "\\\\";
char *lpStr2;
lpStr2 = buffer_2;
// String path name 3.
char buffer_3[] = "acme\\path2\\path3";
char *lpStr3;
lpStr3 = buffer_3;
// Variable to get the return
// from "PathIsUNCServer".
int retval;
// Test path name 1.
retval = PathIsUNCServer(lpStr1);
cout << "The contents of String 1: " << lpStr1
<< "\nThe return value from the function is " << retval << " = TRUE" << endl;
// Test path name 2.
retval = PathIsUNCServer(lpStr2);
cout << "The contents of String 2: " << lpStr2
<< "\nThe return value from the function is " << retval << " = TRUE" << endl;
// Test path name 3.
retval = PathIsUNCServer(lpStr3);
cout << "The contents of String 3: " << lpStr3
<< "\nThe return value from the function is " << retval << " = FALSE"<< endl;
}
OUTPUT:
The contents of String 1: \\path1
The return value from the function is 1 = TRUE
The contents of String 2: \\
The return value from the function is 1 = TRUE
The contents of String 3: acme\path2\path3
The return value from the function is 0 = FALSE
BOOL PathIsUNCServerShare(
LPCTSTR pszPath
);
Determines if the string is a valid UNC (universal naming convention) for a share path only.
Example:
#include <windows.h>
#include <iostream.h>
#include "Shlwapi.h"
void main( void )
{
// String path name 1.
char buffer_1[] = "\\\\path1\\path2";
char *lpStr1;
lpStr1 = buffer_1;
// String path name 2.
char buffer_2[] = "\\\\path3";
char *lpStr2;
lpStr2 = buffer_2;
// String path name 3.
char buffer_3[] = "acme\\path4\\path5";
char *lpStr3;
lpStr3 = buffer_3;
// Variable to get the return
// from "PathIsUNCServerShare".
int retval;
// Test path name 1.
retval = PathIsUNCServerShare(lpStr1);
cout << "The contents of String 1: " << lpStr1
<< "\nThe return value from the function is " << retval << " = TRUE" << endl;
// Test path name 2.
retval = PathIsUNCServerShare(lpStr2);
cout << "The contents of String 2: " << lpStr2
<< "\nThe return value from the function is " << retval << " = FALSE" << endl;
// Test path name 3.
retval = PathIsUNCServerShare(lpStr3);
cout << "The contents of String 3: " << lpStr3
<< "\nThe return value from the function is " << retval << " = FALSE"<< endl;
}
OUTPUT:
=================
The contents of String 1: \\path1\path2
The return value from the function is 1 = TRUE
The contents of String 2: \\path3
The return value from the function is 0 = FALSE
The contents of String 3: acme\path4\path5
The return value from the function is 0 = FALSE
BOOL PathIsURL(
IN LPCTSTR pszPath
);
Tests a given string to determine if it conforms to the URL format. This function does not verify that the path points to an existing siteonly that it is a legal URL format.
Example:
#include <windows.h>
#include <iostream.h>
#include "Shlwapi.h"
void main( void )
{
// String path name 1.
char buffer_1[] = "http://www.microsoft.com/software/index.html";
char *lpStr1;
lpStr1 = buffer_1;
// String path name 2.
char buffer_2[] = "http://www.microsoft.com";
char *lpStr2;
lpStr2 = buffer_2;
// String path name 3.
char buffer_3[] = "microsoft.com";
char *lpStr3;
lpStr3 = buffer_3;
// Variable to get the return
// from "PathIsURL".
int retval;
// Test path name 1.
retval = PathIsURL(lpStr1);
cout << "The contents of String 1: " << lpStr1
<< "\nThe return value from the function is " << retval << " = TRUE" << endl;
// Test path name 2.
retval = PathIsURL(lpStr2);
cout << "The contents of String 2: " << lpStr2
<< "\nThe return value from the function is " << retval << " = TRUE" << endl;
// Test path name 3.
retval = PathIsURL(lpStr3);
cout << "The contents of String 3: " << lpStr3
<< "\nThe return value from the function is " << retval << " = FALSE"<< endl;
}
OUTPUT:
=============
The contents of String 1: http://www.microsoft.com/software/index.html
The return value from the function is 1 = TRUE
The contents of String 2: http://www.microsoft.com
The return value from the function is 1 = TRUE
The contents of String 3: microsoft.com
The return value from the function is 0 = FALSE
BOOL PathMakePretty(
LPTSTR lpPath
);
Converts a path to all lowercase characters to give the path a consistent appearance.
Example:
#include <windows.h>
#include <iostream.h>
#include "Shlwapi.h"
void main( void )
{
// Path name 1.
char buffer_1[] = "C:\\TEST\\FILE";
char *lpStr1;
lpStr1 = buffer_1;
// Path name 2.
char buffer_2[] = "c:\\test\\file";
char *lpStr2;
lpStr2 = buffer_2;
// Test path name 1.
cout << "The content of the unconverted path is : " << lpStr1 << endl;
cout << "The \"PathMakePretty\" function returns the value "
<< PathMakePretty(lpStr1) << " = TRUE & converts" << endl;
cout << "The content of the converted path is : " << lpStr1 << endl;
// Test path name 2.
cout << "\nThe content of the unconverted path is : " << lpStr2 << endl;
cout << "The \"PathMakePretty\" function returns the value "
<< PathMakePretty(lpStr2) << " = FALSE & no conversion" << endl;
cout << "The content of the converted path is : " << lpStr2 << endl;
}
OUTPUT:
=============
The content of the unconverted path is : C:\TEST\FILE
The "PathMakePretty" function returns the value 1 = TRUE & converts
The content of the converted path is : C:\test\file
The content of the unconverted path is : c:\test\file
The "PathMakePretty" function returns the value 0 = FALSE & no conversion
The content of the converted path is : c:\test\file
BOOL PathMakeSystemFolder(
LPTSTR pszPath
);
Gives an existing folder the proper attributes to become a system folder.
BOOL PathMatchSpec(
LPCTSTR pszFileParam,
LPCTSTR pszSpec
);
Searches a string using a DOS wild card match type. The string can be searched for a particular file extension, such as *.bmp, *.doc, and so on.
Example:
#include <windows.h>
#include <iostream.h>
#include "Shlwapi.h"
void main( void )
{
// String path name 1.
char buffer_1[] = "C:\\Test\\File.txt";
char *lpStr1;
lpStr1 = buffer_1;
// String path name 2.
char buffer_2[] = "C:\\Test\\File.bmp";
char *lpStr2;
lpStr2 = buffer_2;
// String path name 3.
char buffer_3[] = "*.txt";
char *lpStr3;
lpStr3 = buffer_3;
// String path name 4.
char buffer_4[] = "C:\\Test\\File";
char *lpStr4;
lpStr4 = buffer_4;
// Variable to get the return.
// from "PathMatchSpec"
int retval;
// Test path name 1.
retval = PathMatchSpec(lpStr1,lpStr3);
cout << "The contents of String 1: " << lpStr1
<< "\nThe return value from the function is " << retval << " = TRUE" << endl;
// Test path name 2.
retval = PathMatchSpec(lpStr2,"*.bmp");
cout << "The contents of String 2: " << lpStr2
<< "\nThe return value from the function is " << retval << " = TRUE" << endl;
// Test path name 4.
retval = PathMatchSpec(lpStr4,lpStr2);
cout << "The contents of String 4: " << lpStr4
<< "\nThe return value from the function is " << retval << " = FALSE"<< endl;
}
OUTPUT:
==========
The contents of String 1: C:\Test\File.txt
The return value from the function is 1 = TRUE
The contents of String 2: C:\Test\File.bmp
The return value from the function is 1 = TRUE
The contents of String 4: C:\Test\File
The return value from the function is 0 = FALSE
int PathParseIconLocation(
IN OUT LPTSTR pszIconFile
);
Parses a file location string for its file component and icon index.
Example:
#include <windows.h>
#include <iostream.h>
#include "Shlwapi.h"
void main( void )
{
// Path to parse for file and icon index.
char buffer_1[] = "C:\\TEST\\sample.txt,3";
char *lpStr1;
lpStr1 = buffer_1;
// Return value from "PathParseIconLocation".
int retval;
// Search a path to parse for file and icon index.
retval = PathParseIconLocation(lpStr1);
cout << "The path to parse for file and icon index is : " << lpStr1 << endl;
cout << "PathParseIconLocation returns the icon index of: " << retval << endl;
}
OUTPUT:
==========
The path to parse for file and icon index is : C:\TEST\sample.txt
PathParseIconLocation returns the icon index of: 3
void PathQuoteSpaces(
LPTSTR lpsz
);
Searches a path for spaces. If spaces are found, the entire path is quoted.
Example:
#include <windows.h>
#include <iostream.h>
#include "Shlwapi.h"
void main( void )
{
// Path with spaces.
char buffer_1[] = "C:\\sample_one\\sample two";
char *lpStr1;
lpStr1 = buffer_1;
// Path before quote spaces.
cout << "The path before PathQuoteSpaces: " << lpStr1 << endl;
// Call "PathQuoteSpaces".
PathQuoteSpaces(lpStr1);
// Path after quote spaces.
cout << "The path after PathQuoteSpaces: " << lpStr1 << endl;
}
OUTPUT:
==================
The path before PathQuoteSpaces: C:\sample_one\sample two
The path after PathQuoteSpaces: "C:\sample_one\sample two"
BOOL PathRelativePathTo(
LPTSTR pszPath,
LPCTSTR pszFrom,
DWORD dwAttrFrom,
LPCTSTR pszTo,
DWORD dwAttrTo
);
Creates a relative path from two paths.
Example:
#include <windows.h>
#include <iostream.h>
#include "Shlwapi.h"
void main( void )
{
char szOut[MAX_PATH] = "";
char szFrom[] = "c:\\a\\b\\path";
char szTo[] = "c:\\a\\x\\y\\file";
cout << "The relative path is relative from: ";
cout << szFrom;
cout << "\n";
cout << "The relative path is relative to: ";
cout << szTo;
cout << "\n";
PathRelativePathTo( szOut,
szFrom,
FILE_ATTRIBUTE_DIRECTORY,
szTo,
FILE_ATTRIBUTE_NORMAL);
cout << "The relative path is: ";
cout << szOut;
cout << "\n";
}
OUTPUT:
==================
The relative path is relative from: c:\a\b\path
The relative path is relative to: c:\a\x\y\file
The relative path is: ..\x\y\file
void PathRemoveArgs(
LPTSTR pszPath
);
Removes any arguments from a given path.
Example:
#include <windows.h>
#include <iostream.h>
#include "Shlwapi.h"
void main( void )
{
// Path with arguments.
char buffer_1[] = "c:\\a\\b\\FileA Arg1 Arg2";
char *lpStr1;
lpStr1 = buffer_1;
// Path before "PathRemoveArgs".
cout << "Path before calling \"PathRemoveArgs\": " << lpStr1 << endl;
// Call function "PathRemoveArgs".
PathRemoveArgs(lpStr1);
// Path after "PathRemoveArgs".
cout << "Path before calling \"PathRemoveArgs\": " << lpStr1 << endl;
}
OUTPUT:
==================
Path before calling "PathRemoveArgs": c:\a\b\FileA Arg1 Arg2
Path before calling "PathRemoveArgs": c:\a\b\FileA
LPTSTR PathRemoveBackslash(
LPTSTR lpszPath
);
Removes the trailing backslash from a given path.
Example:
#include <windows.h>
#include <iostream.h>
#include "Shlwapi.h"
void main( void )
{
// Path with backslash.
char buffer_1[] = "c:\\a\\b\\File\\";
char *lpStr1;
lpStr1 = buffer_1;
// Path before "PathRemoveBackslash".
cout << "Path before calling \"PathRemoveBackslash\": " << lpStr1 << endl;
// Call function "PathRemoveBackslash".
PathRemoveBackslash(lpStr1);
// Path after "PathRemoveBackslash".
cout << "Path after calling \"PathRemoveBackslash\": " << lpStr1 << endl;
}
OUTPUT:
==================
Path before calling "PathRemoveBackslash": c:\a\b\File\
Path after calling "PathRemoveBackslash": c:\a\b\File
void PathRemoveBlanks(
LPTSTR lpszString
);
Removes all leading and trailing spaces from a string.
Example:
#include <windows.h>
#include <iostream.h>
#include "Shlwapi.h"
void main( void )
{
// Path with leading and trailing spaces.
char buffer_1[] = " c:\\TEST\\File1\\File2 ";
char *lpStr1; // |--------- leading & |-------- trailing spaces
lpStr1 = buffer_1;
// Path before "PathRemoveBlanks".
cout << "Path before calling \"PathRemoveBlanks\": " << lpStr1 << endl;
cout << "Dashes are only to indicate path width :"
<<"\n---->|" << lpStr1 << "|<----" << endl;
// Call function "PathRemoveBlanks".
PathRemoveBlanks(lpStr1);
// Path after "PathRemoveBlanks".
cout << "\nPath after calling \"PathRemoveBlanks\": " << lpStr1 << endl;
cout << "Dashes are only to indicate path width :"
<< "\n---->|" << lpStr1 << "|<----" << endl;
}
OUTPUT:
==================
Path before calling "PathRemoveBlanks": c:\TEST\File1\File2
Dashes are only to indicate path width :
---->| c:\TEST\File1\File2 |<----
Path after calling "PathRemoveBlanks": c:\TEST\File1\File2
Dashes are only to indicate path width :
---->|c:\TEST\File1\File2|<----
void PathRemoveExtension(
LPTSTR pszPath
);
Removes the file extension from a path, if there is one.
Example:
#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 extension.
cout << "The path with extension is : " << lpStr1 << endl;
// Call to "PathRemoveExtension".
PathRemoveExtension(lpStr1);
// Print the path without the extension.
cout << "\nThe path without extension is : " << lpStr1 << endl;
}
OUTPUT:
==================
The path with extension is : C:\TEST\sample.txt
The path without extension is : C:\TEST\sample
BOOL PathRemoveFileSpec(
LPTSTR pszPath
);
Removes the trailing file name and backslash from a path, if it has them.
Example:
#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
BOOL PathRenameExtension(
LPTSTR pszPath,
LPCTSTR pszExt
);
Replaces the extension of a file name with a new extension. If the file name does not contain an extension, the extension will be attached to the end of the string.
BOOL PathSearchAndQualify(
LPCTSTR pcszPath,
LPTSTR pszFullyQualifiedPath,
UINT cchFullyQualifiedPath
);
Determines if a given path is correctly formatted and fully qualified.
LPTSTR PathSkipRoot(
LPCTSTR pszPath
);
Parses a path, ignoring the drive letter or UNC server/share path parts.
Example:
#include <windows.h>
#include <iostream.h>
#include "Shlwapi.h"
void main( void )
{
// Path to convert.
char buffer_1[] = "C:\\path1\\path2";
char *lpStr1;
lpStr1 = buffer_1;
cout << "The path before is : " << lpStr1 << endl;
cout << "The path after is : " << PathSkipRoot(lpStr1) << endl;
}
OUTPUT:
==================
The path before is : C:\path1\path2
The path after is : path1\path2
bool PathSetDlgItemPath(
HWND hDlg,
int id,
LPCSTR pszPath,
);
Sets the text of a child control in a window or dialog box, using PathCompactPath to make sure the path fits in the control.
void PathStripPath(
LPTSTR pszPath
);
Removes the path portion of a fully qualified path and file.
BOOL PathStripToRoot(
LPTSTR szRoot
);
Removes all parts of the path except for the root information.
Example:
#include <windows.h>
#include <iostream.h>
#include "Shlwapi.h"
void main( void )
{
// Path to convert.
char buffer_1[] = "C:\\path1\\path2";
char *lpStr1;
lpStr1 = buffer_1;
// Print the path before the root is stripped.
cout << "The contents of the path before is : " << lpStr1 << endl;
// Print the return value from the function.
cout << "The return from \"PathStripToRoot\" is : "
<< PathStripToRoot(lpStr1) << endl;
// Print the path before the root is stripped.
cout << "The contents of the path after is : " << lpStr1 << endl;
}
OUTPUT:
==================
The path before is : C:\path1\path2
The return from "PathStripToRoot" is : 1
The path after is : C:\
BOOL PathUnmakeSystemFolder(
LPTSTR pszPath
);
Removes the attributes from a folder that make it a system folder. This folder must actually exist in the file system.
void PathUnquoteSpaces(
LPTSTR lpsz
);
Removes quotes from the beginning and end of a path.
Example:
#include <windows.h>
#include <iostream.h>
#include "Shlwapi.h"
void main( void )
{
// Path to convert.
char buffer_1[] = "\"C:\\path1\\path2\"";
char *lpStr1;
lpStr1 = buffer_1;
// Print the path before quotes are stripped.
cout << "The contents of the path before is : " << lpStr1 << endl;
// Call the "PathUnquoteSpaces".
PathUnquoteSpaces(lpStr1);
// Print the path after quotes are stripped.
cout << "The contents of the path after is : " << lpStr1 << endl;
}
OUTPUT:
==================
The path before is : "C:\path1\path2"
The path after is : C:\path1\path2
© 1997 Microsoft Corporation. All rights reserved. Terms of Use.