In the past, when I was writing code, I encountered a problem with the file path. for example, extract the directory or file name of the file path and determine whether the directory or file exists. every time you encounter such a problem, you always have to write your own code to implement it. Extracting directories involves tedious string analysis operations. To detect whether directories/files exist, you can determine whether directories/files can be opened. When the level is not enough, of course, the more you write, the more hidden bugs there will be. recently, when I looked at some code collected on the internet, I found some useful shell APIs to implement some of the functions I needed. So I looked at msdn and found that I had been behind closed doors for so long. the following describes some shell APIs for file operations.
Prototype: bool pathfileexists (lpctstr pszpath );
Function: determines whether the directory/file specified by pszpath exists.
Prototype: bool pathisdirectory (lpctstr pszpath );
Function: determines whether the path specified by pszpath is a directory.
Prototype: bool pathisdirectoryempty (lpctstr pszpath );
Function: determines whether the directory specified by pszpath is an empty directory.
Prototype: bool pathremovefilespec (lptstr pszpath );
Function: remove the file name and backslash (if any) at the end of the path specified by pszpath)
Example:
Tchar szpath [] = _ T ("d :\\ fantasy novels \ ridiculous ");
If (: pathremovefilespec (szpath ))
{
// Szpath value: D: \ fantasy novel
}
Prototype: void pathstrippath (lptstrPszpath);
Function: remove the directory file in the path specified by pszpath.
Example:
Tchar szpath [] = _ T ("d :\\ fantasy novels \ ridiculous ");
: Pathstrippath (szpath );
// The szpath value is
Prototype: bool pathisroot (lpctstr pszpath );
Function: determines whether the path specified by pszpath is the root directory (that is, the volume label)
Prototype: bool pathissameroot (lpctstr pszpath1, lpctstr pszpath2 );
Function: determines whether the path specified by pszpath1 and pszpath2 are in the same root directory.
Prototype: bool pathstriptoroot (lptstr szrot );
Function: Remove directories other than the root directory in szrot.
Prototype: lptstr pathskiproot (lpctstr pszpath );
Function: determines other parts of the pszpath except the root directory.
Prototype: int pathisexe (lpcwstr szfile );
Function: determines whether the file specified by szfile is an executable file.
Prototype: bool pathisurl (lpctstr pszpath );
Function: determines whether the path specified by pszpath is in URL format.
There are also a lot of such APIs, ah, I am too class, don't get it. Anyway, when I need similar functions, I will first check the functions provided in shell, instead of building a car behind closed doors.