how to delete all files in a directory c++?

Jul 4, 2011 at 1:09am
closed account (3hkyhbRD)
i want to delete all files in a directory like documents this what i have.Its not working when i just put documents but it deletes the file no problem when i put a specific path.How do i fix this please help its driving me insane!

#include <cstdlib>
#include <iostream>
#include <windows.h>
#include <winable.h>
#include <iostream>

using namespace std;
int main(int argc, char *argv[])
{
std::remove("C:\\Users\\Documents");
std::remove("C:\\Users\\Public\\Pictures\\Sample Pictures\\Autumn Leaves.jpg");
return EXIT_SUCCESS;
}
Jul 4, 2011 at 2:15am
You have to delete everything inside a directory before you can delete the directory itself.
Jul 4, 2011 at 2:55am
closed account (3hkyhbRD)
thats what im trying to do i want do i want to delete every thing in the directory can you help me plzz!!!
Jul 4, 2011 at 6:17am
It can't be done as there will be some file in the folder which is read only file so try to make a folder and create files by your self then del it
Jul 4, 2011 at 12:31pm
One way is to use FindFirstFile() and FindNextFile() and DeleteFile()

As noted above, if the file is read-only, you'll first have to remove that flag, thus before you call DeleteFile() you check the file's read-only flag.

http://msdn.microsoft.com/en-us/library/aa363915(v=VS.85).aspx

http://msdn.microsoft.com/en-us/library/aa364418(v=VS.85).aspx

http://msdn.microsoft.com/en-us/library/aa364428(v=VS.85).aspx
Jul 12, 2011 at 7:08am
One way to do this is enumerate the folder by using FindFirstFile(), FindNestFile() and get attributes for the file using GetFileAttributes and check for readonly and use DeleteFile() finction to delete the file.
Jul 12, 2011 at 7:15am
What is that std::remove() all about???? The only std::remove() I know is this: http://www.cplusplus.com/reference/algorithm/remove/ , and it removes an item from a collection; it has nothing to do with the file system. Is there an obscure std::remove() I know nothing about????

Go with Lamblion's advice, I would say.
Jul 12, 2011 at 9:01am
use system() and the command line command.
Jul 12, 2011 at 9:10am
I believe std:remove does NOT remove the elements. Read again.

"
The relative order of the elements not removed is preserved, while the elements past the new end of range are still valid, although with unspecified values.
"

I believe it just move the "removed" elements to after the new end of range. To actually physically remove them depend on your data structure. If you use list or vector you can use the list.erase(), vector.erase() member function for the actual physical removal.

This lead to me to guess you will usually use std:remove() in conjunction with the data structure erase member function hand in hand to do a physical removal of elements.
Jul 12, 2011 at 9:31am
What is the code for the transfer of file size above 2 gigabytes from one folder to another folder

Last edited on Jul 12, 2011 at 9:32am
Jul 12, 2011 at 10:21am
MoveFileEx()winapi function does exactly that.
Topic archived. No new replies allowed.