简体   繁体   English

如何在Java中为目录的所有子目录递归运行脚本

[英]How to run script recursively for all the subdirectories of a directory in Java

I am writing a Junit test.我正在编写一个 Junit 测试。 I have several components inside of a big module.我在一个大模块中有几个组件。 I have managed to modify a exisitng script, which will scan through one component for now (hard coded the path) and checks the testcases and executes the tests.我设法修改了一个现有脚本,该脚本现在将扫描一个组件(对路径进行硬编码)并检查测试用例并执行测试。

Root Directory: Module根目录:模块

List of sub directories:子目录列表:

Component1 Component2 Component3 Component4 Component5 .组件 1 组件 2 组件 3 组件 4 组件 5 。 . . . . . . Component6组件 6

I have the following code:我有以下代码:

@RunWith(Parameterized.class)
public class ContentTest {
    
    private static final Logger log = LoggerFactory.getLogger(ContentTest.class);
    @Parameter
    public String testCase;    
    
    @Parameters( name = "{index}: {0}" )
    public static String[] data() {
        FileSystem fs = FileSystems.getDefault();
        Path p = fs.getPath("Component1/");
        try {
            PathMatcher matcher = FileSystems.getDefault().getPathMatcher("glob:**/*TEST.tab");
            Set<String> tests = 
                    Files.walk(p,FileVisitOption.FOLLOW_LINKS)
            .filter(x -> matcher.matches(x)|| false)
            .map(x -> x.getFileName().toFile().getName())
            .map(x -> x.substring(0, x.length()-4))
            .collect(Collectors.toSet());
            log.info("Going to run the following tests " );
            tests.stream().forEach(log::info);
            String [] result = (String[]) tests.toArray(new String[tests.size()]);
            return result;
        } catch (IOException e) {
            log.error("Could not get testcases." + e);
        }
        return new String[0];
    }
    
    
    @Test
    public void test1() {
        TestCommand tc = new TestCommand();
        tc.setBaseDir(new HdfsResource("Component1/"));
        List<String> args = new ArrayList<>();
        args.add(testCase);
        args.add("--settings");
        tc.run(args.toArray(new String[args.size()]));
    }   


 }

But, in mycase I have tests inside all the Components.但是,在我的情况下,我对所有组件进行了测试。

Now my query is how can I, modify or change the above script in such a way that.现在我的查询是如何以这种方式修改或更改上述脚本。 It will execute tests from all the Components one after the other.它将一个接一个地从所有组件执行测试。

Any ideas and suggestions are helpful!!任何想法和建议都是有帮助的!!

This can be done fairly simply with a recursive method, ie:这可以通过递归方法相当简单地完成,即:


public class MyClass {
    private List<File> directories;

    public static void recurse() {
    
        String[] directories = file.list(new FilenameFilter() {
          @Override
          public boolean accept(File current, String name) {
            return new File(current, name).isDirectory();
          }
        });
        
        for (String dir : directories) {
            File f = new File(dir) {
                if (f.exists()) {
                    this.directories.add(f);
                } else {
                    System.out.println("Directory " + f.getAbsolutePath() + " does not exist");
                }
        }
    }

    public static void main(String[] args) {
        System.out.println("Loop beginning");
        recurse();
        System.out.println("Loop ending");
    }
        

Hope this helped希望这有帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Java-递归获取目录和子目录中的所有文件 - Java - Get all files in directories and subdirectories recursively 在 Java 中递归压缩包含任意数量的文件和子目录的目录? - Recursively ZIP a directory containing any number of files and subdirectories in Java? 使用 Java 递归列出目录中的所有文件 - List all files from a directory recursively with Java 递归监视Java中的目录和所有子目录 - Recursively monitor a directory and all sub directories in java 删除所有文件和子目录,但在Java中保持当前目录为空 - Delete all files and subdirectories but keep the current directory empty in Java 如何以递归方式列出目录中的所有.rar文件 - How to recursively list all .rar files in a directory 如何使用Java的目录流仅在目录中而不是其他子目录中获取文件/子目录 - How to use Java's directory stream to get files/subdirectories only within directory not other subdirectories 列出目录中的所有子目录和文件 - list all subdirectories and file in a directory 如何在hadoop hdfs中列出目录及其子目录中的所有文件 - How to list all files in a directory and its subdirectories in hadoop hdfs 如何使用正则表达式获取目录中不包含子目录的所有文件? - How to get all files in a directory with exclusion of subdirectories using regular expression?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM