This 0ne Pr0grammer
This 0ne Pr0grammer

Reputation: 2662

Recursively Deleting a Directory

I have this section of code:

public static void delete(File f) throws IOException 
    {
        if (f.isDirectory()) 
        {
            for (File c : f.listFiles())
            {
                delete(c);
            }
        }
        else if (!f.delete())
        {
             throw new FileNotFoundException("Failed to delete file: " + f);
        }
    }   



 public static void traverseDelete(File directory) throws FileNotFoundException, InterruptedException
    {
        //Get all files in directory
        File[] files = directory.listFiles();
        for (File file : files)
        {
            if (file.getName().equalsIgnoreCase("word"))
            {
                boolean containsMedia = false;

                File[] filesInWordFolder = file.listFiles();

                for ( File file2 : filesInWordFolder ) 
                {
                    if ( file2.getName().contains("media"))
                    {
                        containsMedia = true;
                        break;
                    }
                }

                if (containsMedia == false)
                {
                    try 
                    {
                        delete(file.getParentFile());
                    } 
                    catch (IOException e) 
                    {
                        e.printStackTrace();
                    }
                }
            }
            else if (file.isDirectory())
            {
                traverseDelete(file);
            }
        }
    }

Sorry for the lack of commenting, but it's pretty self-explanatory, I think. Essentially what the code is supposed to do is traverses a set of files in a given directory, if it encounters a directory named "word", then it should list out the contents of word, and then if a directory called "media" does NOT exist, recursively delete everything within the parent directory of "word" down.

My main concern comes from this conditional:

if(!filesInWordFolder.toString().contains("media"))

Is that the correct way to say if the files in that array does not contain an instance of "image", go ahead and delete?

Upvotes: 0

Views: 2397

Answers (2)

user372743
user372743

Reputation:

Well using toString() will give you a String representation of the file (in this case the files). The String representation should contain the file name. If your set purpose is to check for any instance of a file containing the word "media" in the directory, you are fine.

In the example you are printing the String representation of the File array. Instead you should iterate through the File array and check the String representation of each individual File as so:

for (int i = 0; i < file_array.length; i++) {
  if ((File)file_array[i]).toString().equals("your_search_term")) {
    // The file contains your search term
  } else {
    // Doesn't contain the search term.
  }
}

Upvotes: 0

Kal
Kal

Reputation: 24910

That won't work.

File[] filesInWordFolder = file.listFiles();

if(!filesInWordFolder.toString().contains("media"))

will give you a string representation of a File array -- which will typically have a reference.

You have to iterate through the files to find out if there's any in there that contain the word media.

boolean containsMedia = false;

for ( File file : filesInWordFolder ) {

if ( file.getName().contains("media") ){
 containsMedia = true;
break;
}

// now check your boolean
if ( !containsMedia ) {

Upvotes: 1

Related Questions