Batch file 在批处理脚本中查找未知文本文件

Batch file 在批处理脚本中查找未知文本文件,batch-file,Batch File,我正在编写一个批处理脚本,用于检查目录中是否存在文本文件。我正在使用以下命令 if exist "test\dir\*.txt" ( echo txt file exist ) 好像这个命令在搜索时并没有区分大小写,所以我计划使用find命令,但我不能将它与*.txt一起使用 有人能告诉我如何使用find命令搜索未知文本文件吗?dir/s将搜索给定目录及其子目录中的文件模式。执行后检查ERRORLEVEL可以知道是否找到了文件。要测试文件是否存在,可以使用以下任一方法 if exis

我正在编写一个批处理脚本,用于检查目录中是否存在文本文件。我正在使用以下命令

if exist "test\dir\*.txt" 
(
    echo txt file exist
)
好像这个命令在搜索时并没有区分大小写,所以我计划使用find命令,但我不能将它与
*.txt
一起使用


有人能告诉我如何使用find命令搜索未知文本文件吗?

dir/s将搜索给定目录及其子目录中的文件模式。执行后检查ERRORLEVEL可以知道是否找到了文件。

要测试文件是否存在,可以使用以下任一方法

if exist "test\dir\*.txt" (
    echo File exists
) else (
    echo File does not exist
)


if exist "test\dir\*.txt" echo File exists


dir /a-d "test\dir\*.txt" > nul 2>nul
if errorlevel 1 (
    echo File does not exist
) else echo File exists


dir /a-d "test\dir\*.txt" >nul 2>nul && echo File Exists || echo File does not exist
只是列举习惯的方式

但是,正如您所说,所有这些构造都不区分大写或小写

find
用于查找文件中的文本,而不是用于文件搜索。但是,如果搜索必须区分大小写,则必须将前面示例中文件存在性的简单检查转换为文件枚举,然后在列表中搜索所需文件

dir /a-d /b "test\dir\*.txt" 2>nul | find ".txt" > nul
if errorlevel 1 (
    echo File does not exist
) else echo File exists
但这将返回文件exists for
myfile.txt.exe
。对于这种情况,
findstr
更灵活,允许指示在何处搜索字符串。在这种情况下,在该行的末尾

dir /a-d /b "test\dir\*.txt" 2>nul | findstr /l /e /c:".txt" > nul
if errorlevel 1 (
    echo File does not exist
) else echo File exists

这将枚举与
*.txt
匹配的文件,并在行末尾(
/e
开关)以小写形式(
/l
开关)
.txt
/c:
参数)过滤具有文本的文件列表

对不起,我听不懂您的描述。您能举个例子吗?您的代码应该在第一行末尾的空格后有
)。