User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 392,044 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 4,252 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser:
Views: 1096 | Replies: 2
Reply
Join Date: Feb 2008
Posts: 80
Reputation: cosmos22 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
cosmos22's Avatar
cosmos22 cosmos22 is offline Offline
Junior Poster in Training

How to delete a folder?

  #1  
Feb 18th, 2008
Hello, I have recently been able to delete files using this code(With the aid of others from the forum) And I was wondering how you can change this, to suit deleting a folder

  1. #include <iostream>
  2. #include <windows.h>
  3. int main(int argc, const char * argv[])
  4. {
  5.  
  6.  
  7. // Get a pointer to the file name/path
  8. const char * pFileToDelete = "c:\\Program Files\\myfile.txt";
  9.  
  10. // try deleting it using DeleteFile
  11. if(DeleteFile(pFileToDelete ))
  12.  
  13. {
  14. // succeeded
  15. std::cout << "Deleted file" << std::endl;
  16. }
  17. else
  18. {
  19. // failed
  20. std::cout << "Failed to delete the file" << std::endl;
  21. }
  22. std::cin.get();
  23. return 0;
  24. }
Thank you
Last edited by WolfPack : Feb 18th, 2008 at 12:04 pm. Reason: Added [CODE=CPP][/CODE] Tags.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,809
Reputation: Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold 
Rep Power: 11
Solved Threads: 184
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: How to delete a folder?

  #2  
Feb 18th, 2008
You need to get a good Win32 reference.

The function you want is
BOOL RemoveDirectory( LPCTSTR lpPathName );

The directory must be empty and your program must have permissions to delete it. If the function fails it returns zero, else it returns non-zero.

Hope this helps.
Reply With Quote  
Join Date: Jan 2008
Posts: 5
Reputation: deadrabit is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
deadrabit deadrabit is offline Offline
Newbie Poster

Re: How to delete a folder?

  #3  
Feb 18th, 2008
similar to how there are all the windows functions to work with files, there are ones to handle directories.
http://msdn2.microsoft.com/en-us/lib...50(VS.85).aspx

unfortunately there is no direct way, first you must delete all the files within the directory.
for this you need to find all the files in the folder.

microsoft are nice enough to give you this code in an example though.
http://msdn2.microsoft.com/en-us/lib...00(VS.85).aspx

store the filenames in an array of strings or something, loop through that array with deletefile function.

then its simple to use removedirectory function
http://msdn2.microsoft.com/en-us/lib...88(VS.85).aspx

MSDN is your friend =D
Dead.Rabit
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb C++ Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 11:16 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC