List files from folder and subfolders recursively in JMeter

泪湿孤枕 提交于 2019-12-11 22:25:10

问题


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());
}

回答1:


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());
}



回答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++
}



回答3:


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
}


来源:https://stackoverflow.com/questions/53187185/list-files-from-folder-and-subfolders-recursively-in-jmeter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!