如何在遍历文件夹和子文件夹时查找具有预定义扩展名的文件?

How to find files with predefined extensions while looping through folders and sub folders?

我是 jmeter 的新手。你能告诉我如何遍历文件夹及其子文件夹以便记下具有预定义扩展名的文件的路径吗? 文件扩展名将作为数组给出。文件路径最好也保存在数组中。

示例解决方案使用 Beanshell Sampler and FileUtils.listFiles() 方法

import org.apache.commons.io.FileUtils;              

String path = System.getProperty("user.dir");
String[] extensions = {"xml"};


Collection files = FileUtils.listFiles(new File(path).getParentFile(), extensions, true);

for (File file : files) {
    log.info(file.getAbsolutePath());               
}

以上代码将在 JMeter 安装文件夹中递归找到的所有扩展名为 .xml 的文件打印到 jmeter.log 文件。

您可以将文件名添加到 JMeter Variables using vars shorthand for later reuse in the test. See How to Use BeanShell: JMeter's Favorite Built-in Component 文章中,而不是将文件名打印到日志中,以获取有关在 JMeter 中使用脚本、为其使用 Beanshell 以及使用 JMeter 和 Java API 的示例的更多信息Beanshell 测试元素.