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

Author Topic: need to read a txt file and display a word on command line  (Read 4420 times)

0 Members and 1 Guest are viewing this topic.

tinkuy

    Topic Starter


    Greenhorn

    need to read a txt file and display a word on command line
    « on: July 08, 2010, 09:01:09 PM »
    Hi

    I need to read a stringfrom txt file and need to put the string in a variable and print the variable on a dos prompt

    can any one help

    regards
    « Last Edit: July 08, 2010, 09:45:34 PM by tinkuy »

    gpl



      Apprentice
    • Thanked: 27
      Re: need to read a txt file and display a word on command line
      « Reply #1 on: July 08, 2010, 09:50:32 PM »
      Is this string in the first, or only line or is it on some other arbitrary line ?
      if the first/only line then the simplest is
      Code: [Select]
      Set /P line=<myfile.txt
      echo %line%

      tinkuy

        Topic Starter


        Greenhorn

        Re: need to read a txt file and display a word on command line
        « Reply #2 on: July 08, 2010, 10:27:58 PM »
        i need to compare find the string that is there in the file with the string i have if it exist then i want to display the string and come out of the batch file

        pls can help on this

        thanks

        marvinengland



          Hopeful

          Thanked: 11
          Re: need to read a txt file and display a word on command line
          « Reply #3 on: July 09, 2010, 12:19:43 AM »
          i need to compare find the string that is there in the file with the string i have if it exist then i want to display the string and come out of the batch file

          pls can help on this

          thanks


          C:\test>type displaystr.bat
          @echo off
          echo  stringIhave > mystr.txt

          echo  nonsense >> mystr.txt

          echo  computerhope  >> mystr.txt

          findstr  "string" mystr.txt  > x.txt

          set  /p mystr=<x.txt

          If %mystr%==stringIhave (
          echo mystr=%mystr%
          goto :end
          )
          echo string does not exist
          :end

          Output:
          C:\test>displaystr.bat
          mystr= stringIhave

          C:\test>
          « Last Edit: July 09, 2010, 12:49:37 AM by marvinengland »
          USA

          tinkuy

            Topic Starter


            Greenhorn

            Re: need to read a txt file and display a word on command line
            « Reply #4 on: July 09, 2010, 01:46:48 AM »
            hi

            this is working. but it i have 2 string for eg (read write.) then it does not work

            marvinengland



              Hopeful

              Thanked: 11
              Re: need to read a txt file and display a word on command line
              « Reply #5 on: July 09, 2010, 04:35:12 AM »

              This is working. but it I have 2 string for eg (read write.) then it does not work

              I'm not sure what you are asking?

              Maybe put quotes around both strings and treat as one:  "read write"

              or run the program twice?

              Or Modify the code to do what you need?

              Post your code and error message.
              USA

              tinkuy

                Topic Starter


                Greenhorn

                Re: need to read a txt file and display a word on command line
                « Reply #6 on: July 09, 2010, 07:44:01 AM »
                when i run the following code

                @echo off
                echo  stringIhave > mystr.txt
                echo  nonsense >> mystr.txt
                echo  computer hope  >> mystr.txt

                findstr  "Computer hope" mystr.txt  > x.txt

                set  /p mystr=<x.txt

                If %mystr%=="Computer hope" (
                echo mystr=%mystr%
                goto :end
                )
                echo string does not exist
                :end

                I get the error as
                "hope was unexpected at this time."

                I need to compare computer hope

                thanks

                Salmon Trout

                • Guest
                Re: need to read a txt file and display a word on command line
                « Reply #7 on: July 09, 2010, 08:28:45 AM »
                Bad idea to copy marvinengland's scripts.

                1. Every line in mystr.txt will have a leading space. Even if everything else was OK, this would make the search for "Computer hope" doomed to fail.

                2. In the if test, there are some quotes missing, and also some sloppiness around case.




                Salmon Trout

                • Guest
                Re: need to read a txt file and display a word on command line
                « Reply #8 on: July 09, 2010, 09:03:20 AM »
                Code: [Select]
                @echo off

                set wantedstring=Computer Hope

                set found=0

                echo Moat has managed to evade police for seven days > mystr.txt
                echo after shooting his ex-girlfriend last Saturday >> mystr.txt
                echo morning and murdering her new boyfriend. He shot >> mystr.txt
                echo and wounded an unarmed police officer 24 hours later. >> mystr.txt
                echo His new job is as a moderator on Computer Hope. >> mystr.txt
                echo So watch out marvinengland >> mystr.txt

                for /f "delims=" %%S in (mystr.txt) do (
                echo %%S | find "%wantedstring%">nul && (
                set found=1
                set fileline=%%S
                )
                )

                if %found% equ 1 (
                echo found "%wantedstring%" in this line:
                echo %fileline%
                ) else (
                echo string "%wantedstring%" not found
                )

                mac50

                • Guest
                Re: need to read a txt file and display a word on command line
                « Reply #9 on: July 10, 2010, 03:30:29 AM »
                when i run the following code

                echo  computer hope  >> mystr.txt

                findstr  "Computer hope" mystr.txt  > x.txt

                set  /p mystr=<x.txt

                If %mystr%=="Computer hope" (
                echo mystr=%mystr%
                goto :end
                )
                echo string does not exist
                :end

                I get the error as
                "hope was unexpected at this time."

                I need to compare computer hope

                thanks

                C:\test>type  dis1.bat

                @echo off
                echo  "computer hope"  > mystr.txt

                echo  nonsense >> mystr.txt

                rem echo  computerhope  >> mystr.txt

                findstr  "computer hope" mystr.txt  > x.txt

                set  /p mystr=<x.txt

                If %mystr%=="computer hope" (
                echo mystr=%mystr%
                goto :end
                )
                echo string does not exist
                :end

                C:\test>dis1.bat
                mystr= "computer hope"

                C:\test>
                tinkuy ,
                echo  computer hope  >> mystr.txt
                echo  "computer hope"  >> mystr.txt
                use quotes on above  input

                « Last Edit: July 10, 2010, 03:41:06 AM by mac50 »

                Salmon Trout

                • Guest
                Re: need to read a txt file and display a word on command line
                « Reply #10 on: July 10, 2010, 03:48:17 AM »
                Interesting posting style you got there, mac50. Kinda reminds me of someone?

                Helpmeh



                  Guru

                • Roar.
                • Thanked: 123
                  • Yes
                  • Yes
                • Computer: Specs
                • Experience: Familiar
                • OS: Windows 8
                Re: need to read a txt file and display a word on command line
                « Reply #11 on: July 10, 2010, 01:54:50 PM »
                Interesting posting style you got there, mac50. Kinda reminds me of someone?

                Bill never learns, does he?
                Where's MagicSpeed?
                Quote from: 'matt'
                He's playing a game called IRL. Great graphics, *censored* gameplay.