使用它作为文本的批处理文件中的通配符

时间:2014-06-14 16:59:06

标签: batch-file

有没有办法在文本中使用通配符,例如

:type
set menu=
set /p menu=type:
if /i "%menu%" == "go* north" goto north

3 个答案:

答案 0 :(得分:0)

无法使用if,因为它会比较字符串。

尝试使用此代替if

echo %menu%|findstr /R /c:"go.* north">nul && goto north

PS:请不要问,如何批量编程AI; - )

答案 1 :(得分:0)

恐怕我不明白这段代码的问题是什么:

@echo off

:type
set menu=
set /p menu=type:
if /i "%menu%" == "go* north" echo It works!

示例:

C:\> test.bat
type:go* north
It works!

您应该意识到“星号”是这个字符:*,而“通配符”是指在文件名中插入星号或问号时。只要不将其用作文件名,就可以在字符串中插入星号或问号。

如果你想检查字符串是否以某些单词开头和结尾,你可以这样做:

if /i "%menu:~0,2%" == "go" if /i "%menu:~-5%" == "north" echo goto north

答案 2 :(得分:0)

echo %menu%|findstr /b /e /i /r /c:"go.* north" >nul
if errorlevel 1 goto notnorth
:north
echo Go North!

应该可以匹配任何以go开头并结束 space north`的字符串。


这是他完整的测试设置:

@ECHO OFF
SETLOCAL
set menu=
set /p menu=type:

ECHO.&ECHO "%menu%" was input

echo %menu%|findstr /b /e /i /r /c:"go.* north" >nul
if errorlevel 1 goto notnorth
:north
echo Go North!

PAUSE
GOTO :eof

:notnorth
echo The input was NOT go North
ECHO I've no idea what you want to do under those circumstances.

PAUSE

GOTO :EOF

我把它放在一个名为q24222234.bat的文件中,然后执行命令行:

cls&echo go north|q24222234&echo go somewhat north|q24222234&echo go a bit north|q24222234&echo gomostly north|q24222234&echo go south|q24222234&echo go east|q24222234

运行批处理文件多次,输入明显的字符串。结果是:

type:
"go north" was input
Go North!
Press any key to continue . . . 
type:
"go somewhat north" was input
Go North!
Press any key to continue . . . 
type:
"go a bit north" was input
Go North!
Press any key to continue . . . 
type:
"gomostly north" was input
Go North!
Press any key to continue . . . 
type:
"go south" was input
The input was NOT go North
I've no idea what you want to do under those circumstances.
Press any key to continue . . . 
type:
"go east" was input
The input was NOT go North
I've no idea what you want to do under those circumstances.
Press any key to continue . . .