IF EXIST [string in file] THEN [command]

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
alleypuppy
Posts: 82
Joined: 24 Apr 2011 19:20

IF EXIST [string in file] THEN [command]

#1 Post by alleypuppy » 05 Aug 2011 20:28

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?

Batcher
Posts: 74
Joined: 16 Apr 2009 10:36

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

#2 Post by Batcher » 05 Aug 2011 22:37

Code: Select all

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

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

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

#3 Post by dbenham » 06 Aug 2011 10:42

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

Post Reply