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

Author Topic: How to see if a string exists in a txt file.  (Read 33139 times)

0 Members and 1 Guest are viewing this topic.

BatchFileCommand

    Topic Starter


    Hopeful
  • Thanked: 1
    How to see if a string exists in a txt file.
    « on: January 18, 2009, 07:34:20 PM »
    How would you make the batch file search for a string in a batch file, then if the string exists , it does the specified command.
    οτη άβγαλτος μεταφ βαθμολογία

    BC_Programmer


      Mastermind
    • Typing is no substitute for thinking.
    • Thanked: 1140
      • Yes
      • Yes
      • BC-Programming.com
    • Certifications: List
    • Computer: Specs
    • Experience: Beginner
    • OS: Windows 11
    Re: How to see if a string exists in a txt file.
    « Reply #1 on: January 18, 2009, 07:40:05 PM »
    use find command and errorlevel.
    I was trying to dereference Null Pointers before it was cool.

    BatchFileCommand

      Topic Starter


      Hopeful
    • Thanked: 1
      Re: How to see if a string exists in a txt file.
      « Reply #2 on: January 18, 2009, 07:44:41 PM »
      So would this work.
      Code: [Select]
      find /i "string" filename
      if errorlevel 0 do echo string unfound
      οτη άβγαλτος μεταφ βαθμολογία

      Dias de verano

      • Guest
      Re: How to see if a string exists in a txt file.
      « Reply #3 on: January 19, 2009, 04:01:26 AM »
      So would this work.
      Code: [Select]
      find /i "string" filename
      if errorlevel 0 do echo string unfound

      Suggestion: try it and see.

      BC_Programmer


        Mastermind
      • Typing is no substitute for thinking.
      • Thanked: 1140
        • Yes
        • Yes
        • BC-Programming.com
      • Certifications: List
      • Computer: Specs
      • Experience: Beginner
      • OS: Windows 11
      Re: How to see if a string exists in a txt file.
      « Reply #4 on: January 19, 2009, 04:07:40 AM »
      So would this work.
      Code: [Select]
      find /i "string" filename
      if errorlevel 0 do echo string unfound

      Suggestion: try it and see.


      OMG! I was gonna say that!
      I was trying to dereference Null Pointers before it was cool.

      BatchFileCommand

        Topic Starter


        Hopeful
      • Thanked: 1
        Re: How to see if a string exists in a txt file.
        « Reply #5 on: January 19, 2009, 08:43:07 AM »
        Quote
        Suggestion: try it and see.

        I was looking for a more direct answer  ::). But no, it doesn't work.
        οτη άβγαλτος μεταφ βαθμολογία

        Dias de verano

        • Guest
        Re: How to see if a string exists in a txt file.
        « Reply #6 on: January 19, 2009, 10:50:25 AM »
        Quote
        Suggestion: try it and see.

        I was looking for a more direct answer

        Ok, "no".

        Quote
        it doesn't work.

        I can see why.

        So can you if you read the error message.

        You can roll your eyes all you like. Attitude like that will get you a whole bunch of help, wise guy.




        BatchFileCommand

          Topic Starter


          Hopeful
        • Thanked: 1
          Re: How to see if a string exists in a txt file.
          « Reply #7 on: January 19, 2009, 11:50:13 AM »
          Unfortunately I turn echo on and there is no error message. I just took a random guess with the errorlevel.
          Besides, Dias, why are you always this "I know everything" guy who has no "patience" .
          οτη άβγαλτος μεταφ βαθμολογία

          Dias de verano

          • Guest
          Re: How to see if a string exists in a txt file.
          « Reply #8 on: January 19, 2009, 12:35:25 PM »
          Unfortunately I turn echo on and there is no error message. I just took a random guess with the errorlevel.
          Besides, Dias, why are you always this "I know everything" guy who has no "patience" .

          I am not. I just respond badly to lazy people.

          computers.txt contains the text 'bb'

          Code: [Select]
          @echo off
          find /i "bb" computers.txt
          if errorlevel 0 do echo string unfound

          I saved it as tme.bat

          Code: [Select]
          C:\>tme

          ---------- COMPUTERS.TXT
          bb
          'do' is not recognized as an internal or external command,
          operable program or batch file.



          BatchFileCommand

            Topic Starter


            Hopeful
          • Thanked: 1
            Re: How to see if a string exists in a txt file.
            « Reply #9 on: January 19, 2009, 12:46:18 PM »
            Oh yeah, I took away the do shortly after the first post. Should've edited and took that out. So, any other reason it shouldn't be working?
            οτη άβγαλτος μεταφ βαθμολογία

            Dias de verano

            • Guest
            Re: How to see if a string exists in a txt file.
            « Reply #10 on: January 19, 2009, 12:51:37 PM »
            Oh yeah, I took away the do shortly after the first post. Should've edited and took that out. So, any other reason it shouldn't be working?

            It depends what you mean by "working".

            You have the logic the wrong way around. If errorlevel is 0 that means there was no error, that is, the string was found, not "unfound", (did you mean not found?)

            BatchFileCommand

              Topic Starter


              Hopeful
            • Thanked: 1
              Re: How to see if a string exists in a txt file.
              « Reply #11 on: January 19, 2009, 01:22:18 PM »
              Oh, I thought if errorlevel was 0 there was an error  ::).
              So I need to add a bit more. Would it matter if I have the .txt or not?

              Code: [Select]
              find /i "text" file
              if not errorlevel 0 (echo found) else (echo unfound)
              pause>nul

              It always says found. I've tried this with the extension on and off the file name.

              « Last Edit: January 19, 2009, 01:36:37 PM by BatchFileCommand »
              οτη άβγαλτος μεταφ βαθμολογία

              Dias de verano

              • Guest
              Re: How to see if a string exists in a txt file.
              « Reply #12 on: January 19, 2009, 01:54:28 PM »
              You must always use the full file name including extension.

              This works

              Code: [Select]
              find /i "text" filename.ext
              if %errorlevel% EQU 0 (echo found) else (echo not found)

              BatchFileCommand

                Topic Starter


                Hopeful
              • Thanked: 1
                Re: How to see if a string exists in a txt file.
                « Reply #13 on: January 19, 2009, 02:23:24 PM »
                Finally! It works  ;D.
                « Last Edit: January 19, 2009, 07:03:36 PM by BatchFileCommand »
                οτη άβγαλτος μεταφ βαθμολογία

                BC_Programmer


                  Mastermind
                • Typing is no substitute for thinking.
                • Thanked: 1140
                  • Yes
                  • Yes
                  • BC-Programming.com
                • Certifications: List
                • Computer: Specs
                • Experience: Beginner
                • OS: Windows 11
                Re: How to see if a string exists in a txt file.
                « Reply #14 on: January 19, 2009, 07:12:12 PM »
                awwwww. Darn. I was reading the thread and hoping BFC got contrexed. But he isn't worthy of a Contrexing. As entertaining as they are, they likely take some effort.

                Now he's gonna ask- "whats contrexing?"
                I was trying to dereference Null Pointers before it was cool.