简体   繁体   English

如何递归列出指定目录中所需文件类型的所有文件?

[英]How to recursively list all files of desired file type in a specified directory?

I want list all .java files in the desired directory using .bat script in Windows.我想在 Windows 中使用.bat脚本列出所需目录中的所有.java文件。 When I run this:当我运行这个:

set JMETALHOME=%cd%
dir %JMETALHOME%\jmetal\problems /S /B 

it lists all the files in the problems directory.它列出了problems目录中的所有文件。 But when I want to filter the files only to the java files:但是当我只想将文件过滤到 java 文件时:

set JMETALHOME=%cd%
dir %JMETALHOME%\jmetal\problems /S /B *.java > sources.txt

it lists all the .java files in the %JMETALHOME% folder.它列出了%JMETALHOME%文件夹中的所有.java文件。

There are two common methods to get names of all files with a specific file extension found in a directory tree recursively with full path.有两种常用的方法可以通过完整路径递归地获取在目录树中找到的具有特定文件扩展名的所有文件的名称。 One common method is using command DIR and the other common method is using command FOR .一种常用方法是使用命令DIR ,另一种常用方法是使用命令FOR

Example code demonstrating both methods for *.java and *.html files:演示 *.java 和 *.html 文件的两种方法的示例代码:

@echo off
set "JMETALHOME=%CD%"

dir "%JMETALHOME%\jmetal\problems\*.java" "%JMETALHOME%\jmetal\problems\*.html" /B /S >SourcesDir.txt

if exist SourcesFor1.txt del SourcesFor1.txt
for /R "%JMETALHOME%\jmetal\problems" %%I in (*.java *.html) do echo %%I>>SourcesFor1.txt

(for /R "%JMETALHOME%\jmetal\problems" %%I in (*.java *.html) do echo %%I)>SourcesFor2.txt

DIR requires the specification of 1 or even more wildcard patterns together with path (absolute or relative). DIR需要指定 1 个或更多通配符模式以及路径(绝对或相对)。

FOR offers the possibility to specify the path separately from the wildcard patterns. FOR提供了与通配符模式分开指定路径的可能性。

FOR outputs the found file names line by line which requires either usage of >> redirection operator with an extra deletion of a perhaps already existing output file before running FOR as demonstrated above with SourcesFor1.txt example, or embedding FOR itself in a block and use > to redirect everything output by the block into a file overwriting a perhaps already existing file as demonstrated with SourcesFor2.txt example. FOR逐行输出找到的文件名,这需要使用>>重定向运算符并在运行FOR之前额外删除可能已经存在的输出文件,如上面使用SourcesFor1.txt示例所示,或者将FOR本身嵌入到块中并使用>将块输出的所有内容重定向到一个文件中,覆盖可能已经存在的文件,如SourcesFor2.txt示例所示。

On second FOR example make sure not having a space between echo %%I and closing parenthesis ) of the block because otherwise this space would be appended on each file name written into output file SourcesFor2.txt .在第二个FOR示例中,请确保块的echo %%ISourcesFor2.txt括号)之间没有空格,否则该空格将附加在写入输出文件SourcesFor2.txt每个文件名上。

For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.要了解使用的命令及其工作原理,请打开命令提示符窗口,在那里执行以下命令,并仔细阅读为每个命令显示的所有帮助页面。

  • del /?
  • dir /?
  • echo /?
  • for /?
  • if /?
  • set /?

Read also the Microsoft article about Using command redirection operators .另请阅读有关使用命令重定向运算符的 Microsoft 文章。

暂无
暂无

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

相关问题 如何以递归方式列出Windows .bat文件中* .mp3类型的所有文件? - How do I recursively list all files of type *.mp3 in a Windows .bat file? 列出所有处理指定文件类型的应用程序 - List all applications that handle a specified file type Powershell以递归方式获取目录中文件的过滤列表 - Powershell Recursively getting filtered list of files in directory Windows 7文件权限-递归强制所有当前和将来文件的目录权限 - Windows 7 File Permissions - Force directory permissions recursively to all current and future files 如何在目录树中递归地将所有文件和文件夹名称中的所有空格替换为下划线? - How to replace all spaces by underscores in all file and folder names recursively in a directory tree? (批次)如何递归删除目录中除带有前导。之外的所有文件/文件夹? - (Batch) How to recursively delete all files/folders in a directory except those with a leading .? 二进制比较所有文件与指定目录(和子目录)中的所有文件 - Binary compare all files against all files in a specified directory (and subdirectories) R:获取目录中所有文件的短文件名(文件名)列表 - R: get a list of short file names (filenames) for all files in a directory 如何根据文件路径中的目录递归地删除* .bak文件,早于特定日期? - How to delete *.bak files recursively older than a specific date depending on directory in file path? 如何递归删除目录中的最小文件? - How to delete the smallest file in a directory recursively?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM