top of page

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 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:


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:

final File directory, final String[] extensions, final boolean recursive) 
  1. the first parameter, the 'directory' will specify inside which folder on the computer to search for the desired files

  2. 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.

  3. 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

  1. 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); 
  1. 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.


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); 
if (file.getName().contains("someDesiredValue")) {      //here is the code for updating the file 

Recent Posts

See All

Creating an Architecture for Your Automated Tests Writing Automated Tests in Small Increments 4 Times Your Automation Passed While Bugs Were Present Now That You’ve Created Automated Tests, Run Them!

This week my newest article was released, this time on how to use Log4j to log relevant test automation information:

bottom of page