【问题标题】:How to check if all the arguments have the .txt file extension in a batch file? [closed]如何检查批处理文件中的所有参数是否都具有 .txt 文件扩展名? [关闭]
【发布时间】:2019-11-14 10:13:38
【问题描述】:

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

【问题讨论】:

  • 欢迎来到 SO,新手。为了提供帮助,请将您尝试执行的命令作为文本复制/粘贴到问题中。请查看有关如何提出好问题的 SO 帮助信息。没有代码的问题一般去superuser.com

标签: batch-file cmd command-line-arguments


【解决方案1】:

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

@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 是相对的 - 命令行的长度是有限制的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-03-22
    • 2018-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-17
    • 2011-08-09
    相关资源
    最近更新 更多