0

I need to delete all the files and folders in a directory but i need to .svn folder in this so that i can commit and delete the folder everytime. My below code worked but it retains .svn parent folder only but rest of its child .svn folders are deleted

my code:

      if (pFile.exists() ) {
        System.out.println(pFile.getName());
        if (pFile.isDirectory()) {
            if (pFile.list().length == 0) {
                 System.out.println("0>"+pFile.getName());
                pFile.delete();
            } else {
                System.out.println("1>"+pFile.getName());
                String[] strFiles = pFile.list();

                for (String strFilename : strFiles) {
                    File fileToDelete = new File(pFile, strFilename);
                    System.out.println("2>"+fileToDelete.getName());
                    if(fileToDelete.getName()==".svn")
                    {
                        // Do Nothing
                        break;
                    }
                    else
                    {
                    delete(fileToDelete);
                    }
                }
            }
        } else {
             System.out.println("3>"+pFile.getName());
           pFile.delete();
        }
    }
user3354849
  • 11
  • 2
  • 8
  • 1
    `==".svn"` that is not how String values should be compared. Read more about it in [how-do-i-compare-strings-in-java](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – Pshemo Feb 26 '14 at 11:15

3 Answers3

0

Need to modify condition as below. Here break will stop loop where as continue will skip only current deletion (ie, folder as .svn)

 if(fileToDelete.getName()!=null && fileToDelete.getName().equals(".svn")){
     // Do Nothing
     continue;
}
Dark Knight
  • 8,218
  • 4
  • 39
  • 58
0

You can use pFile.isHidden() to check if it is a hidden file. In addition you can list all files in a folder with File.listFiles() instead of File.list() so you dont have to create a new File.

Yser
  • 2,086
  • 22
  • 28
0

The other suggestions should solve your issue else you say, you need to delete all files and folders in a directory. So may be you are deleting all child folders that contain .svn in them and so you dont see them remain.

Smitt
  • 178
  • 7