C++ code to delete any folder:
#include <string>
#include <iostream>
#include "stdafx.h"
#include <stdio.h>
#include <afx.h>
#include <windows.h>
#include <conio.h>
#include <io.h>
using namespace std;
BOOL IsDots(wchar_t* str)
{
if (_tcscmp(str, TEXT(".")) && _tcscmp(str, TEXT("..")))
return FALSE;
return TRUE;
}
BOOL DeleteDirectory(wchar_t* sPath)
{
HANDLE hFind;
WIN32_FIND_DATA FindFileData;
wchar_t DirPath[MAX_PATH];
wchar_t FileName[MAX_PATH];
_tcscpy(DirPath, sPath);
_tcscat(DirPath, TEXT("\\*"));
_tcscpy(FileName, sPath);
_tcscat(FileName, TEXT("\\"));
//GETTING THE FISRT FILE
hFind = FindFirstFile(DirPath, &FindFileData);
if (hFind == INVALID_HANDLE_VALUE)
return FALSE;
_tcscpy(DirPath, FileName);
bool bSearch = true;
while (bSearch) {
if (FindNextFile(hFind, &FindFileData)) {
if (IsDots(FindFileData.cFileName))
continue;
_tcscat(FileName, FindFileData.cFileName);
if ((FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
//DELETING THE DIRECTORY
if (!DeleteDirectory(FileName)) {
FindClose(hFind);
return FALSE;
}
RemoveDirectory(FileName);
_tcscpy(FileName, DirPath);
}
else {
if (FindFileData.dwFileAttributes & FILE_ATTRIBUTE_READONLY)
_chmod(FileName, _S_IWRITE);
mode if (!DeleteFile(FileName))
{
FindClose(hFind);
return FALSE;
}
_tcscpy(FileName, DirPath);
}
}
else {
if (GetLastError() == ERROR_NO_MORE_FILES)
bSearch = false;
else {
FindClose(hFind);
return FALSE;
}
}
}
FindClose(hFind);
return RemoveDirectory(sPath);
}
//CALLING THE DEL DIR FUNCTION
HOPE ANYONE COULD HELP!!
I get the following error:
error C2664: '_chmod' : cannot convert parameter 1 from 'wchar_t [260]' to 'const char *'
P.S. I use Microsoft Visual Studio.
You're using wide characters, but calling the narrow character version of the function.
Most Windows API functions transparently switch between the two by virtue of the UNICODE
macro being set. The "chmod" function with this behaviour is _tchmod
.
You should switch to _tchmod
.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments