If you need to delete a directory in Windows, the function RemoveDirectory is useful if the directory is empty. If the folder is not empty, you don’t need to implement recursive logic. The shell API function SHFileOperation is likely the function that you want to use. To use this function include the header <shellapi.h>. The SHFileOperation can perform various file operations, but the one we are most interested in is deletion. For those of you looking for something quick to copy-and-paste, here is the code.
void EmptyFolder(std::wstring path, HWND hWnd = NULL)
{
std::vector<WCHAR> doubleTerminated(path.size() + 2);
wcscpy_s(doubleTerminated.data(), doubleTerminated.size(), path.c_str());
doubleTerminated[doubleTerminated.size() - 1] = L'\0';
doubleTerminated[doubleTerminated.size() - 2] = L'\0';
std::wstring progressTitle = L"Cleaning Folder";
SHFILEOPSTRUCT options = { 0 };
options.hwnd = hWnd;
options.wFunc = FO_DELETE;
options.pFrom = doubleTerminated.data();
options.fFlags = FOF_NOCONFIRMATION | FOF_NOERRORUI | FOF_SILENT;
options.lpszProgressTitle = progressTitle.c_str();
DWORD result = SHFileOperation(&options);
}
Explanation
Other code that you may encounter might use pointer data types to perform this same task. I tend minimize managing memory myself. Instead of using pointers to characters as strings, I used types from the standard library. std::wstring and std::vector<WCHAR> are used instead. When pointers to data types are needed, std::wstring::c_str() and std::vector<WCHAR>::data() can be used to supply them. The function I provide here accepts the full path to the folder to be deleted as a std::wstring. That path is copied to the std::vector<WCHAR>. In addition to the text data being copied, two nulls are copied to the end of the data. This is a requirement of the SHFileOperation function for our purpose. Appending a L'\0\0' to the end of the std::wstring does not result in those null characters being present when we use std::wstring::c_str() to get a WCHAR pointer to the data.
A SHFILEOPSTRUCT structure must be populated with the parameters needed for the operation that we would like to perform. Setting it to { 0 } at initialization will set all of the fields in the structure to zero. This is great for structures where zero or null values are what one wants to set as the default values. The fields that we do populate are
hwnd– The handle to the owner window. This can be set to NULL.wFunc– Set to a value for the operation that we want to perform.FO_DELETEis the value to use for deletion.pFrom– Set to the double – null terminated string containing the path to the folder to be deletelpszProgressTitle– Set to a title to show in a UI window that shows the progress of the operationfFlags– flags for various operations. The operations selected here includeFOF_NOCONFIRMATION– don’t ask the user for confirmationFOF_NOERRORUI– don’t show an error UI if the operation failsFOF_SILENT– don’t show the UI.
In testing this, my results have generally been success or 0x7c (for invalid name). The invalid name return value was encountered when a directory had already been deleted (in which case the value passed really was not a valid identifier for a directory!).
Posts may contain products with affiliate links. When you make purchases using these links, we receive a small commission at no extra cost to you. Thank you for your support.
Mastodon: @j2inet@masto.ai
Instagram: @j2inet
Facebook: @j2inet
YouTube: @j2inet
Telegram: j2inet
Bluesky: @j2i.net







