Verify Path Exists
-6
Verify Path Exists
DWORD VerifyPathExists(LPCTSTR pszPathName)
{
if (pszPathName == NULL)
{
return 0;
}
// Declare variablees
TCHAR szPath[MAX_PATH];
lstrcpy(szPath, pszPathName);
// Find and skip the first entry of '\\' character, that separates drive letter from the rest of the path
LPTSTR psz = _tcschr(szPath, _T('\\'));
if (psz == NULL)
{
return 0;
}
psz++;
// Sequentially increase length of the path, while checking it on existance
for (;;)
{
// -- Find the name of a directory
psz = _tcschr(psz, _T('\\'));
if (psz == NULL)
{
break;
}
// -- Temporarily substitute '\\' character with end of line character
*psz = 0;
// -- Creater the directory if it does not exist
if (GetFileAttributes(szPath) == 0xFFFFFFFF)
{
if (!CreateDirectory(szPath, NULL))
{
return GetLastError();
}
}
// -- Restore '\\' character and move ahead
*psz++ = _T('\\');
}
if (GetFileAttributes(szPath) == 0xFFFFFFFF)
{
if (!CreateDirectory(szPath, NULL))
{
return GetLastError();
}
}
return 0;
}
{
if (pszPathName == NULL)
{
return 0;
}
// Declare variablees
TCHAR szPath[MAX_PATH];
lstrcpy(szPath, pszPathName);
// Find and skip the first entry of '\\' character, that separates drive letter from the rest of the path
LPTSTR psz = _tcschr(szPath, _T('\\'));
if (psz == NULL)
{
return 0;
}
psz++;
// Sequentially increase length of the path, while checking it on existance
for (;;)
{
// -- Find the name of a directory
psz = _tcschr(psz, _T('\\'));
if (psz == NULL)
{
break;
}
// -- Temporarily substitute '\\' character with end of line character
*psz = 0;
// -- Creater the directory if it does not exist
if (GetFileAttributes(szPath) == 0xFFFFFFFF)
{
if (!CreateDirectory(szPath, NULL))
{
return GetLastError();
}
}
// -- Restore '\\' character and move ahead
*psz++ = _T('\\');
}
if (GetFileAttributes(szPath) == 0xFFFFFFFF)
{
if (!CreateDirectory(szPath, NULL))
{
return GetLastError();
}
}
return 0;
}






Oh, and why create intervening directories and keep moving? At the first missing directory, you know the path is invalid, and you're not going to create a new directory and find valid paths rooted in it. Puzzling . . . unless, of course, you were actually trying to implement `mkdir -p` . . .
I've got good news, and I've got bad news:
The universe is merely a figment of my imagination.
Now are you ready for the bad news?