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

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;
}
You have to delete everything inside a directory before you can delete the directory itself.
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!!!
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
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
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.
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.
use system() and the command line command.
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.
What is the code for the transfer of file size above 2 gigabytes from one folder to another folder

Last edited on
MoveFileEx()winapi function does exactly that.
Topic archived. No new replies allowed.