Find files inside a folder in your automated test

Context: you have a folder containing some files. You need to find all the files whose name contains some expected value in this folder and all its’ sub-folders. Or you need to find all files with a specified extension in the folder. Maybe you need to count how many files with a specified size are found in the folder. How can you do these tasks easily?

You can use the FileUtils class from the Apache Commons library for such a task. The ‘listFiles’ method from this class will return a Collection of all the files found inside a specified baseFolder. Specifically, a Collection of java.io.File items is returned.

Once you have this, you can perform all actions on those files that you need in your tests, like counting how many they are, reading their content, updating their content, deleting them, moving them and so on.

The required import

In order to use this method in your tests, you will need to do an import as follows:

import org.apache.commons.io.FileUtils;

You also need to have the dependency to the Apache Commons IO library set up in your project. You can find its latest version in the Maven Central Repository.

Method signature

The signature of this method looks like this:

listFiles(
final File directory, final String[] extensions, final boolean recursive)
  • the first parameter, the ‘directory‘ will specify inside which folder on the computer to search for the desired files
  • the second parameter, the ‘extensions‘ can be either null or can have a value. It its’ value is null, all files will be considered, no matter their extension. If you want to get files with only one or several extensions, you can specify them in this parameter which is an array of Strings.
  • the third parameter, the ‘recursive‘ boolean one, specifies whether you want to look recursively in all the folders found inside the ‘directory’, which is the first parameter.

The result of calling this method can be stored in a variable of type Collection.

Usage examples
  • searching for all files inside a baseFolder, no matter the extension, and in all sub-folders of the baseFolder
Collection files = FileUtils.listFiles(new File(baseFolder), null, true);
  • searching for all .png, .jpg and .bmp files inside a baseFolder, but only in the baseFolder and not in its’ sub-folders
Collection files = FileUtils.listFiles(new File(baseFolder), new String[]{"png", "jpg", "bmp"}, false);
Counting how many results were returned

If, in your test, you need to check how many files were found based on your search criteria, you can do that easily by calling the ‘size()’ method on the variable to which you stored the results of the ‘listFiles()’ method.

files.size()
Iterating over the result

Iterating over the Collection of files can be useful when you want to do some changes on one or more of them. For example, maybe you want to do some changes on all files whose name contains a specified value. Such a task can be accomplished as follows:

for (Iterator iterator = files.iterator(); iterator.hasNext(); ) { 
File file = (File) iterator.next(); 
if (file.getName().contains("someDesiredValue")) {      //here is the code for updating the file 
}}

One thought on “Find files inside a folder in your automated test”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.