使用批处理脚本更新属性

时间:2019-01-10 11:24:49

标签: batch-file

我正在编写一个批处理脚本来更新属性文件。我能够更新属性文件并将输出重定向到另一个文件。但是我想更新相同的属性文件,并按原样保留所有注释和空白行。

示例:项目属性

Set "portNum=8080"
Set "hostname=localhost"

Set "FindportNum=^<port_number^>"
Set "Findhost=^<host^>"


Set "username=someusername"
Set "password=somepassword"
Set "applicationAdminstratorUsername=someapplicationAdminstratorUsername"
Set "applicationAdminstratorPassword=someapplicationAdminstratorPassword"


Set "textFile=C:\Users\Varun\...\installer\none.properties"
Set "textFileOut=C:\Users\...\installer\NewData.txt"
@Echo off&SetLocal
( for /f "usebackq tokens=1* delims==" %%i in (
"%textFile%"
) do If "%%i" equ "Username" (
echo Username=%username%
) else If "%%i" equ "Password" (
echo Password=%password%
) else If "%%i" equ "consumer_key" (
echo consumer_key=%consumerKey%
) else If "%%i" equ "consumer_secret" (
echo consumer_secret=%consumerSecret%
) else If "%%i" equ "c360rest.username" (
echo c360rest.username=%applicationAdminstratorUsername%
) else If "%%i" equ "c360rest.password" (
echo c360rest.password=%applicationAdminstratorPassword%
) else if "%%j" neq "" (
      echo %%i=%%j
   ) else (
      echo %%i
   )
) >C:\Users\...\installer\NewData.txt
pause

请告诉我如何直接更新属性文件,而无需将其处理后的输出重定向到另一个文件。

谢谢

1 个答案:

答案 0 :(得分:0)

您不能立即执行此操作(使用简单方法)。无法访问该文件,因为它将被另一个进程使用。您可以做的是创建一个临时文件,然后type到原始none.properties文件。

)>C:\Users\...\installer\NewData.txt
rem OPTIONAL; added for safety: timeout to be sure the file is closed:
timeout 2 >nul
(type "C:\Users\...\installer\NewData.txt")>"C:\Users\Varun\...\installer\none.properties"
del "C:\Users\...\installer\NewData.txt"
pause