我需要帮助来更新 file2.txt 中的值,使用批处理脚本从 file1.txt 中获取这些值
文件1:
sampleId=sampleid123
SerialNumber=serialno123
文件2:
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
答案1
您可以创建以下批处理文件:
@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"
以下是我所做的一些测试: