从另一个文件中提取字符串号
我目前正在处理一个批处理文件,该文件应该从另一个文件读取版本号。
基本上,我只需要从另一个文档中提取字符串并从中获取数字(随时间变化)。
set dir=..\folder1\file1.vcxproj
:: I want to get a string from %dir% with a specific beginning and then extract the version number from it
set string=... ("<PlatformToolset>*")
set vernum=... ("..>v142<..)
那就是我想实现的目标:
echo %string%
<PlatformToolset>v142</PlatformToolset>
echo %vernum%
142
希望我能描述我的问题。不幸的是,我对CMD的经验很少,在这方面,我很难表达自己。
我希望有人仍然可以帮助:)
I am currently working on a batch file that is supposed to read a version number from another file.
Basically, I just need to extract the string from the other document and get the number from it (number changes over time).
set dir=..\folder1\file1.vcxproj
:: I want to get a string from %dir% with a specific beginning and then extract the version number from it
set string=... ("<PlatformToolset>*")
set vernum=... ("..>v142<..)
Thats what I want to achieve:
echo %string%
<PlatformToolset>v142</PlatformToolset>
echo %vernum%
142
I hope I could describe my problem. Unfortunately I have little experience with cmd and it is difficult for me to articulate myself in this respect.
I hope someone can still help :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
dir
是一个糟糕的名称,有两个原因。第一个dir
是批处理中的一个关键字,然后意味着它似乎是文件时是一个目录。使用
设置“ var1 = value”
用于设置字符串值 - 这避免了落后空间引起的问题。设置算术值(set/a
)不需要引用,因此假定您要做的是从字符串
&lt中提取
在文件142
; platformToolSet&gt; v142&lt;/platformToolSet&gt;dir
(请更改该名称)中,请注意,位置和报价类型(
'
''> “ )很关键。<代码>读取文件的每一行并分析该行,将第一个“令牌”(1)分配给
%% B
和第二(2)至%% C
被认为是
&lt; deLimiter序列&gt;&lt;
A 定界符序列是
delims =
option中指定的任何一个或多个字符;在这种情况下,&lt; ,&gt; 和 spaceso- so- white
%% a
(token 1)是PlatformToolSet
,我们要分配%% C
(令牌2)为vernum
so
vernum
将成为V142
使用包含您发布的测试行的文件。set
命令背后的象形文字由set/?
从提示中解释 - 它在vernum
中取值,从“字符1开始) “(字符编号以 0 开始),并将结果分配给变量vernum
有关的更多详细信息,请参见体操,请参阅
>对于 /?< /code>从提示符中。
dir
is a poor name for a variable for two reasons. Firstdir
is a keyword in batch, and then it implies that it's a directory when it seems to be a file.Use
set "var1=value"
for setting STRING values - this avoids problems caused by trailing spaces. Quotes are not needed for setting arithmetic values (set /a
)So presuming that what you're doing is to extract the
142
from the string<PlatformToolset>v142</PlatformToolset>
found in the filedir
(please change that name)note that the location and quote type (
'
or"
) are critical.The
for
reads each line of the file and analyses that line, assigning the first "token" (1) to%%b
and the second (2) to%%c
.The line is regarded as being
<delimiter sequence><token1><delimiter sequence><token2><delimiter sequence><token3><delimiter sequence><token4>
A delimiter sequence is one or more of any of the characters specified in the
delims=
option; in this case, <, > and SpaceSo - where
%%a
(token 1) isPlatformToolset
, we want to assign%%c
(token 2) tovernum
so
vernum
will becomev142
with a file containing the test line you've posted.And the hieroglyphics behind the
set
command is explained byset /?
from the prompt - it takes the value invernum
, starting at "character 1" (where the character numbering starts at 0) and assigns the result to the variablevernum
for more detail about
for
gymnastics, seefor /?
from the prompt.