DOS 批處理命令筆記

一、判斷x 變量是否在foo變量中函數

echo "%foo%" | findstr /C:"%x%"

** 二、 函數寫法** 參考: 函數定義和用法.net

@echo off  
set "aStr=Expect no changed, even if used in function"  
set "var1=Expect changed"  
echo.aStr before: %aStr%  
echo.var1 before: %var1%  
call:myGetFunc var1  
echo.aStr after : %aStr%  
echo.var1 after : %var1%  
echo.&pause&goto:eof  
::--------------------------------------------------------  
::-- Function section starts below here  
::--------------------------------------------------------  
:myGetFunc    - passing a variable by reference  
SETLOCAL  
set "aStr=DosTips"  
( ENDLOCAL  
    set "%~1=%aStr%"  
)  
goto:eof  
:myGetFunc2    - passing a variable by reference  
SETLOCAL  
set "aStr=DosTips"  
ENDLOCAL&set "%~1=%aStr%"       &rem THIS ALSO WORKS FINE  
goto:eof

一個標準函數定義code

:myFunctionName    -- function description here  
::                 -- %~1: argument description here  
SETLOCAL  
REM.--function body here  
set LocalVar1=...  
set LocalVar2=...  
(ENDLOCAL & REM -- RETURN VALUES  
    IF "%~1" NEQ "" SET %~1=%LocalVar1%  
    IF "%~2" NEQ "" SET %~2=%LocalVar2%  
)  
GOTO:EOF
相關文章
相關標籤/搜索