Jakers326
Jakers326

Reputation: 89

Add Text To A Certain Line In A Text File With Batch

Is it possible to have a command used in a batch file to append to a text file and add text on a certain Line in the txt file?

Txt File:
1
2
3
5

Is it possible to add a new line and put 4 between 3 and 5?

Upvotes: 1

Views: 5337

Answers (1)

Monacraft
Monacraft

Reputation: 6630

This answer will add a 4 between a 3 and a 5.

@echo off
setlocal enabledelayedexpansion
ren in.txt in.tmp
set p=
for /f %%a in (in.tmp) do (
  if "%%a"=="5" if "!p!"=="3" Echo 4 >> in.txt
  Echo %%a >>in.txt
  set p=%%a
)
del in.tmp

Upvotes: 2

Related Questions