0

I am writing a batch script to update a properties file. I am able to update the properties file and redirect the output to another file. But i want to update the same properties file and maintain all the comments and blank line as it is.

example: project properties

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

Please tell me how to update a properties file directly without redirecting its processed output to another file.

Thank you

Shree
  • 21
  • 3
  • @Shree, there is a way without creating a new file, but it is more complicated than just writing to new file then do `type NewData.txt > OldData.txt` to replicate the old new data to the old file. – Gerhard Jan 10 '19 at 12:02
  • @Shree.Bottom of the script, line before pause, add this line `timeout 3 >nul && type "C:\Users\...\installer\NewData.txt" > C:\Users\Varun\...\installer\none.properties"` – Gerhard Jan 10 '19 at 12:12

1 Answers1

0

You cannot do this immediately (with a simple way). The file cannot be accessed, as it will be used by another process. What you can do, is to create a temporary file and then type it to the original none.properties file.

)>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
double-beep
  • 5,031
  • 17
  • 33
  • 41
  • Thanks for the answer but can you please tell me how to retain blank lines, so that the file redirected file looks exactly like source file with updated property values. – Shree Jan 11 '19 at 04:19