Computer Hope

Microsoft => Microsoft DOS => Topic started by: kamak on May 16, 2008, 05:28:06 AM

Title: Determining what is in a text file via batch
Post by: kamak on May 16, 2008, 05:28:06 AM
Hi all, how would i go about making a batch file that reads a text file, and if the text file contains a certain word, execute a certain action?

Eg. i want my batch file to check if text file EXAMPLE.txt has EXAMPLEWORD in it, and if it does i want the batch file to continue with the code, or not do anything if EXAMPLEWORD is not found.

Thanks in advance,

Kamak
Title: Re: Determining what is in a text file via batch
Post by: Sidewinder on May 16, 2008, 08:32:53 AM
Code: [Select]
for /f "tokens=* delims=" %%v in ('type example.txt ^| find "EXAMPLEWORD"') do echo %%v

This example executes the echo instruction if the argument is found. You can change this to whatever instruction you want executed. The search for EXAMPLEWORD is case sensitive. Add the /i switch to the find command to make it case insensitive.

Happy coding,  8)
Title: Re: Determining what is in a text file via batch
Post by: ghostdog74 on May 16, 2008, 09:14:21 AM
I am sure findstr takes in a file as input
Code: [Select]
findstr /I /G:file  "STRING to SEARCH" && execute_command
Title: Re: Determining what is in a text file via batch
Post by: Dias de verano on May 16, 2008, 09:35:26 AM
Findstr takes a file or receives an input via a pipe as Sidewinder's example shows.
Title: Re: Determining what is in a text file via batch
Post by: ghostdog74 on May 16, 2008, 11:06:37 AM
Findstr takes a file or receives an input via a pipe as Sidewinder's example shows.

yup, so using type would be unnecessary , not that its a big deal anyway :)
Title: Re: Determining what is in a text file via batch
Post by: Dias de verano on May 16, 2008, 11:24:37 AM
I am sure findstr takes in a file as input
Code: [Select]
findstr /I /G:file  "STRING to SEARCH" && execute_command

findstr /I "String to search" filename.txt>nul && execute_command
Title: Re: Determining what is in a text file via batch
Post by: kamak on May 16, 2008, 07:51:53 PM
Thanks all, you've been really helpful :)