Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Determining what is in a text file via batch  (Read 2413 times)

0 Members and 1 Guest are viewing this topic.

kamak

    Topic Starter


    Rookie

    Determining what is in a text file via batch
    « 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

    Sidewinder



      Guru

      Thanked: 139
    • Experience: Familiar
    • OS: Windows 10
    Re: Determining what is in a text file via batch
    « Reply #1 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)
    The true sign of intelligence is not knowledge but imagination.

    -- Albert Einstein

    ghostdog74



      Specialist

      Thanked: 27
      Re: Determining what is in a text file via batch
      « Reply #2 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

      Dias de verano

      • Guest
      Re: Determining what is in a text file via batch
      « Reply #3 on: May 16, 2008, 09:35:26 AM »
      Findstr takes a file or receives an input via a pipe as Sidewinder's example shows.

      ghostdog74



        Specialist

        Thanked: 27
        Re: Determining what is in a text file via batch
        « Reply #4 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 :)

        Dias de verano

        • Guest
        Re: Determining what is in a text file via batch
        « Reply #5 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

        kamak

          Topic Starter


          Rookie

          Re: Determining what is in a text file via batch
          « Reply #6 on: May 16, 2008, 07:51:53 PM »
          Thanks all, you've been really helpful :)