473,386 Members | 1,791 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Delete Folder (C++)

134 100+
anybody know how i could delete a FOLDER in c++?
i know to delete a file is

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <fstream>
  3. using namespace std;
  4.  
  5.  
  6. /* remove example: remove myfile.txt */
  7. #include <stdio.h>
  8.  
  9. int main ()
  10. {
  11.   if( remove( "gay/gay.txt" ) != 0 )
  12.   {
  13.     perror( "Error deleting file" );
  14.     cin.get();
  15. }
  16.   else
  17.   {
  18.     puts( "File successfully deleted" );
  19.     cin.get();
  20. }
  21.   return 0;
  22. }
  23.  
  24.  
but anybody know how delete a whole folder?

thanks
May 10 '07 #1
6 24590
ilikepython
844 Expert 512MB
anybody know how i could delete a FOLDER in c++?
i know to delete a file is

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <fstream>
  3. using namespace std;
  4.  
  5.  
  6. /* remove example: remove myfile.txt */
  7. #include <stdio.h>
  8.  
  9. int main ()
  10. {
  11.   if( remove( "gay/gay.txt" ) != 0 )
  12.   {
  13.     perror( "Error deleting file" );
  14.     cin.get();
  15. }
  16.   else
  17.   {
  18.     puts( "File successfully deleted" );
  19.     cin.get();
  20. }
  21.   return 0;
  22. }
  23.  
  24.  
but anybody know how delete a whole folder?

thanks
Well, I guess you could delete it through your OS command line using the system function. What OS are you using?
If you don't want to use that, then try searching on Google for it.
May 10 '07 #2
gpraghuram
1,275 Expert 1GB
Hi,
there is a function called rmdir in stdlib.h which can help u to remove a directory.

Thanks
Raghuram
May 10 '07 #3
lumpybanana247
134 100+
thanks to both of you. this is what i used (and it worked :-) )

Expand|Select|Wrap|Line Numbers
  1. #include <errno.h>
  2. #include <dirent.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <iostream>
  6. #include <fstream>
  7. using namespace std;
  8.  
  9.  
  10. /* remove example: remove myfile.txt */
  11. #include <stdio.h>
  12.  
  13. int main ()
  14. {
  15.   if( rmdir("C:/Documents and Settings/Nathan/Desktop/gay") != 0 )
  16.   {
  17.     perror( "Error deleting file" );
  18.     cin.get();
  19. }
  20.   else
  21.   {
  22.     puts( "File successfully deleted" );
  23.     cin.get();
  24. }
  25.   return 0;
  26. }
  27.  
  28.  
May 10 '07 #4
lumpybanana247
134 100+
crap, that code ^^ only works if the directory is empty
May 10 '07 #5
I use this to delete folders even when they are full of files (in linux):

if (system("rm -r ./folder_name")==0)
cout << "folder succesfully deleted" <<endl;
else
cout<<"folder delete failed"<<endl;

rmdir is for deleting empty folders, and rm -r is to delete non-empty folders.

This "./folder_name" is of course when the folder is in the same directory of your application. When it is not, you can try using global route //import/home/... etct, or //tmp/... etc , depending on where your folder is. You can also use the relative path for files and folders, as you wish.

regards,
Jan 22 '09 #6
JosAH
11,448 Expert 8TB
@valverde
The -r flag stands for 'recursive', i.e. if the content of a directory (and its optional sub-directories) can be removed, the entire (now empty) directory is removed.

kind regards,

Jos
Jan 22 '09 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Alf Laue | last post by:
Hi, how do I delete a folder at my ftp-server, that was automatically created through a installation script? I dont have the permission to delete that folder :-( Thx for your help Alf
2
by: Keith Smith | last post by:
I have noticed that when I uninstall my app it doesn't delete the Program Files / MyApp folder if there are "new" files in it that weren't there before. It deletes all files except the "new" ones....
3
by: News | last post by:
Is it possible to delete a file by copying it to the "bit bucket" or "null device"? Back in my youth when I live in VMS-land you could delete a file by copying it to NL: ========== I have...
7
by: Charts | last post by:
I login as administrator of the machine. However running the following code get access denied exception for file inside the directory. The source code: DirectoryInfo target = new...
9
by: Paul | last post by:
I'm trying to make get my app to delete all the files in a specified folder and all the files within the folders of the specified folder. e.g. Folder 1 contains three files (File1, File2, File3)...
3
by: ad | last post by:
Hi, How can I delete all files in a folder by C# for example, I want to delete all files under c:\Test
4
by: Zeb | last post by:
Hi I'm using DirectoryInfo.Delete so that when a product is removed, all it's associated images (including the folder they sit in) are deleted. This actually seems to work fine. However, the...
24
by: biganthony via AccessMonster.com | last post by:
Hi, I have the following code to select a folder and then delete it. I keep getting a Path/File error on the line that deletes the actual folder. The line before that line deletes the files in...
4
by: - HAL9000 | last post by:
When un-installing an application... Is it normal practice to write a special program that erases all the files and folders for all the users of an application that reads and writes to...
0
by: wolfsbane | last post by:
Alright, here it is I am trying to write a win32 app in VB 2005 to clean up user's profiles. everything works correctly except for the Delete("directory", True) statement. I get a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.