2017-10-21 84 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,但相對較慢。

相關問題