Hello everyone,

I'm new to C ++, and I want to know how to delete a whole folder.
I've tried to do this, but it doesn't work. Can anyone help me?

#include <iostream>
#include <stdio.h>
using namespace std;

int main()
{
	remove ("c:\\users\\asus\\new");
	return 0;
}

I'm running Vista, and using Microsoft Visual Studio 2008 Express Edition.

Thanks, but there they say that you can only specify an empty folder. I was looking for a folder with files in it.
Sorry, I forgot to tell you.

Ok. But can I use '*', when deleting files?

#include <iostream>
#include <stdio.h>
using namespace std;

int main()
{
	remove ("c:\\users\\asus\\new\\*.*");
	return 0;
}

yes u can use all wild character with those functions :)

WIN32_FIND_DATA FindFileData;
HANDLE hFind = INVALID_HANDLE_VALUE;
DWORD dwError;

wstring FilePath = L"c:\\users\\asus\\new\\*.*";
wstring FileName;

hFind = FindFirstFile( FilePath.c_str(), &FindFileData);

FileName = FindFileData.cFileName;

printf ("First file name is %s\n", FindFileData.cFileName);
while (FindNextFile(hFind, &FindFileData) != 0) 
{
	FileName = FindFileData.cFileName;
	printf ("Next file name is %s\n", FindFileData.cFileName);
}

dwError = GetLastError();
FindClose(hFind);
if (dwError != ERROR_NO_MORE_FILES) 
{
	printf ("FindNextFile error. Error is %u\n", dwError);
	return (-1);
}

May this help you.

Wow! That's nice. Because I'm not such a good C++ writer, is there a function included which deletes the files? Because this is such a good source code, but could you also show one which only deletes all the files in a folder, and then deletes the folder itself?
Thanks for all you work! :icon_smile:

Standard C function remove() will delete a file. If the folder contains other folders than its a little more complicated because the code will have to be recursive (function calling itself) to process all the sub-folders.

Well, it doesn't contain any. :)

What I did first was just write this in batch, and then just include it in C++, but this did't work well...:(

i thought u would add delete logic yourself. Try and try. If u then face issue post ur comment and code

I think this one is to dificult for me. I had a look at the sites, but why doesn't this work then?

#include <windows.h>
using namespace std;

HANDLE WINAPI FindFirstFile(
							_in LPCTSTR c:\\users\\asus\\example,
							_out LPWIN32_FIND_DATA data
							);
LPWIN32_FIND_DATA(
				  __in HANDLE data,
				  _ out LPWIN32_FIND_DATA data2
				  );
BOOL WINAPI DeleteFile(
					   _in LPCTSTR data2
					   );

Thanks for now :)

All you posted is a bunch of nonsense. See post #6 in this thread for example of FindFirstFile() etc. The only other thing you need is the function remove (click the link here) to delete the file. But then we have already told you all that.

Hey robinotje, check out this msdn article. Read the top portion then scroll all the way to the bottom and check out the C++ code snippet.
http://msdn.microsoft.com/en-us/library/aa328747(VS.71).aspx

*EDIT: Keep in mind though, using mscorlib requires the use of .NET so you have to have all windows updates installed for the program to work. If you are using it within normal windows operation (i.e with the OS mounted and running, .NET installed) then the program should work fine. The program will not run in a PE enviroment UNLESS you create a winPE boot disk

Ok, sorry for beginner knowledge. :(

But could anyone post the whole source code?

Ok, sorry for beginner knowledge. :(

But could anyone post the whole source code?

short answer? NOPE! Remember, the whole point of posting on the forum is to LEARN. You study, post code, then if you have questions ppl on the forum will help you with the code you posted. Oh, and dont apologize for being new to programming. Nobody here knows everything there is to know about it. Im def no master at any programming language (yet.... hehe) and personally, I love answering questions and helping people when I can because when I was just starting out I also recieved A TON of help

you could use cstdlib's system(), then call DOS function rmdir..

string name = /* The directory path */;
system("rmdir /s /q " + name");

I have managed to retrieve a list of all files contained in a folder (and all sub folders) but unfortunately this is relying heavily on and written using QT libraries... if you would like I could post said code

Member Avatar for MonsieurPointer

I have managed to retrieve a list of all files contained in a folder (and all sub folders) but unfortunately this is relying heavily on and written using QT libraries... if you would like I could post said code

Sounds like a bunch of nonsense IMHO. That means that you would have to allocate memory, especially a very large amount depending how the folder you are trying to delete is structured. Did you take a look at the function I mentioned earlier? If you need a code example, then don't hesitate to ask.

"tkud" can u help me with the system function well i tried this:

It doesnt work

   int check;
   char *dirname;
   printf("Enter a directory path and name to be deleted (C:/name):");
   gets(dirname);
   system("dir/p");

   check = rmdir(dirname);
   system("cls");
   if (!check){
      printf("Directory deleted\n");
      exit(EXIT_SUCCESS);
   }

   else
   {
              printf("Unable to remove directory\n");
              exit(EXIT_FAILURE);
   }

rmdir() does nothing if the folder contains files.

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.