我有一个包含以下文本的文本文件。
“模块”
{ "ModuleSignature" = "8:MergeModule.6F1248514B3047E99E4EE8A129CB8605" "Version" = "8:1.0.0.0" "Title" = "8:uoipmsm" "Subject" = "8:" "Author" = "8:Microsoft" "Keywords" = "8:" "Comments" = "8:" "SearchPath" = "8:" "UseSystemSearchPath" = "11:TRUE" "TargetPlatform" = "3:1" "PreBuildEvent" = "8:" "PostBuildEvent" = "8:" "RunPostBuildEvent" = "3:0" }
在上面;我想更改从工具触发构建时将给出的版本号。
我想在批处理文件中传递一个参数 $ Version ,它必须从我使用的工具中获取版本号,并在该文本文件中更新它。
对于例如:在上面的文本中我想将其编码为“Version”=“8:$ Version”因此当我在触发构建时提供版本号时,它必须在此文本文件中更新相同的版本号。
您能否指导我如何编辑特定的行。我是Windows批处理脚本的新手。
并且我要再添加一个点...在文本文件中我必须修改行号399中的版本。因此批处理文件必须跳转到该文本文件中的行号399并修改它。请帮我修理一下......
我已将上述脚本保存在文本栏中并保存为ver.bat;并且在同一文件夹中我保存了Intext文件。当我提到要替换的行号时,它将从第1行到399删除“=”符号后面的内容。
在运行批处理文件之前:
"ModuleSignature" = "8:MergeModule.6F1248514B3047E99E4EE8A129CB8605"
"Version" = "8:1.0.0.0"
"Title" = "8:uoipmsm"
"Subject" = "8:"
"Author" = "8:Microsoft"
"Keywords" = "8:"
"Comments" = "8:"
"SearchPath" = "8:"
"UseSystemSearchPath" = "11:TRUE"
"TargetPlatform" = "3:1"
"PreBuildEvent" = "8:"
"PostBuildEvent" = "8:"
"RunPostBuildEvent" = "3:0"
如果我运行批处理文件,我提到了行号为10和after after tat;我得到以下输出;
"ModuleSignature" =
"Version" = ""
"Title" =
"Subject" =
"Author" =
"Keywords" =
"Comments" =
"SearchPath" =
"UseSystemSearchPath" =
"TargetPlatform" =
"PreBuildEvent" =
"PostBuildEvent" =
"RunPostBuildEvent" =
任何想法??
答案 0 :(得分:1)
这应该这样做。当然,如果输入文件发生变化,这将不得不改变。如果输入文件中有空行(他们不计算),您可能还需要进行调整。请注意,您只需要传递版本(而不是整个新行399)。在你的例子中," 8:1.0.0.0"。最好引用字符串,以防有空格。 作为解释,这将:
x
@echo off
REM %1=Version (use quotes if there are spaces in version)
set ReplaceLine=399
set InFile=Test.txt
set TempFile=TempTest.txt
if exist "%TempFile%" del "%TempFile%"
if "%~1"=="" (
color CF
echo.This program must be called with an argument!
pause
goto :eof
)
setlocal enabledelayedexpansion
set /A Cnt=1
for /F "tokens" %%a in (%InFile%) do (
echo.%%a>> "%TempFile%"
set /A Cnt+=1
if !Cnt! GEQ %ReplaceLine% GOTO :ExitLoop
)
:ExitLoop
endlocal
echo."Version" = "%~1">> "%TempFile%"
more +%ReplaceLine% < "%InFile%">> "%TempFile%"
copy /y "%TempFile%" "%InFile%"
del "%TempFile%"
goto :eof