Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't this method of deleting files inside a folder work?

std::wstring inxmpath ( L"folder" );
HANDLE hFind;
BOOL bContinue = TRUE;
WIN32_FIND_DATA data;
hFind = FindFirstFile(inxmpath.c_str(), &data); 
// If we have no error, loop through the files in this dir
int counter = 0;
while (hFind && bContinue) {
        std::wstring filename(data.cFileName);
        std::string fullpath = "folder/";
        fullpath += (const char* )filename.c_str();
        if(remove(fullpath.c_str())!=0) return error;
    bContinue = FindNextFile(hFind, &data);
    counter++;
}
FindClose(hFind); // Free the dir

I don't understand why it doesn't work, I think it has something to do with the conversions between wstring and string however I'm not sure about that. I have a folder which has some .txt files, I need to delete all of them using C++. There are no folders in it nothing. How hard can this be?

like image 221
ksm001 Avatar asked Dec 27 '25 16:12

ksm001


1 Answers

Secondly, according to MSDN about the FindFirstFile function:

"Searches a directory for a file or subdirectory with a name that matches a specific name (or partial name if wildcards are used)."

I cannot see a wildcard in your input string, so I can only guess that FindFirstFile will look for files named "folder" in the current execution directory.

Try looking for "folder\\*".

like image 148
cli_hlt Avatar answered Dec 30 '25 06:12

cli_hlt



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!