如何检查批处理文件中的所有参数是否都具有 .txt 文件扩展名?

How to check if all the arguments have the .txt file extension in a batch file?

我想检查 cmd 中是否有无限数量的参数具有 .txt 扩展名。 例如:myscript.bat test.txt test2.exe test3.txt 应该 return 一条错误消息,因为并非所有参数都具有 .txt 扩展名。

这很简单:为每个参数检查扩展名,如果它是错误的则停止:

@echo off
for %%A in (%*) do (
  if /i "%%~xA" neq ".txt" (
    echo Wrong extension found
    pause
    goto :eof
  )
)
echo all of them are .txt
pause

注意:an unlimited number of arguments 是相对的 - 命令行的长度是有限制的。