Important Notice: Our web hosting provider recently started charging us for additional visits, which was unexpected. In response, we're seeking donations. Depending on the situation, we may explore different monetization options for our Community and Expert Contributors. It's crucial to provide more returns for their expertise and offer more Expert Validated Answers or AI Validated Answers. Learn more about our hosting issue here.

How do I delete a directory and all of the files inside of it, including the read-only files?

0
Posted

How do I delete a directory and all of the files inside of it, including the read-only files?

0

There is no Shell function that deletes a directory and all of its files, so we can use Feroz’s function: Uses the Windows API: //By Feroz Zahid BOOL DeleteDirectory(const TCHAR* sPath) { HANDLE hFind; // file handle WIN32_FIND_DATA FindFileData; TCHAR DirPath[MAX_PATH]; TCHAR FileName[MAX_PATH]; _tcscpy(DirPath,sPath); _tcscat(DirPath,”\\*”); // searching all files _tcscpy(FileName,sPath); _tcscat(FileName,”\\”); hFind = FindFirstFile(DirPath,&FindFileData); // find the first file if(hFind == INVALID_HANDLE_VALUE) return FALSE; _tcscpy(DirPath,FileName); bool bSearch = true; while(bSearch) { // until we finds an entry if(FindNextFile(hFind,&FindFileData)) { if(IsDots(FindFileData.cFileName)) continue; _tcscat(FileName,FindFileData.cFileName); if((FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) { // we have found a directory, recurse if(!DeleteDirectory(FileName)) { FindClose(hFind); return FALSE; // directory couldn’t be deleted } RemoveDirectory(FileName); // remove the em

Related Questions

What is your question?

*Sadly, we had to bring back ads too. Hopefully more targeted.

Experts123