从另一个文件中提取字符串号

发布于 2025-01-28 02:05:56 字数 530 浏览 3 评论 0原文

我目前正在处理一个批处理文件,该文件应该从另一个文件读取版本号。

基本上,我只需要从另一个文档中提取字符串并从中获取数字(随时间变化)。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

所谓喜欢 2025-02-04 02:05:56

dir是一个糟糕的名称,有两个原因。第一个dir是批处理中的一个关键字,然后意味着它似乎是文件时是一个目录。

使用设置“ var1 = value”用于设置字符串值 - 这避免了落后空间引起的问题。设置算术值(set/a)不需要引用,

因此假定您要做的是从字符串

&lt中提取142 ; platformToolSet&gt; v142&lt;/platformToolSet&gt;在文件dir(请更改该名称)中,

for /f "tokens=1,2delims=<> " %%b in ('type "%dir%"') do if "%%b"=="PlatformToolset" set "vernum=%%c"
set "vernum=%vernum:~1%"

请注意,位置和报价类型('''> “ )很关键。

<代码>读取文件的每一行并分析该行,将第一个“令牌”(1)分配给%% B和第二(2)至%% C

被认为是
&lt; deLimiter序列&gt;&lt;

A 定界符序列delims = option中指定的任何一个或多个字符;在这种情况下,&lt; &gt; space

so- 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. First dir 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 file dir (please change that name)

for /f "tokens=1,2delims=<> " %%b in ('type "%dir%"') do if "%%b"=="PlatformToolset" set "vernum=%%c"
set "vernum=%vernum:~1%"

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 Space

So - where %%a (token 1) is PlatformToolset, we want to assign %%c (token 2) to vernum

so vernum will become v142 with a file containing the test line you've posted.

And the hieroglyphics behind the set command is explained by set /? from the prompt - it takes the value in vernum, starting at "character 1" (where the character numbering starts at 0) and assigns the result to the variable vernum

for more detail about for gymnastics, see for /? from the prompt.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文