如何在cmd脚本中的字符串中找到版本号?

时间:2021-08-11 02:08:19

For my software development project, I am using .cmd files to automatically create binaries with the version number in the file name. The version number is stored in a C header file.

对于我的软件开发项目,我使用.cmd文件自动创建文件名中包含版本号的二进制文件。版本号存储在C头文件中。

I have a file called "app_version.h" which contains something like

我有一个名为“app_version.h”的文件,其中包含类似的内容

Generic include file code or commenting
#define APP_SW_VER ("Vx.y.zww")
More generic include file code or commenting

Where Vx.y.zww is the format for the version number. Valid examples are:

其中Vx.y.zww是版本号的格式。有效的例子是:

  • V1.0.1
  • V1.0.1DBG
  • V1.0.1PT11
  • V81.2.1RC2

As for the file name, I want to replace the dots with underscores and append it to a predefined string and then use that as a file name.

至于文件名,我想用下划线替换点,并将其附加到预定义的字符串,然后将其用作文件名。

Prime problem: To extract the Vx.y.zww part and use it as a variable in a cmd file.

Prime问题:提取Vx.y.zww部分并将其用作cmd文件中的变量。

Example of the whole procedure

整个程序的例子

Inputs:

"MYPROGRAM" as predefined string

“MYPROGRAM”作为预定义的字符串

Binary filed called "output.bin"

二进制文件称为“output.bin”

File app_version.h contains the line #define APP_SW_VER ("V1.1.3")

文件app_version.h包含#define APP_SW_VER(“V1.1.3”)行

Desired output:

File with name "MYPROGRAM_V1_1_3.bin"

文件名称“MYPROGRAM_V1_1_3.bin”

1 个解决方案

#1


1  

quite straight forward (theoretical - see note below): get the correct line and get the part between the quotes. Replace . with _ and add your predefined string and the extension:

非常直接(理论 - 请参阅下面的注释):获取正确的行并获得引号之间的部分。替换。使用_并添加预定义的字符串和扩展名:

for /f tokens^=2delims^=^" %%a in ('type app_version.h^|findstr /bc:"#define APP_SW_VER"') do set "ver=%%a"
echo MYPROGRAM_%ver:.=_%.bin

Note: the usual syntax for /f "tokens=2 delims=x" can't be used for a delimiter ". Therefore we have to use this ugly workaround (without surrounding quotes, but escaping some special chars instead).

注意:/ f“tokens = 2 delims = x”的通常语法不能用于分隔符“。因此我们必须使用这个丑陋的解决方法(不包含引号,而是转义一些特殊的字符)。

Another possibility (thanks, Squashman) is to use () as delimiters. The quotes can easily removed by using %%~a (can keep the familiar syntax):

另一种可能性(谢谢,Squashman)是使用()作为分隔符。使用%% ~a可以轻松删除引号(可以保持熟悉的语法):

for /f "tokens=2 delims=()" %%a in ('type app_version.h^|findstr /bc:"#define APP_SW_VER"') do set "ver=%%~a"
echo MYPROGRAM_%ver:.=_%.bin

#1


1  

quite straight forward (theoretical - see note below): get the correct line and get the part between the quotes. Replace . with _ and add your predefined string and the extension:

非常直接(理论 - 请参阅下面的注释):获取正确的行并获得引号之间的部分。替换。使用_并添加预定义的字符串和扩展名:

for /f tokens^=2delims^=^" %%a in ('type app_version.h^|findstr /bc:"#define APP_SW_VER"') do set "ver=%%a"
echo MYPROGRAM_%ver:.=_%.bin

Note: the usual syntax for /f "tokens=2 delims=x" can't be used for a delimiter ". Therefore we have to use this ugly workaround (without surrounding quotes, but escaping some special chars instead).

注意:/ f“tokens = 2 delims = x”的通常语法不能用于分隔符“。因此我们必须使用这个丑陋的解决方法(不包含引号,而是转义一些特殊的字符)。

Another possibility (thanks, Squashman) is to use () as delimiters. The quotes can easily removed by using %%~a (can keep the familiar syntax):

另一种可能性(谢谢,Squashman)是使用()作为分隔符。使用%% ~a可以轻松删除引号(可以保持熟悉的语法):

for /f "tokens=2 delims=()" %%a in ('type app_version.h^|findstr /bc:"#define APP_SW_VER"') do set "ver=%%~a"
echo MYPROGRAM_%ver:.=_%.bin