Jul 14, 2012 at 4:16pm Jul 14, 2012 at 4:16pm UTC
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
#include<windows.h>
#include<string>
#include<iostream>
using namespace std;
int del()
{
WIN32_FIND_DATA FindFileData;
HANDLE hFind = INVALID_HANDLE_VALUE;
DWORD dwError;
string FilePath = "C:\\test\\*.*" ;
string FileName;
hFind = FindFirstFile( FilePath.c_str(), &FindFileData);
FileName = FindFileData.cFileName;
cout<<"FOUND: " <<FindFileData.cFileName<<endl;
while (FindNextFile(hFind, &FindFileData) != 0)
{
FileName = FindFileData.cFileName;
cout<<"FOUND: " <<FileName<<endl;
DeleteFile(FileName.c_str());//this does not work
/*remove(FileName.c_str());//this doesn't work either*/
}
dwError = GetLastError();
FindClose(hFind);
if (dwError != ERROR_NO_MORE_FILES)
{
cout<<"ERROR: " <<dwError<<endl;
return (-1);
}
return 0;
}
int main()
{
del();
return 0;
}
Its really annoying, what i am missing here..
EDIT: Never mind, i found out.. i forgot to put the whole adress in DeleteFile()
if anyone needs the same function he should use strcat() before DeleteFile().
Last edited on Jul 14, 2012 at 4:32pm Jul 14, 2012 at 4:32pm UTC
Jul 15, 2012 at 2:51pm Jul 15, 2012 at 2:51pm UTC
The problem is this:
1 2 3 4 5 6 7 8 9 10
while (FindNextFile(hFind, &FindFileData) != 0)
{
FileName = FindFileData.cFileName;
char * file="" ;
strcpy(file,path);
strcat(file,FileName.c_str());
cout<<"DELETED: " <<file<<endl;
DeleteFile(file);
}
You are only allocate a pointer, but no memory in
variable. A quick fix could be this:
1 2 3 4 5 6 7 8 9 10
while (FindNextFile(hFind, &FindFileData) != 0)
{
FileName = FindFileData.cFileName;
char file [MAX_PATH] = {0};
strcpy(file,path);
strcat(file,FileName.c_str());
cout<<"DELETED: " <<file<<endl;
DeleteFile(file);
}
Last edited on Jul 15, 2012 at 2:52pm Jul 15, 2012 at 2:52pm UTC