這是我使用的代碼:
setlocal enabledelayedexpansion
set argCount=0
for %%x in (%*) do (
set /A argCount =1
set "argVec[!argCount!]=%%~x"
)
echo Number of processed arguments: %argCount%
set str1=hello
for /L %%i in (1,1,%argCount%) do (
set "str2=!argVec[%%i]!"
If NOT "!str1!"=="!str1:!str2!=!" set COND=1
echo !COND!
)
此代碼計算傳遞給批處理檔案的引數并將它們分配給環境變數。僅當引數字串包含 string時,才COND應定義環境變數。1hello
問題出在這一行:
If NOT "!str1!"=="!str1:!str2!=!" set COND=1
變數COND總是用 定義的1。
任何想法我該如何解決?
PS:str2用當前引數(引數)定義,以檢查hello.
uj5u.com熱心網友回復:
@ECHO OFF
setlocal enabledelayedexpansion
set argCount=0
set "str1=hello"
SET /a cond=0
for %%x in (%*) do (
set /A argCount =1
set "argVec[!argCount!]=%%~x"
IF NOT "%str1%"=="!str1:%%~x=!" SET /a cond=argcount
ECHO !cond!
)
echo Number of processed arguments: %argCount%
GOTO :EOF
將顯示在其中找到子字串的引數編號,cond并將cond設定為這些發現中的最后一個,如果沒有找到,則設定為 0
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/482008.html
