0

I use BeanShell Sampler in JMeter to list all the files from a folder. It lists files only from directory and unable to do the same in subdirectories

File folder = new File("C:\\_private\\Files\\input");

File[] files = folder.listFiles(new FileFilter() {
    public boolean accept(File file) {
        return file.isFile();
    }
});

for (int i=0; i < files.length; i++) {
    vars.put("file_" + i, files[i].getAbsolutePath());
}
Ori Marko
  • 56,308
  • 23
  • 131
  • 233
plaidshirt
  • 5,189
  • 19
  • 91
  • 181
  • 1
    Possible duplicate of [List all files from a directory recursively with Java](https://stackoverflow.com/questions/2534632/list-all-files-from-a-directory-recursively-with-java) – Deepak Gunasekaran Nov 07 '18 at 10:10

3 Answers3

2

Move to use JSR223 Sampler with the following code using FileUtils:

import org.apache.commons.io.FileUtils;
List<File> files = FileUtils.listFiles(new File("C:\\_private\\Files\\input"), null, true);

Notice to replace files.length with files.size():

for (int i=0; i < files.size(); i++) {
    vars.put("file_" + i, files[i].getAbsolutePath());
}
Ori Marko
  • 56,308
  • 23
  • 131
  • 233
2

Since JMeter 3.1 it's recommended to use JSR223 Test Elements and Groovy language for any form of scripting mainly because Groovy performance is much better comparing to other scripting options

Groovy in its turn provides File.eachFileRecurse() function which is exactly what you're looking for.

Example code:

def index = 1

new File('c:/apps/jmeter/bin').eachFileRecurse(groovy.io.FileType.FILES) {
    vars.put('file_' + index, it.getAbsolutePath())
    index++
}
Dmitri T
  • 159,985
  • 5
  • 83
  • 133
1

You'll need to do it recursively. You can list all the directories the same way you did for the files, and then call the function you created, recursively. When you then call the function with your initial file, it will traverse the tree structure and give you all files in a list. To add to a list use addAll.

def listFiles(File folder) {
    ... // Recursive function
}
SBylemans
  • 1,764
  • 13
  • 28