0

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;
}

Edited by happygeek: fixed formatting

2
Contributors
2
Replies
3
Views
9 Years
Discussion Span
Last Post by cosmos22
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.

0

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

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.