批处理文件来编辑ini文件行 [英] Batch file to edit line in ini file
问题描述
我有被自动生成ini文件。
I have an ini file that gets autogenerated.
它的第二行总是:版本= W.XX.Y.ZZ
其中,是W
是主版本号, XX
是次版本,ÿ
是生成和 ZZ
是修订
Where W
is the major version number, XX
is the minor version, Y
is the Build and ZZ
is the Revision.
我需要打开ini文件和使用批处理文件编辑行,以便在该版本的内部版本号和修订号移除。因此,该行最终应该是这样的:版本= W.XX
I need to open that ini file and edit that line using a batch file so that the build and revision numbers in that version get removed. Therefore, the line should end up like this:
Version = W.XX
主要数字将永远是一个字符,而辅号永远是两个,因此整个字符串为14个字符(含空格)长。
The major number will always be one character and the minor number will always be two, therefore the entire string is 14 characters (inc spaces) long.
我希望我能得到的是字符串左
该行14个字符,并替换该字符串该行。
I was hoping that I could get the string that is LEFT
14 characters of that line and replace that line with that string.
推荐答案
左的语法你要求的是使用一个变量子扩展:%VAR:〜14%
The "LEFT" syntax you're asking for is to use a variable substring expansion: %var:~,14%
以下code将包含字符串的每一行执行LEFT 14,版本
The following code will perform a "LEFT 14" on every line that contains the string "Version"
setlocal enabledelayedexpansion
del output.ini
for /f "tokens=*" %%a in (input.ini) do (
set var=%%a
if not {!var!}=={!var:Version=!} set var=!var:~,14!
echo.!var! >> output.ini
)
endlocal
如果有其他的线,版本的话,你也可以修改循环使用一个计数器。
If there are other lines with the word "Version", you can also modify the loop to use a counter.
setlocal enabledelayedexpansion
del output.ini
set counter=0
for /f "tokens=*" %%a in (input.ini) do (
set var=%%a
set /a counter=!counter!+1
if !counter! EQU 2 set var=!var:~,14!
echo.!var! >> output.ini
)
endlocal
请注意,在这两种情况下,你可能需要做更多的工作,如果你的文件包含特殊符号像|,&LT,或>
Note that in both cases, you might have to do more work if your file contains special symbols like |, <, or >
这篇关于批处理文件来编辑ini文件行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!