1

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

2 Answers2

2
@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
Aacini
  • 65,180
  • 12
  • 72
  • 108
  • it added the new line, but did not copy the rest of the lines. – user3158712 Dec 11 '15 at 17:17
  • @Squashman: See [this post](http://stackoverflow.com/questions/8844868/what-are-the-undocumented-features-and-limitations-of-the-windows-findstr-comman/28278628#28278628) – Aacini Dec 11 '15 at 17:21
  • How do I get it to copy over the remaining lines from in.tmp to test.txt? – user3158712 Dec 11 '15 at 17:24
  • Plase, check your data. This method work as intended... **EDIT**: I just tested it! – Aacini Dec 11 '15 at 17:28
  • The cmd screen stays up too. Only way to close it is to hit the x. thoughts? – user3158712 Dec 11 '15 at 17:29
  • something with the findstr"^" I believe it should have a location after it such as findstr"^" test.txt or something. That does not work exactly, but the cmd window now closes. I believe the original code did not finish executing. – user3158712 Dec 11 '15 at 17:36
  • What happened with this solution? The least thing you should do after received free aid is to inform the final result, particularly if you reported an error in the code _four times_! `**:(**` – Aacini Dec 14 '15 at 12:26
0

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
Squashman
  • 13,649
  • 5
  • 27
  • 36
  • It deletes my original text.txt file. Not sure if it worked before deleting or not. – user3158712 Dec 11 '15 at 17:06
  • @user3158712, you do not have a file named text.txt in your code, nor is it in my code. But if you are referring to test.txt then of course. You are renaming test.txt to in.tmp. – Squashman Dec 11 '15 at 17:08
  • Sorry for the confusion. I do mean test.txt ;however, I do not want to rename test.txt to in.tmp, rather, just insert 'test4' into line 2 in-between lines 1 () and line 3 ( – user3158712 Dec 11 '15 at 17:13
  • @user3158712, you have to create a temp file. Batch cannot in place edit a file. – Squashman Dec 11 '15 at 17:14