Recursively Delete Directory





11
Date Submitted Wed. Mar. 29th, 2006 12:04 PM
Revision 1 of 1
Coder mattrmiller
Tags Delete | Directory | Java | Recursive | REMOVE
Comments 1 comments
Recursively delete a directory, emptying all contents.


        // Delete directory
        public static boolean deleteDir(String strFile)
        {
                // Declare variables variables
                File fDir = new File(strFile);
                String[] strChildren = null;
                boolean bRet = false;
               
                // Validate directory
                if (fDir.isDirectory())
                {
                        // -- Get children
                        strChildren = fDir.list();
                       
                        // -- Go through each
                        for (int i = 0; i < strChildren.length; i++)
                        {
                                bRet = deleteDir(new File(fDir, strChildren[i]).getAbsolutePath());
                                if (!bRet)
                                {
                                        return false;
                                }
                        }
                }
               
                // The directory is now empty so delete it
                return fDir.delete();
        }

 

Matthew R. Miller

www.bluecreststudios.com
=================
Matthew R. Miller

http://bluecreststudios.com
http://www.codeandcoffee.com

Comments

Comments unnecesary conversions
Sat. Oct. 28th, 2006 2:05 PM    Scripter SCoon

Voting