Reputation: 11
I am new to batch files and having trouble piecing together the last steps here. My goal is purely to insert 'Test4' inbetween 2 lines of text. The text never changes and is always line 1 and line 3 (line 2 is blank within the text file). The code removes line 2 (blank) but does not insert the text.
Current txt file:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE ProductDataeXchangePackage [
1
2
3
Current Batch script:
rem Saved in D:\Temp\WriteText.bat
echo off
setlocal enabledelayedexpansion
ren test.txt in.tmp
set p=
for /f "delims=" %%a in (in.tmp) do (
if "%%a"=="<!DOCTYPE_ProductDataeXchangePackage+[" if "!p!"=="<?xml version="1.0" encoding="UTF-8" ?>" Echo Test4 >> test.txt
Echo %%a >>test.txt
set p=%%a
)
del in.tmp
Upvotes: 1
Views: 206
Reputation: 67196
@echo off
setlocal EnableDelayedExpansion
ren test.txt in.tmp
< in.tmp (
set /P "line1="
echo !line1!
set /P "line2="
echo Test4
rem Copy the rest of lines
findstr "^"
) > test.txt
REM del in.tmp
EDIT: Output example added
C:\> type test.txt
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE ProductDataeXchangePackage [
1
2
3
C:\> test.bat
C:\> type test.txt
<?xml version="1.0" encoding="UTF-8" ?>
Test4
<!DOCTYPE ProductDataeXchangePackage [
1
2
3
Upvotes: 2
Reputation: 14290
One way you could do it.
rem Saved in D:\Temp\WriteText.bat
@echo off
setlocal enabledelayedexpansion
ren test.txt in.tmp
set /p line1=<in.tmp
>test.txt echo %line1%
>>test.txt Echo Test4
for /f "skip=1 delims=" %%a in (in.tmp) do >>test.txt Echo %%a
del in.tmp
Upvotes: 0