parsingbatch file中的string

我有以下string:

MyProject/Architecture=32bit,BuildType=Debug,OS=winpc

我希望能够获取32位值,Debug和winpc的值,并将其存储在名为Architecture,BuildType和OS的variables中,以便稍后在批处理脚本中引用。 我通常是一个Unix的人,所以这是我的新领域。 任何帮助将不胜感激!

Solutions Collecting From Web of "parsingbatch file中的string"

这应该做到这一点:

 FOR /F "tokens=1-6 delims==," %%I IN ("MyProject/Architecture=32bit,BuildType=Debug,OS=winpc") DO ( ECHO I %%I, J %%J, K %%K, L %%L, M %%M, N %%N ) REM output is: I MyProject/Architecture, J 32bit, K BuildType, L Debug, M OS, N winpc 

批次FOR循环是一个非常有趣的机器。 键入FOR /? 在一个控制台中描述一些疯狂的东西,它可以做的。

这里有一个有趣的解决方案,不关心指定了name = value对的数量或顺序。 诀窍是用换行字符替换每个逗号,以便FOR / F将迭代每个名称=值对。 只要字符串中只有一个,就应该工作。

 @echo off setlocal enableDelayedExpansion set "str=MyProject/Architecture=32bit,BuildType=Debug,OS=winpc" ::Eliminate the leading project info set "str=%str:*/=%" ::Define a variable containing a LineFeed character set LF=^ ::The above 2 empty lines are critical - do not remove ::Parse and set the values for %%A in ("!LF!") do ( for /f "eol== tokens=1,2 delims==" %%B in ("!str:,=%%~A!") do set "%%B=%%C" ) ::Display the values echo Architecture=%Architecture% echo BuildType=%BuildType% echo OS=%OS% 

多一点的代码可以有选择地解析出我们感兴趣的名称=值对。它还将变量初始化为未定义的变量,以防变量从字符串中丢失。

 @echo off setlocal enableDelayedExpansion set "str=MyProject/Architecture=32bit,BuildType=Debug,OS=winpc" ::Eliminate the leading project info set "str=%str:*/=%" ::Define a variable containing a LineFeed character set LF=^ ::The above 2 empty lines are critical - do not remove ::Define the variables we are interested in set "vars=Architecture BuildType OS" ::Clear any existing values for %%A in (%vars%) do set "%%A=" ::Set the values for %%A in ("!LF!") do ( for /f "eol== tokens=1,2 delims==" %%B in ("!str:,=%%~A!") do ( for %%D in (%vars%) do if "%%B"=="%%D" set "%%B=%%C" ) ) ::Display the values for %%A in (%vars%) do echo %%A=!%%A! 

尝试以下操作:

 @ECHO OFF SET Var=MyProject/Architecture=32bit,BuildType=Debug,OS=winpc FOR /F "tokens=1,2,3 delims=," %%A IN ("%Var%") DO ( FOR /F "tokens=1,2 delims==" %%D IN ("%%A") DO ( SET Architecture=%%E ) FOR /F "tokens=1,2 delims==" %%D IN ("%%B") DO ( SET BuildType=%%E ) FOR /F "tokens=1,2 delims==" %%D IN ("%%C") DO ( SET OS=%%E ) ) ECHO %Architecture% ECHO %BuildType% ECHO %OS% PAUSE