2017-10-21 131 views
1

下面的脚本命令检查相匹配的命令行参数%1对固定字ALA如何匹配命令行参数在批处理文件

<code> 
     @echo off 
    set one=%1 
    set two=%2 
    If NOT "%one%"=="%one:ala=%" (echo the first argument contains the word "ala") 
    else (echo no matching !) 
    </code> 

如何使用参数取代固定字“ALA” %2从命令行改为。 (因为用%2简单替换ala不起作用)。 比较参数字符串有没有更好的解决方案?

回答

0

您需要使用延迟扩展来完成该类型的字符串替换。

@echo off 
setlocal enabledelayedexpansion 
set "one=%~1" 
set "two=%~2" 
If NOT "%one%"=="!one:%two%=!" (
    echo the first argument contains the word "%two%" 
) else ( 
    echo no matching 
) 

而且您还可以使用CALL命令使用技术进行延迟扩展。

@echo off 
set "one=%~1" 
CALL set "two=%%one:%~2=%%" 
If NOT "%one%"=="%two%" (
    echo the first argument contains the word "%two%" 
) else ( 
    echo no matching 
) 
0
@ECHO OFF 
SETLOCAL 
ECHO %~1|FIND "%~2">NUL 
IF ERRORLEVEL 1 (
ECHO "%~2" NOT found IN "%~1" 
) ELSE (
ECHO "%~2" WAS found IN "%~1" 
) 

GOTO :EOF 

使用find设施。这避免了delayedexpansion,但相对较慢。

相关问题