Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Other Discussions
  3. Article Writing
  4. how to delete an non-empty directory

how to delete an non-empty directory

Scheduled Pinned Locked Moved Article Writing
jsontutorial
5 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    SAK
    wrote on last edited by
    #1

    Can someone tell me how to easily delete a non-empty directory. I am aware of the 'RemoveDirectory' api but it states in its documentation that it only deletes an empty directory. Is there an easy way to do this. I was thinking of using the ShellExecutre API and calling the 'RD' or 'RMDIR' dos commands, but I cant path to these programs. Thank you.

    N L M T 4 Replies Last reply
    0
    • S SAK

      Can someone tell me how to easily delete a non-empty directory. I am aware of the 'RemoveDirectory' api but it states in its documentation that it only deletes an empty directory. Is there an easy way to do this. I was thinking of using the ShellExecutre API and calling the 'RD' or 'RMDIR' dos commands, but I cant path to these programs. Thank you.

      N Offline
      N Offline
      Nemanja Trifunovic
      wrote on last edited by
      #2

      Why ShellExecute? You can use system, defined in process.h I vote pro drink :beer:

      1 Reply Last reply
      0
      • S SAK

        Can someone tell me how to easily delete a non-empty directory. I am aware of the 'RemoveDirectory' api but it states in its documentation that it only deletes an empty directory. Is there an easy way to do this. I was thinking of using the ShellExecutre API and calling the 'RD' or 'RMDIR' dos commands, but I cant path to these programs. Thank you.

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        Classic recursion. Here's some pseudo code: RecursiveDelete(directory)   If f = GetFirstFile(directory)     do       If f is a directory         RecursiveDelete(sub-directory)       else         DeleteFile(file)     while (f = GetNextFile())   RemoveDirectory(directory)

        1 Reply Last reply
        0
        • S SAK

          Can someone tell me how to easily delete a non-empty directory. I am aware of the 'RemoveDirectory' api but it states in its documentation that it only deletes an empty directory. Is there an easy way to do this. I was thinking of using the ShellExecutre API and calling the 'RD' or 'RMDIR' dos commands, but I cant path to these programs. Thank you.

          M Offline
          M Offline
          Michael Dunn
          wrote on last edited by
          #4

          SHFileOperation() will do it. I have a wrapper class in the Shell section here on CP to make it easier to use, as well. --Mike-- http://home.inreach.com/mdunn/ "....." -- Silent Bob :love: your :bob: with :vegemite: and :beer:

          1 Reply Last reply
          0
          • S SAK

            Can someone tell me how to easily delete a non-empty directory. I am aware of the 'RemoveDirectory' api but it states in its documentation that it only deletes an empty directory. Is there an easy way to do this. I was thinking of using the ShellExecutre API and calling the 'RD' or 'RMDIR' dos commands, but I cant path to these programs. Thank you.

            T Offline
            T Offline
            TomKat
            wrote on last edited by
            #5

            BOOL ClearDirectory(const char *path, BOOL recurs, BOOL deldirs) { if(!path) return FALSE; char xpath[512]=""; strcpy(xpath,path); HANDLE hFile; WIN32_FIND_DATA findData; BOOL Go=TRUE; char fullp[500]=""; char pattern[500]=""; if(xpath[strlen(xpath)-1]!='\\') strcat(xpath,"\\"); if(!IsDirectory(xpath)) return FALSE; strcpy(pattern,xpath); strcat(pattern,"*.*"); hFile=FindFirstFile(pattern,&findData); while((hFile!=INVALID_HANDLE_VALUE)&&(Go)) { if((strcmp(findData.cFileName,".")!=0)&&(strcmp(findData.cFileName,"..")!=0)) { strcpy(fullp,""); sprintf(fullp,"%s%s",xpath,findData.cFileName); if(IsDirectory(fullp)) { ClearDirectory(fullp,recurs,deldirs); strcat(fullp,"\\"); if(deldirs) RemoveDirectory(fullp); } else { SetFileAttributes(fullp,128); DeleteFile(fullp); } } ZeroMemory((void*)&findData,sizeof(findData)); Go=FindNextFile(hFile,&findData); } FindClose(hFile); if(deldirs) RemoveDirectory(xpath); return TRUE; } //rate me or hate I am the mighty keeper of the book on knowledge . Contact me to get your copy .

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups