Hi viewers,
I was trying to delete a file from my hard drive and I don't really have a good place to start. I googled it and came up with a code derived from multiple sources. I was wondering if anyone could help me out with the code to delete files from anywhere in your hard drive. Doesn't matter what file it is because this code is for a bigger program i'm trying to create. Here's my code so far...

#include <iostream>
#include <cstdlib>
#include <windows.h>
using namespace std;

int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow);

int main()
{
cout <<"Deleting file...";

system("del C:\Program Files\filedel.txt");

	MessageBox(NULL, "File Deleted!", "Alert!", NULL);
return 0;
}

If you guys could give me any ideas or suggestions i would greatly appreciate it. thx.

Recommended Answers

All 3 Replies

Lazy way to do it:
First strore the file name and location in a string. Append to this string 'del ' at the beginning. Execute the 'System' stmt on that string.

Hi viewers,
I was trying to delete a file from my hard drive and I don't really have a good place to start. I googled it and came up with a code derived from multiple sources. I was wondering if anyone could help me out with the code to delete files from anywhere in your hard drive. Doesn't matter what file it is because this code is for a bigger program i'm trying to create. Here's my code so far...

#include <iostream>
#include <cstdlib>
#include <windows.h>
using namespace std;

int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow);

int main()
{
cout <<"Deleting file...";

system("del C:\Program Files\filedel.txt");

	MessageBox(NULL, "File Deleted!", "Alert!", NULL);
return 0;
}

If you guys could give me any ideas or suggestions i would greatly appreciate it. thx.

You can also use the function FileDelete("path"). "path" is a string containing the filename. This fxn also supports wildcards if you are not sure of the file's extension.

i can't apply this at Borland C

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.