0

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

0

3 Answers 3

2
@echo off&setlocal
for /f "delims=" %%a in ('findstr "%~2" "%~1"') do (echo(%%a)>>newfile.txt
Sign up to request clarification or add additional context in comments.

Comments

0

You could use FINDSTR:

FINDSTR abc myfile.txt >> newfile.txt

Comments

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

???

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.