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

Author Topic: How to check condition in txt file  (Read 7136 times)

0 Members and 1 Guest are viewing this topic.

Yogesh123

    Topic Starter


    Beginner

    How to check condition in txt file
    « on: November 17, 2009, 05:45:09 AM »
    Dear Experts,

    How to do this,

    if token no 5 in abc.txt is LEQ 25, do echo %%i %%j %%k %%l %%m %%n %%o of abc.txt to output.txt

    this is what i have tried
    cd\
    d:
    for /f "tokens=5" %%i in (abc.txt) do set LD=%%i
    if %LD% LEQ 25 (echo %%i %%j %%k %%l %%m %%n %%o>> output.txt
    ) else (
    echo Noresult found
    )
    pause


    abc.txt has following record,
    abc.tre 0 W131738 qwe 16 2009
    sfsdf.sdfsdf 0 W131455 qwe 27 2009
    fsdf.rty 0 W7F0BCU182 qwe 30 2009
    qw.qwe 2 w4059 qwe 15 2009
    wsx.rfv 2 W0132017 qwe 12 2009
    xdr.cft 0 W0132017 qwe 11 2009
    tgb.ikm 2 W0131738 qwe 31 2009
    xdr.cft 0 W0132017 qwe 25 2009


    Output.txt should have, (records where the 5th coloumn value is LEQ 25 )
    abc.tre 0 W131738 qwe 16 2009
    qw.qwe 2 w4059 qwe 15 2009
    wsx.rfv 2 W0132017 qwe 12 2009
    xdr.cft 0 W0132017 qwe 11 2009
    xdr.cft 0 W0132017 qwe 25 2009


    but i am getting following result in output.txt
    %i %j %k %l %m %n %o
    pls advise.

    gh0std0g74



      Apprentice

      Thanked: 37
      Re: How to check condition in txt file
      « Reply #1 on: November 17, 2009, 05:52:35 AM »
      you are missing a bracket of the for loop

      Yogesh123

        Topic Starter


        Beginner

        Re: How to check condition in txt file
        « Reply #2 on: November 17, 2009, 06:01:27 AM »
        Dear gh0std0g74,
        are you talking about for bracket,
        i have tried by this also , but not working
        cd\
        d:
        for /f "tokens=5" %%i in (abc.txt) do (
        set LD=%%i
        if %LD% LEQ 25 (echo %%i %%j %%k %%l %%m %%n %%o>> output.txt
        ) else (
        echo Noresult found
        )
        )
        pause

        Salmon Trout

        • Guest
        Re: How to check condition in txt file
        « Reply #3 on: November 17, 2009, 08:54:36 AM »
        Fixed your code. The 5th token is %%m so I don't know why you were testing %%i. Also you need to learn about delayed expansion.

        Code: [Select]
        setlocal enabledelayedexpansion
        cd\
        d:
        for /f "tokens=5" %%i in (abc.txt) do (
        set LD=%%m
        if !LD! LEQ 25 (
        echo %%i %%j %%k %%l %%m %%n %%o>> output.txt
        ) else (
        echo No result found
        )
        )

        Yogesh123

          Topic Starter


          Beginner

          Re: How to check condition in txt file
          « Reply #4 on: November 17, 2009, 09:21:41 AM »
          Dear Salmon Trout,
          seems to be not working properly,
          the result i got in output.txt is as follows,
          16 %j %k %l %m %n %o
          27 %j %k %l %m %n %o
          30 %j %k %l %m %n %o
          15 %j %k %l %m %n %o
          12 %j %k %l %m %n %o
          11 %j %k %l %m %n %o
          31 %j %k %l %m %n %o
          25 %j %k %l %m %n %o


          the result in output.txt should be (the output.txt should omit the record, where in the 5th token of abc.txt is greater than 25)
          abc.tre 0 W131738 qwe 16 2009
          qw.qwe 2 w4059 qwe 15 2009
          wsx.rfv 2 W0132017 qwe 12 2009
          xdr.cft 0 W0132017 qwe 11 2009
          xdr.cft 0 W0132017 qwe 25 2009

          Salmon Trout

          • Guest
          Re: How to check condition in txt file
          « Reply #5 on: November 17, 2009, 09:33:33 AM »
          "tokens=1-7"

          You want to process tokens %%i to %%o - that makes 7, so "tokens=5" is wrong.

          You should be checking these things yourself!

          Code: [Select]
          setlocal enabledelayedexpansion
          cd\
          d:
          for /f "tokens=1-7" %%i in (abc.txt) do (
          set LD=%%m
          if !LD! LEQ 25 (
          echo %%i %%j %%k %%l %%m %%n %%o>> output.txt
          ) else (
          echo No result found
          )
          )
          « Last Edit: November 17, 2009, 09:55:39 AM by Salmon Trout »

          Yogesh123

            Topic Starter


            Beginner

            Re: How to check condition in txt file
            « Reply #6 on: November 17, 2009, 09:57:57 AM »
            Thanx Salmon Trout,
            Condition checking part is done,
            but values of %%j %%k %%l %%m %%n are not displaying

            result of output.txt i got is,
            16 %j %k %l %m %n %o
            15 %j %k %l %m %n %o
            12 %j %k %l %m %n %o
            11 %j %k %l %m %n %o

            please advise.

            Salmon Trout

            • Guest
            Re: How to check condition in txt file
            « Reply #7 on: November 17, 2009, 10:04:47 AM »
            my abc.txt

            Code: [Select]
            abc.tre 0 W131738 qwe 16 2009
            sfsdf.sdfsdf 0 W131455 qwe 27 2009
            fsdf.rty 0 W7F0BCU182 qwe 30 2009
            qw.qwe 2 w4059 qwe 15 2009
            wsx.rfv 2 W0132017 qwe 12 2009
            xdr.cft 0 W0132017 qwe 11 2009
            tgb.ikm 2 W0131738 qwe 31 2009
            xdr.cft 0 W0132017 qwe 25 2009

            my batch

            Code: [Select]
            setlocal enabledelayedexpansion
            for /f "tokens=1-7" %%i in (abc.txt) do (
            set LD=%%m
            if !LD! LEQ 25 (
            echo %%i %%j %%k %%l %%m %%n %%o>> output.txt
            ) else (
            echo No result found
            )
            )

            My output.txt

            Code: [Select]
            abc.tre 0 W131738 qwe 16 2009
            qw.qwe 2 w4059 qwe 15 2009
            wsx.rfv 2 W0132017 qwe 12 2009
            xdr.cft 0 W0132017 qwe 11 2009
            xdr.cft 0 W0132017 qwe 25 2009

            I advise: check your code carefully! (Do you need glasses?)

            Yogesh123

              Topic Starter


              Beginner

              Re: How to check condition in txt file
              « Reply #8 on: November 17, 2009, 10:28:34 AM »
              Thanks a lot Salmon Trout,
              followed ur advise,  :), had small break and cleaned my glasses.
              its working great.

              billrich

              • Guest
              Re: How to check condition in txt file
              « Reply #9 on: November 17, 2009, 11:51:06 AM »

              C:\batch>cat yo.bat
              Code: [Select]
              @echo off
              rem for /f "delims=" %%a in (abc5.txt) do (
              rem echo %%a
              rem pause
              )

              setlocal enabledelayedexpansion
              for /f "tokens=1-7" %%i in (abc5.txt) do (
                      set LD=%%m
                      if !LD! LEQ 25 (
                              echo %%i %%j %%k %%l %%m %%n %%o
                      ) else (
              echo.
              echo No result found
                              echo  !LD! is greater than 25
                              echo %%i %%j %%k %%l %%m %%n %%o
              echo.
                      )
              )

              )

              OUTPUT:

              C:\batch>yo.bat
              abc.tre 0 W131738 Nov 16 12:00:57 2009
              sfsdf.sdfsdf 0 W131455 Nov 16 10:02:11 2009
              fsdf.rty 0 W7F0BCU182 Nov 16 11:02:12 2009
              qw.qwe 2 w4059 Nov 16 09:05:38 2009

              No result found
               26 is greater than 25
              wsx.rfv 2 W0132017 Nov 26 05:06:13 2009

              xdr.cft 0 W0132017 Nov 16 07:06:33 2009
              tgb.ikm 2 W0131738 Nov 16 12:15:01 2009
              C:\batch>

              billrich

              • Guest
              Re: How to check condition in txt file
              « Reply #10 on: November 17, 2009, 12:48:17 PM »

              C:\batch>cat yo2.bat

              Code: [Select]
              @echo off
              rem for /f "delims=" %%a in (abc5.txt) do (
              rem echo %%a
              rem pause
              )

              rem setlocal enabledelayedexpansion
              for /f "tokens=1-7" %%i in (abc5.txt) do (
                      set LD=%%m
                      if %%m LEQ 25 (
                              echo %%i %%j %%k %%l %%m %%n %%o
                      ) else (
              echo.
              echo No result found
                              echo  %%m is greater than 25
                              echo %%i %%j %%k %%l %%m %%n %%o
              echo.
                      )
              )

              )

              OUTPUT:

              C:\batch>yo2.bat
              abc.tre 0 W131738 Nov 16 12:00:57 2009
              sfsdf.sdfsdf 0 W131455 Nov 16 10:02:11 2009
              fsdf.rty 0 W7F0BCU182 Nov 16 11:02:12 2009
              qw.qwe 2 w4059 Nov 16 09:05:38 2009

              No result found
               26 is greater than 25
              wsx.rfv 2 W0132017 Nov 26 05:06:13 2009

              xdr.cft 0 W0132017 Nov 16 07:06:33 2009
              tgb.ikm 2 W0131738 Nov 16 12:15:01 2009
              C:\batch>

              gh0std0g74



                Apprentice

                Thanked: 37
                Re: How to check condition in txt file
                « Reply #11 on: November 17, 2009, 01:18:16 PM »
                @OP, are you sure this is a foolproof method to check for date just by comparing 1 or 2 token??

                Salmon Trout

                • Guest
                Re: How to check condition in txt file
                « Reply #12 on: November 17, 2009, 01:44:15 PM »
                @OP, are you sure this is a foolproof method to check for date just by comparing 1 or 2 token??

                He'll tell you in 13 days  :)

                billrich

                • Guest
                Re: How to check condition in txt file
                « Reply #13 on: November 17, 2009, 02:25:46 PM »
                @OP, are you sure this is a foolproof method to check for date just by comparing 1 or 2 token??

                There should be no problem as long as the input date is consistent.

                i.e.:

                The fifth field ( fifth token ) is the day of the month and the default delimiter is a space.

                Salmon Trout

                • Guest
                Re: How to check condition in txt file
                « Reply #14 on: November 17, 2009, 03:19:26 PM »
                But 1st December satisfies LEQ 30th November; and I see trouble ahead if the day number has a leading zero. I guess we'll find out in a fortnight.