Adi Kish
Adi Kish

Reputation: 89

How to write a batch file to find a string in a text file and push it into a new text file

i want to write a batch file to find a particular string in a file. If the string is found then i want to redirect the whole line which contains the string to another file. for ex:

suppose a file myfile.txt contains the following text

abcwerthfdh
qwerewtretywr
weqreqwrtabcwerwe
wqerweqabcqwewq

when i launch the batch file giving myfile.txt and abc as command line arguments, then the output should be into a file called newfile.txt and it should contain only the lines which contain the text "abc". if i run this code again it should append to newfile.txt and not delete existing contents. in this case it should push the lines 1, 3 and 4 into newfile.txt

Upvotes: 0

Views: 5313

Answers (3)

npocmaka
npocmaka

Reputation: 57332

type  myfile.txt | findstr /n "abc" >>  newfile.txt 

???

Upvotes: 0

Endoro
Endoro

Reputation: 37589

@echo off&setlocal
for /f "delims=" %%a in ('findstr "%~2" "%~1"') do (echo(%%a)>>newfile.txt

Upvotes: 2

fthiella
fthiella

Reputation: 49089

You could use FINDSTR:

FINDSTR abc myfile.txt >> newfile.txt

Upvotes: 0

Related Questions