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 33274 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.

                BatchFileCommand

                  Topic Starter


                  Hopeful
                • Thanked: 1
                  Re: How to see if a string exists in a txt file.
                  « Reply #15 on: January 19, 2009, 07:23:08 PM »
                  The reason I may of seemed a bit "feeble-minded" is because I was quite unfamiliar with errorlevel. Contrexing is a word that doesn't exist   :P. Can't fool me there. I checked Wikipedia. 4 different dictionaries. I googled it. It doesn't exist. So what's the meaning of the meaningless word contrexing.
                  οτη άβγαλτος μεταφ βαθμολογία

                  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 #16 on: January 19, 2009, 07:29:48 PM »
                  har har har. it's a secret word. Look in the forum archives.


                  it's standard procedure for a error flag to me a number, and 0 means success. each different number means a different error. That way, one can easily check success by determining if the error code is 0.

                  I was trying to dereference Null Pointers before it was cool.

                  Helpmeh



                    Guru

                  • Roar.
                  • Thanked: 123
                    • Yes
                    • Yes
                  • Computer: Specs
                  • Experience: Familiar
                  • OS: Windows 8
                  Re: How to see if a string exists in a txt file.
                  « Reply #17 on: January 20, 2009, 04:27:34 PM »
                  I wasn't around when Contrex was, but I think I know how it works...

                  Contrexing (proper-verb=must have capital) is the act of flaming another user, in the way that Contrex would have.

                  (Is that right?)
                  Where's MagicSpeed?
                  Quote from: 'matt'
                  He's playing a game called IRL. Great graphics, *censored* gameplay.

                  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 #18 on: January 20, 2009, 05:12:08 PM »
                  I wasn't around when Contrex was, but I think I know how it works...

                  Contrexing (proper-verb=must have capital) is the act of flaming another user, in the way that Contrex would have.

                  (Is that right?)

                  yep, very good.

                  Contrex is among us.
                  I was trying to dereference Null Pointers before it was cool.

                  Helpmeh



                    Guru

                  • Roar.
                  • Thanked: 123
                    • Yes
                    • Yes
                  • Computer: Specs
                  • Experience: Familiar
                  • OS: Windows 8
                  Re: How to see if a string exists in a txt file.
                  « Reply #19 on: January 20, 2009, 06:40:01 PM »
                  I wasn't around when Contrex was, but I think I know how it works...

                  Contrexing (proper-verb=must have capital) is the act of flaming another user, in the way that Contrex would have.

                  (Is that right?)

                  yep, very good.

                  Contrex is among us.

                  Really? Then I have never met him(/her). The way you and Ivy and other members were talking about him (or her) made it sound like (s)he left a while ago.
                  Where's MagicSpeed?
                  Quote from: 'matt'
                  He's playing a game called IRL. Great graphics, *censored* gameplay.

                  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 #20 on: January 20, 2009, 07:14:25 PM »
                  I wasn't around when Contrex was, but I think I know how it works...

                  Contrexing (proper-verb=must have capital) is the act of flaming another user, in the way that Contrex would have.

                  (Is that right?)

                  yep, very good.

                  Contrex is among us.

                  Really? Then I have never met him(/her). The way you and Ivy and other members were talking about him (or her) made it sound like (s)he left a while ago.

                  lol. He's closer then you realize. It's not me though, before you suspect it.
                  I was trying to dereference Null Pointers before it was cool.

                  Dias de verano

                  • Guest
                  Re: How to see if a string exists in a txt file.
                  « Reply #21 on: January 21, 2009, 12:15:56 AM »
                  Flaming is just gratuitous rudeness; contrexing is usually deserved.

                  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 #22 on: January 21, 2009, 12:55:29 AM »
                  Flaming is just gratuitous rudeness; contrexing is usually deserved.

                  agreed.

                  I knew what it meant- couldn't immaculate it.
                  I was trying to dereference Null Pointers before it was cool.

                  Helpmeh



                    Guru

                  • Roar.
                  • Thanked: 123
                    • Yes
                    • Yes
                  • Computer: Specs
                  • Experience: Familiar
                  • OS: Windows 8
                  Re: How to see if a string exists in a txt file.
                  « Reply #23 on: January 21, 2009, 02:22:20 PM »
                  Flaming is just gratuitous rudeness; contrexing is usually deserved.

                  OHHH...now I understand.
                  Where's MagicSpeed?
                  Quote from: 'matt'
                  He's playing a game called IRL. Great graphics, *censored* gameplay.

                  BatchFileCommand

                    Topic Starter


                    Hopeful
                  • Thanked: 1
                    Re: How to see if a string exists in a txt file.
                    « Reply #24 on: January 21, 2009, 05:37:29 PM »
                    So, Contrexing is flaming another person because you think you can judge if they deserve it.
                    οτη άβγαλτος μεταφ βαθμολογία

                    Helpmeh



                      Guru

                    • Roar.
                    • Thanked: 123
                      • Yes
                      • Yes
                    • Computer: Specs
                    • Experience: Familiar
                    • OS: Windows 8
                    Re: How to see if a string exists in a txt file.
                    « Reply #25 on: January 21, 2009, 06:18:27 PM »
                    So, Contrexing is flaming another person because you think you can judge if they deserve it.
                    From what I understand, Contrexing is flaming another person BECAUSE they DO deserve it, not flaming someone because you hate their idea.
                    Where's MagicSpeed?
                    Quote from: 'matt'
                    He's playing a game called IRL. Great graphics, *censored* gameplay.

                    BatchFileCommand

                      Topic Starter


                      Hopeful
                    • Thanked: 1
                      Re: How to see if a string exists in a txt file.
                      « Reply #26 on: January 22, 2009, 06:18:21 AM »
                      Idea has nothing to do with it  :P.
                      οτη άβγαλτος μεταφ βαθμολογία

                      Dias de verano

                      • Guest
                      Re: How to see if a string exists in a txt file.
                      « Reply #27 on: January 22, 2009, 10:10:45 AM »
                      Roughly speaking, a contrexing is a rebuke for attitude or bumptiousness or rudeness or severe lack of respect or being a troll.

                      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 #28 on: January 22, 2009, 10:25:33 AM »
                      Roughly speaking, a contrexing is a rebuke for attitude or bumptiousness or rudeness or severe lack of respect or being a troll.


                      ahhh, yes, Trolls get something they don't expect. They are begging for Flaming, and yet, POOF! they get a contrexing! what you do, is you make it bullet-proof. You take each point given by the poster and give a well-researched, immaculately drawn, bulletproof counterpoint. If you see any direct personal insults that seem out-of-context, then the contrexer is likely a trainee in the fine art of Contrexing. Although, the rules if Contrexing border the land of Flames, so one or two "yo mamas" might slip out even from the most season professional.

                      "overwhelmed by the dark side you must not be. Use insults in context and not as double-adjectives you must"

                      -The book of Contrex, "Fire Wrath Thee" 13-40
                      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 #29 on: January 22, 2009, 04:45:33 PM »
                        Oh, so now contrexing is some kind of rule. Sure....
                        οτη άβγαλτος μεταφ βαθμολογία

                        Dias de verano

                        • Guest
                        Re: How to see if a string exists in a txt file.
                        « Reply #30 on: January 23, 2009, 12:13:27 AM »
                        Oh, so now contrexing is some kind of rule. Sure....

                        Not a rule, more a process.

                        Helpmeh



                          Guru

                        • Roar.
                        • Thanked: 123
                          • Yes
                          • Yes
                        • Computer: Specs
                        • Experience: Familiar
                        • OS: Windows 8
                        Re: How to see if a string exists in a txt file.
                        « Reply #31 on: January 24, 2009, 06:45:26 PM »
                        Oh, so now contrexing is some kind of rule. Sure....
                        Contrexing is not A rule, but it HAS a (almost) unspoken set of rules that you must follow to be successful in Contrexing.
                        Where's MagicSpeed?
                        Quote from: 'matt'
                        He's playing a game called IRL. Great graphics, *censored* gameplay.

                        BatchFileCommand

                          Topic Starter


                          Hopeful
                        • Thanked: 1
                          Re: How to see if a string exists in a txt file.
                          « Reply #32 on: January 24, 2009, 07:42:19 PM »
                          Quote
                          Contrexing (proper-verb=must have capital) is the act of flaming another user, in the way that Contrex would have.

                          What happened to the simple definition.
                          οτη άβγαλτος μεταφ βαθμολογία

                          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 #33 on: January 24, 2009, 07:45:23 PM »
                          Quote
                          Contrexing (proper-verb=must have capital) is the act of flaming another user, in the way that Contrex would have.

                          What happened to the simple definition.

                          What simple definition?
                          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 #34 on: January 24, 2009, 07:52:07 PM »
                            Here's the simple explanation of Contrexing

                            Quote
                            Contrexing (proper-verb=must have capital) is the act of flaming another user, in the way that Contrex would have.

                            But after that it comes out to a process, some kind of proverb. Whatever you people call it.
                            οτη άβγαλτος μεταφ βαθμολογία

                            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 #35 on: January 24, 2009, 07:55:18 PM »
                            Here's the simple explanation of Contrexing

                            Quote
                            Contrexing (proper-verb=must have capital) is the act of flaming another user, in the way that Contrex would have.

                            But after that it comes out to a process, some kind of proverb. Whatever you people call it.

                            The definition advanced because you refused to understand the previous explanations.
                            I was trying to dereference Null Pointers before it was cool.