Search code examples
batch-fileinsertdoslines

Insert lines at the end of text file if not exist using DOS


I need batch file.bat to do

I have filename.txt with content:

blah blah
....
Miss Phuong
I love you all. Miss Phuong
End of file

I want to insert following lines at the end if the line is not exist:

I love you all
Miss Phuong

After run it is:

blah blah
....
Miss Phuong
I love you all. Miss Phuong
End of file
I love you all

"Miss Phuong" will be not added because line exist. "I love you all" will be added because string "I love you all" exist but that line is "I love you all. Miss Phuong"

Thanks


Solution

  • I think you need something like this:

    @echo off
    
    findstr /x /c:"I love you all" text.txt >nul || echo I love you all>> text.txt
    findstr /x /c:"Miss Phuong" text.txt >nul || echo Miss Phuong>> text.txt