Read for content from one file and update in another using batch script

I Need help to update the values in file2.txt taking those values from file1.txt using batch script

file1:

sampleId=sampleid123

SerialNumber=serialno123

file2:

websocketURL=https://websocketURL.com/

graphNumber=1

sampleId=sampleid122

#Please enter dateAndTime in dd-MM-yyyy hh:mm format only (Eg:30-08-2021 04:20) dateAndTime=09-09-2021 05:30

SerialNumber=serialno122

[email protected]

user.password=password

api.basepath=https://basepathurl/

user.switched.tenant=netzaskjfas

Solution 1:

You could create the following batch file:

@echo off

set File1=C:\Users\ricar\Desktop\Test\file1.txt
set File2=C:\Users\ricar\Desktop\Test\file2.txt
set TempFile=%userprofile%\Desktop\Test\file2.tmp

:: Pharsing file1:
for /f "tokens=1* eol= delims==" %%a in ('type %File1%') do (
                                                             if /i "%%a"=="sampleId" set "SampleId=%%b"
                                                             if /i "%%a"=="SerialNumber" set "SerialNumber=%%b"
                                                            )
:: Creating a Temporary file:
>"%TempFile%" echo websocketURL=https://websocketURL.com/
>>"%TempFile%" echo.
>>"%TempFile%" echo graphNumber=1
>>"%TempFile%" echo.
>>"%TempFile%" echo sampleId=%SampleID%
>>"%TempFile%" echo.
>>"%TempFile%" echo #Please enter dateAndTime in dd-MM-yyyy hh:mm format only (Eg:30-08-2021 04:20) dateAndTime=09-09-2021 05:30
>>"%TempFile%" echo.
>>"%TempFile%" echo SerialNumber=%SerialNumber%
>>"%TempFile%" echo.
>>"%TempFile%" echo [email protected]
>>"%TempFile%" echo.
>>"%TempFile%" echo user.password=password
>>"%TempFile%" echo.
>>"%TempFile%" echo api.basepath=https://basepathurl/
>>"%TempFile%" echo.
>>"%TempFile%" echo user.switched.tenant=netzaskjfas

:: deleting file2:
if /i exist "%File2%" del /q "%File2%"

:: Renaming temporary file to file2
ren "%TempFile%" "file2.txt"

Here are some tests I made: UpdateFile.gif