windows 解析批处理文件中的字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12629775/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Parsing string in batch file
提问by user869525
I have the following string:
我有以下字符串:
MyProject/Architecture=32bit,BuildType=Debug,OS=winpc
MyProject/Architecture=32bit,BuildType=Debug,OS=winpc
I would like to be able to grab the values 32bit, Debug, and winpc and store them in variables named Architecture, BuildType, and OS to reference later in the batch script. I'm normally a Unix guy so this is new territory for me. Any help would be greatly appreciated!
我希望能够获取 32bit、Debug 和 winpc 的值并将它们存储在名为 Architecture、BuildType 和 OS 的变量中,以便稍后在批处理脚本中引用。我通常是一个 Unix 人,所以这对我来说是一个新领域。任何帮助将不胜感激!
回答by D.Shawley
This should do it:
这应该这样做:
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
The batch FOR
loop is a pretty interesting piece of machinery. Type FOR /?
in a console for a description of some of the crazy stuff it can do.
批处理FOR
循环是一个非常有趣的机器。键入FOR /?
在控制台的一些疯狂的东西它可以做一个说明。
回答by dbenham
Here is an interesting solution that doesn't care how many or what order the name=value pairs are specified. The trick is to replace each comma with a linefeed character so that FOR /F will iterate each name=value pair. This should work as long as there is only one /
in the string.
这是一个有趣的解决方案,它不关心指定了 name=value 对的数量或顺序。诀窍是用换行符替换每个逗号,以便 FOR /F 将迭代每个 name=value 对。只要/
字符串中只有一个,这应该有效。
@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* delims==" %%B in ("!str:,=%%~A!") do set "%%B=%%C"
)
::Display the values
echo Architecture=%Architecture%
echo BuildType=%BuildType%
echo OS=%OS%
With a bit more code it can selectively parse out only name=value pairs that we are interested in. It also initializes the variables to undefined in case the variable is missing from the string.
使用更多的代码,它可以有选择地仅解析出我们感兴趣的 name=value 对。它还会将变量初始化为 undefined,以防字符串中缺少变量。
@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="
::Parse and conditionally set the values
for %%A in ("!LF!") do (
for /f "eol== tokens=1* delims==" %%B in ("!str:,=%%~A!") do (
if !vars: %%B ! neq !vars! set "%%B=%%C"
)
)
::Display the values
for %%A in (%vars%) do echo %%A=!%%A!
回答by LittleBobbyTables - Au Revtheitroad
Try the following:
请尝试以下操作:
@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