Page 1 of 1

IF EXIST [string in file] THEN [command]

Posted: 05 Aug 2011 20:28
by alleypuppy
I'm making a batch file that will add a string to a file of it doesn't exist in that file. Is this possible?

Re: IF EXIST [string in file] THEN [command]

Posted: 05 Aug 2011 22:37
by Batcher

Code: Select all

findstr /c:"hello world" a.txt || echo hello world >>a.txt

Re: IF EXIST [string in file] THEN [command]

Posted: 06 Aug 2011 10:42
by dbenham
The above works, but you probably don't want to see the FINDSTR output if the string already exists. So we just redirect output to NUL:

Code: Select all

findstr /c:"hello world" a.txt >nul || echo hello world >>a.txt


Dave Benham