如何检查cmd批处理脚本中是否存在具有特定模式的文件名?

发布于 2025-01-09 23:05:20 字数 283 浏览 0 评论 0原文

如何在 if exit 语句中使用 [0-9]。我想检查第二个数字是1-9的文件是否存在。

If exist "filepath\\a[1-9]*" (echo "yes")

看来 [1-9] 通配符在 if exit 语句中不起作用。

您知道如何实施这项检查吗?

例如 当名为 a1 (a2da) 的文件存在但不存在 a0 (a0ss) 时打印“yes”

How to use [0-9] in if exist statement. I want to check a file with second digit is 1-9 exist or not.

If exist "filepath\\a[1-9]*" (echo "yes")

It seems [1-9] wildcard does not work in if exist statement.

Do you have any idea how to implement this check?

e.g.
print "yes" when a file named a1 (a2da) exists, but not a0 (a0ss)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

柳若烟 2025-01-16 23:05:21

由于这是一个批处理文件,并且只写入一次,因此几乎不需要尝试使命令尽可能短。因此,只需使用一个 dir 命令和一个 echo 命令,而不是当前接受的答案,该答案实际上运行十个 if 命令,可能是九个 set 命令和一个 echo 命令。

@Dir "filepath\a1*" "filepath\a2*" "filepath\a3*" "filepath\a4*" "filepath\a5*" "filepath\a6*" "filepath\a7*" "filepath\a8*" "filepath\a9*" /B /A:-D 2>NUL 1>&2 && Echo Yes

由于 filepath 是恒定的,您甚至可以这样做:

@CD /D "filepath" 2>NUL && (Dir a1* a2* a3* a4* a5* a6* a7* a8* a9* /B /A:-D 2>NUL 1>&2 && Echo Yes)

但是请注意,您的问题不够清晰,无法确定您的完整任务。如果 a[123456789]... 存在于 filepath 中,此示例将打印 Yes,无论名为 a0 的文件是否存在,它都会执行此操作... 也存在于 filepath 中。

As this is a batch file, and is only written once, there's little need to try to make your command as short as possible. Therefore just use one dir command and one echo command, instead of the currently accepted answer which essentially runs ten if commands, potentially nine set commands, and one echo command.

@Dir "filepath\a1*" "filepath\a2*" "filepath\a3*" "filepath\a4*" "filepath\a5*" "filepath\a6*" "filepath\a7*" "filepath\a8*" "filepath\a9*" /B /A:-D 2>NUL 1>&2 && Echo Yes

As filepath is constant, you could even do it like this:

@CD /D "filepath" 2>NUL && (Dir a1* a2* a3* a4* a5* a6* a7* a8* a9* /B /A:-D 2>NUL 1>&2 && Echo Yes)

Please note however, that your question is not clear enough to determine your full task. This example will print Yes if a[123456789]… exists in filepath, it will do that regardless of whether a file named a0… also exists in filepath.

苍暮颜 2025-01-16 23:05:21

cmd 中唯一支持 REGEX(残缺版本)的命令是 findstr。因此,您可以使用它来完成您的任务:

dir /a-d "%filepath%\a*" | findstr /ib "a[123456789].*" >nul && echo yes || echo no

注意,这还会找到诸如 a12anything.txtA2anything.txt 之类的文件,但不会找到 a0anything.txt >。

The one and only command in cmd that supports (a crippled version of) REGEX is findstr. So you can use that to complete your task:

dir /a-d "%filepath%\a*" | findstr /ib "a[123456789].*" >nul && echo yes || echo no

Note, this will also find files like a12anything.txt and A2anything.txt but not a0anything.txt.

默嘫て 2025-01-16 23:05:20

如果您只检查第二个字符,而不考虑其后的其他字符,则可以使用如下内容:

for /l %%n in (1,1,9) do if exist "a%%n*" set "ANS=%%n"
if defined ANS echo yes

If you only check the second character, without considering the other characters after it, you can use something like this:

for /l %%n in (1,1,9) do if exist "a%%n*" set "ANS=%%n"
if defined ANS echo yes
北笙凉宸 2025-01-16 23:05:20

另一种方法是使用 PowerShell。如果您使用的是受支持的 Windows 系统,则它已安装。

powershell -NoLogo -NoProfile -Command "Test-Path -Path '.\a[1-9]*'"

Another way to do this would be to use PowerShell. If you are on a supported Windows system it is already installed.

powershell -NoLogo -NoProfile -Command "Test-Path -Path '.\a[1-9]*'"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文