Skip to main content
Code Review

Return to Question

edited tags
Link
Source Link
1221
  • 23
  • 3

filesystem create a folder

I got a mixture of two languages ​​C and C ++

The code finds the folder my documents and creates additional folders in it when the program starts, I found no other way to implement this better. I also did not find any ready-made example where all this has already been implemented so as not to make a bicycle, I like when the code looks compact, but it looks bad for me.

template <typename T1, typename T2>
void knownFolder(T1& setKnownFolderID, T2& getDisplayName)
{
 PWSTR pszFolderPath;
 for (;;) // !!!>
 {
 if (FAILED(SHGetKnownFolderPath(setKnownFolderID, 0, nullptr, &pszFolderPath)))
 continue;
 getDisplayName = pszFolderPath;
 CoTaskMemFree(pszFolderPath);
 break; // <!!
 }
}

/

template <typename T2>
void folderLink(T2& setDisplayName, T2& getDisplayName)
{
 getDisplayName = setDisplayName + L"\\" + getDisplayName;
 for (;;) // !!!>
 {
 if (!filesystem::exists(getDisplayName))
 if (!filesystem::create_directory(getDisplayName))
 continue;
 break; // <!!
 }
}

/

wstring folder_Documents, // C:\ ... \Documents
 folder_Microsoft = L"Microsoft", // C:\ ... \Documents\Microsoft
 folder_XP = L"XP",
 folder_XP_Alpha = L"Alpha",
 folder_XP_Beta = L"Beta",
 folder_Win7 = L"Win7",
 folder_Win7_Alpha = L"Alpha",
 folder_Win7_Beta = L"Beta";

/

void foldersystem()
{
 knownFolder(FOLDERID_Documents, folder_Documents); // Documents
 folderLink(folder_Documents, 
 folder_Microsoft); // Microsoft
 folderLink(folder_Microsoft,
 folder_XP); // XP
 folderLink(folder_XP, folder_XP_Alpha);
 folderLink(folder_XP, folder_XP_Beta);
 folderLink(folder_Microsoft,
 folder_Win7); // Win7
 folderLink(folder_Win7, folder_Win7_Alpha);
 folderLink(folder_Win7, folder_Win7_Beta);
}
lang-cpp

AltStyle によって変換されたページ (->オリジナル) /