954,496 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

How to delete a whole folder

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.

robinotje
Light Poster
27 posts since Jan 2010
Reputation Points: 10
Solved Threads: 0
 
ashishchoure
Junior Poster
105 posts since Sep 2008
Reputation Points: 59
Solved Threads: 10
 

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.

robinotje
Light Poster
27 posts since Jan 2010
Reputation Points: 10
Solved Threads: 0
 

Oh.
So you need to delete files first. You can use FindFirstFile, FindNextFile , DeleteFile functions recursively. After this you can use RemoveDirectory function.

See this Links
http://msdn.microsoft.com/en-us/library/aa364418%28VS.85%29.aspx
http://msdn.microsoft.com/en-us/library/aa364428%28VS.85%29.aspx
http://msdn.microsoft.com/en-us/library/aa363915%28VS.85%29.aspx

ashishchoure
Junior Poster
105 posts since Sep 2008
Reputation Points: 59
Solved Threads: 10
 

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;
}
robinotje
Light Poster
27 posts since Jan 2010
Reputation Points: 10
Solved Threads: 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.

ashishchoure
Junior Poster
105 posts since Sep 2008
Reputation Points: 59
Solved Threads: 10
 

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:

robinotje
Light Poster
27 posts since Jan 2010
Reputation Points: 10
Solved Threads: 0
 

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.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

Well, it doesn't contain any. :)

robinotje
Light Poster
27 posts since Jan 2010
Reputation Points: 10
Solved Threads: 0
 

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

robinotje
Light Poster
27 posts since Jan 2010
Reputation Points: 10
Solved Threads: 0
 

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

ashishchoure
Junior Poster
105 posts since Sep 2008
Reputation Points: 59
Solved Threads: 10
 

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

robinotje
Light Poster
27 posts since Jan 2010
Reputation Points: 10
Solved Threads: 0
 

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.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

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

hag++
Junior Poster
197 posts since Jan 2010
Reputation Points: 34
Solved Threads: 31
 

Ok, sorry for beginner knowledge. :(

But could anyone post the whole source code?

robinotje
Light Poster
27 posts since Jan 2010
Reputation Points: 10
Solved Threads: 0
 

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

hag++
Junior Poster
197 posts since Jan 2010
Reputation Points: 34
Solved Threads: 31
 

Take a look at the SHFileOperation Win32 API function ( http://msdn.microsoft.com/en-us/library/bb762164%28v=VS.85%29.aspx ). It will allow you to delete all files and subdirectories, without having to empty the subdirectories first.

MonsieurPointer
Junior Poster
125 posts since Jun 2011
Reputation Points: 31
Solved Threads: 12
 

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

string name = /* The directory path */;
system("rmdir /s /q " + name");
tkud
Posting Whiz in Training
235 posts since Sep 2009
Reputation Points: 13
Solved Threads: 46
 

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

Eagletalon
Junior Poster
113 posts since Mar 2011
Reputation Points: 47
Solved Threads: 13
 
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.

MonsieurPointer
Junior Poster
125 posts since Jun 2011
Reputation Points: 31
Solved Threads: 12
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: