I wrote a batch file that I want to overwrite lines with lines from another .txt file.
it currently completely copies the new File.txt file, but does not replace the lines with the lines from the OldFile.txt file.
Example lines in File.txt:
...
# Password
Pword =
# AccountName
Account =
# TownName
City =
# Postcode
Index =
# LocationChangedDate
LocationChanged =
An example of the lines in the OldFile.txt file I want to replace from:
...
# Password
Pword = abc
# AccountName
Score = 123
# TownName
City = LDN
# Postcode
Postal Code = WS77TP
# LocationChangedDate
LocationChanged = 01/01/2015
Can someone point me in the right direction or explain where I made a mistake?
@echo off setlocal disableDelayedExpansion ::Variables set InputFile=F:\EXCHANGE\3\Machine\File.txt set OutputFile=F:\EXCHANGE\3\File-New.txt set CopyFile=F:\EXCHANGE\3\OldMachine\OldFile.txt set _strFindPword=Pword=.* for /F "delims=" %%A in ('findstr /x "Pword=.*" %CopyFile%') do set _strInsertPword=%%A echo.%_strInsertPword% set _strFindAccount=Account=.* for /F "delims=" %%B in ('findstr /x "Account=.*" %CopyFile%') do set _strInsertAccount=%%B echo.%_strInsertAccount% set _strFindTown=Town=.* for /F "delims=" %%C in ('findstr /x "Town=.*" %CopyFile%') do set _strInsertTown=%%C echo.%_strInsertTown% set _strFindLocationChanged=LocationChanged=.* for /F "delims=" %%D in ('findstr /x "LocationChanged=.*" %CopyFile%') do set _strInsertLocationChanged=%%D echo.%_strInsertLocationChanged% set _strFindPostcode=Postcode=.* for /F "delims=" %%E in ('findstr /x "Postcode=.*" %CopyFile%') do set _strInsertPostcode=%%E echo.%_strInsertPostcode% ( for /F "delims=" %%L in ('findstr /n "^" "%InputFile%"') do ( set "line=%%L" setlocal EnableDelayedExpansion set "line=!line:*:=!" if "%%L" equ "_strFindPword" (echo.!_strInsertPword!) else ( if "%%L" equ "%_strFindAccount%" (echo.!_strInsertAccount!) else ( if "%%L" equ "%_strFindTown%" (echo.!_strInsertTown!) else ( if "%%L" equ "%_strFindLocationChanged%" (echo.!_strInsertLocationChanged!) else ( if "%%L" equ "%_strFindPostcode%" (echo.!_strInsertPostcode!) else (echo.!line!) ) ) ) ) endlocal ) ) > "%OutputFile%" del %InputFile% ren %OutputFile% File.txt pause
source share