Using the code below, I tried to implement a second line of code to delete another file in the same folder. I got an error message as a result.

I'm using messenger as an example:

I tried to replecate this line:

const char * pFileToDelete = "h:\\MSN Messenger\\myfile.txt";


into something like


const char * pFileToDelete = "h:\\MSN Messenger\\myfile2.txt";

No result...

How do I list another file into the equation?

Thank you

#include <iostream>
#include <windows.h>
int main(int argc, const char * argv[])
{



// Get a pointer to the file name/path
const char * pFileToDelete = "h:\\MSN Messenger\\myfile.txt";


// try deleting it using DeleteFile
if(DeleteFile(pFileToDelete ))


{
// succeeded
std::cout << "Deleted file"  << std::endl;
}
else
{
// failed
std::cout << "ERROR! " << std::endl;
}
std::cin.get();
return 0;
}

You can only delete one file at a time. If you have a list of files, you can iterate through the list and delete each file in turn.

vector<string> file_list;
int num_files_not_deleted = 0;

for (int i = 0; i < file_list.size(); i++)
  if (!DeleteFile( file_list[ i ].c_str() ))
    num_files_not_deleted++;

if (num_files_not_deleted)
  cout << "Could not delete all the files." << endl;

Hope this helps.

I tested another way, I made it up myself:

const char * pFileToDelete2 = "h:\\MSN Messenger\\filename2.txt";


Then I assigned this second file to the code below


if(DeleteFile(pFileToDelete2 ))

Etc...


It works :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.