1

在 windows cmd 中,临时目录设置为

 C:\spec>echo %temp%
 C:\Users\mahmood\AppData\Local\Temp

还有一个文件 %temp%\specdev.txt 包含

 C:\spec>type %temp%\specdev.txt
 c:\cpu

现在当我执行这个命令

findstr -r "^[a-zA-Z]:$" %temp%\specdev.txt >nul 2>&1

它不返回任何东西!

C:\spec>findstr -r "^[a-zA-Z]:$" %temp%\specdev.txt >nul 2>&1

C:\spec>

问题是什么??你能解释一下这个命令是做什么的吗?它是批处理脚本的一部分。

4

3 回答 3

4

您看不到任何结果,因为所有控制台输出都重定向到 NUL:命令的最后一部分>nul将标准输出重定向到 NUL,2>&1并将错误输出重定向到标准输出(因此是 NUL)。

因为此命令是脚本的一部分,所以并不意味着它没有用:FINDSTR 在找到匹配项时将全局环境变量设置%ERRORLEVEL%为 0,在未找到匹配项时将其设置为 1。因此,脚本可以将所有输出发送到 NUL(而不是阻塞用户屏幕)并检查%ERRORLEVEL%以验证结果。

About the pattern this command is searching for, "^[a-zA-Z]:$" means that it searches for a line that only contains a single letter from "a" to "z" (uppercase and lowercase) and ends with a colon ":". Thus, the file %temp%\specdev.txt you described will not match the expression.

于 2012-04-30T18:18:42.997 回答
0

试试这个代码:

findstr -r "^[a-zA-Z]*$" %temp%\specdev.txt >nul 2>&1
于 2012-04-30T18:04:00.277 回答
0

问题是字符串与正则表达式不匹配,正则表达式匹配只包含一个字母后跟':'字符的行。

所以现在的问题是,你真正想要匹配什么模式?也许你想要:

"^[a-zA-Z]:"

这将匹配以字母开头后跟 a 的行':'(但随后可以在该行后面有其他字符)。但我怀疑你想要更复杂的东西。

于 2012-04-30T18:06:55.223 回答