通过批处理文件替换.properties文件中的数字

时间:2013-11-17 19:03:08

标签: batch-file batch-processing

我有一个.properties文件说abc.properties文件。它有一个文本BuildNumber = 0。我想搜索BuildNumber并通过批处理文件将其值替换为当前的内部版本号。

请有人帮忙。我是批处理脚本的新手。

任何帮助都将不胜感激。

由于

2 个答案:

答案 0 :(得分:2)

你可以用sed

轻松完成
#!/bin/bash
BUILD="1.1"
sed "s/^BuildNumber=.*/BuildNumber=$BUILD/" abc.properties

这假设BuildNumber和=符号之间没有空格,否则你可以使用这个:

sed "s/^\(BuildNumber\s*=\s*\).*$/\1$BUILD/" abc.properties

答案 1 :(得分:0)

这是使用batch / vbs混合脚本的另一种方法。

@echo off
setlocal

call :FindReplace "Buildnumber=0" "Buildnumber=1.21" abc.properties
exit /b 

:FindReplace <findstr> <replstr> <file>
set tmp="%temp%\tmp.txt"
If not exist %temp%\_.vbs call :MakeReplace
for /f "tokens=*" %%a in ('dir "%3" /s /b /a-d /on') do (
  for /f "usebackq" %%b in (`Findstr /mic:"%~1" "%%a"`) do (
    echo(&Echo Replacing "%~1" with "%~2" in file %%~nxa
    <%%a cscript //nologo %temp%\_.vbs "%~1" "%~2">%tmp%
    if exist %tmp% move /Y %tmp% "%%~dpnxa">nul
  )
)
del %temp%\_.vbs
exit /b

:MakeReplace
>%temp%\_.vbs echo with Wscript
>>%temp%\_.vbs echo set args=.arguments
>>%temp%\_.vbs echo .StdOut.Write _
>>%temp%\_.vbs echo Replace(.StdIn.ReadAll,args(0),args(1),1,-1,1)
>>%temp%\_.vbs echo end with
相关问题