如何查看一个字符串是否长四个字母? – Windows批处理 [英] How can I see if a string is four letters long? – Windows Batch

查看:81
本文介绍了如何查看一个字符串是否长四个字母? – Windows批处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我正在研究Windows Batch脚本,我想知道输入字符串(文件名)是否正好是四个字母长.我想使用正则表达式或字符串匹配.

So I'm working on a Windows Batch script and I want to know if an input string (the name of a file) is exactly four letters long. I want to do it with regular expressions or string matching.

我尝试了以下操作,但没有成功...

I tried the following but it didn't work...

for /R "%windir%\system32" %%f in (*) do (
    set filename=%%~nf

    if not "!filename!"=="!filename:[a-z][a-z][a-z][a-z]=!" (
        echo %%~nf
    )
)

所以我的代码循环遍历\ system32中的所有文件.应该回显诸如mode.com之类的文件,但事实并非如此.

So my code loops through all the files in \system32. The files like mode.com should be echoed, but it's not the case.

推荐答案

这有效:

dir /B "%windir%\system32" | findstr "^[a-z][a-z][a-z][a-z]\."

在Windows 10上测试

Tested on Windows 10

这篇关于如何查看一个字符串是否长四个字母? – Windows批处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆