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;
// failed
std::cout << "ERROR! " << std::endl;
return 0;

Edited 3 Years Ago by happygeek: fixed formatting

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() ))

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 ))


It works :)

This article has been dead for over six months. Start a new discussion instead.