我正在努力使用BATCH文件,希望有人可以帮助我。
我想解析传递给.BAT Windows文件的参数。基本上,我将收到括号内的2组参数。一般语法是:
BuildApplication.bat (params_for_application_1) (params_for_ application2)
因此,我可以获取应用程序1的参数和应用程序2的参数,并从批处理文件中执行这两个应用程序,例如:
MyApp1.exe params_for_application_1
MyApp2.exe params_for_application_2
这是对批处理文件的常见调用:
BuildApplication.bat (-m 15000 -I "include/fasm" -DDEBUG="yes" "C:\my_projects\test project\test.asm") ("C:\my_projects\test project\test.obj" -i 400)
所以我想抓住上面两组参数。
params_for_application_1 = -m 15000 -I "include/fasm" -DDEBUG="yes" "C:\my_projects\test project\test.asm" params_for_application_2 = "C:\my_projects\test project\test.obj" -i 400
答案 0 :(得分:2)
@echo off
setlocal enableDelayedExpansion
set "arg_line=%*"
for /f "useback tokens=1,3 delims=()" %%a in ('!arg_line!') do (
set "param1=%%a"
set "param2=%%b"
)
echo %param1%
echo %param2%
答案 1 :(得分:2)
您可以使用以下内容在整个命令行参数字符串的(
和)
之间提取字符串,并将它们存储到名为$ARRAY[]
的数组中:
@echo off
setlocal EnableExtensions DisableDelayedExpansion
rem // Initialise variables here:
set ^"STRING=%*^"
set /A "IDX=0"
rem /* Loop through the entire command line argument string and store
rem every part in between `(`/`)` into the array `$ARRAY[]`: */
:LOOP
rem // Terminate loop if no string is left:
if not defined STRING goto :NEXT
setlocal EnableDelayedExpansion
rem // Check if string contains `(`:
if "!STRING!"=="!STRING:*(=!" (
endlocal
rem // No more `(` found, so terminate loop:
goto :NEXT
)else (
endlocal
rem // `(` found, so increment index:
set /A "IDX+=1"
)
setlocal EnableDelayedExpansion
rem // Extract part between first `(` and first following `)`:
for /F "tokens=1,* delims=) eol=)" %%K in (" !STRING:*(=!") do (
endlocal
rem /* Store extracted part with a preceding SPACE into array variable;
rem this SPACE makes empty strings between `(`/`)` not to be lost: */
set "$ARRAY[%IDX%]=%%K"
rem // Store the remaining string behind `)`:
set "STRING=%%L"
setlocal EnableDelayedExpansion
)
endlocal
rem // Jump back to beginning of loop:
goto :LOOP
:NEXT
rem // Simply return the array, including the preceding SPACE:
2> nul set $ARRAY[
endlocal
exit /B