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

Author Topic: batch check codition & echo  (Read 4294 times)

0 Members and 1 Guest are viewing this topic.

Yogesh123

    Topic Starter


    Beginner

    batch check codition & echo
    « on: November 17, 2009, 12:07:14 AM »
    Dear Experts,

    How to do this,

    if token no 6 in abc.txt is LEQ current time, do echo %%a %%b %%c %%d %%e %%f %%g >> output.txt

    abc.txt is =
    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
    wsx.rfv 2 W0132017 Nov 16 05:06:13 2009
    xdr.cft 0 W0132017 Nov 16 07:06:33 2009
    tgb.ikm 2 W0131738 Nov 16 12:15:01 2009


    if current time is 11:00:00
    Output.txt should have,
    sfsdf.sdfsdf 0 W131455 Nov 16 10:02:11 2009
    qw.qwe 2 w4059 Nov 16 09:05:38 2009
    wsx.rfv 2 W0132017 Nov 16 05:06:13 2009
    xdr.cft 0 W0132017 Nov 16 07:06:33 2009


    This is what i have tried,
    but the output.txt is just showing %i %j %k %l %m %n %o
    cd\
    set Ct=%time%
    set Ctt=%Ct:~0,2%
    for /f "tokens=6" %%i in (abc.txt) do set LD=%%i
    set LDD=%LD:~0,2%
    if %LDD% LEQ %Ctt% (echo %%i %%j %%k %%l %%m %%n %%o>> output.txt
    ) else (
    echo No result
    )
    pause

    please advise,
    « Last Edit: November 17, 2009, 12:39:27 AM by Yogesh123 »

    gh0std0g74



      Apprentice

      Thanked: 37
      Re: batch check codition & echo
      « Reply #1 on: November 17, 2009, 12:28:22 AM »
      i often see your posts. you should show what you have tried by now.

      Yogesh123

        Topic Starter


        Beginner

        Re: batch check codition & echo
        « Reply #2 on: November 17, 2009, 12:41:43 AM »
        Dear gh0std0g74,
        as advised,
        I have updated my post

        gh0std0g74



          Apprentice

          Thanked: 37
          Re: batch check codition & echo
          « Reply #3 on: November 17, 2009, 01:51:39 AM »
          use a better language to do date comparison. eg in vbscript
          Code: [Select]
          Set objFS=CreateObject("Scripting.FileSystemObject")
          Set objArgs = WScript.Arguments
          strFile = objArgs(0)
          Set objFile = objFS.OpenTextFile(strFile)
          CurrentDateTime =Now
          Do Until objFile.AtEndOfStream
          strLine = objFile.ReadLine
          s = Split(strLine," ")
          t=""
          For i = 3 To UBound(s)
          t = t &" "&s(i)
          Next
          t=Trim(t)
          datetime = Split(t," ")
          mth= datetime(0)
          dy =  datetime(1)
          thetime =  Split(datetime(2),":")
          hr = thetime(0)
          min = thetime(1)
          sec = thetime(2)
          theyr =  datetime(3)
          strDateTimeInFile = CDate( dy & " " & mth & " " & theyr) &" "&CDate( hr & ":" & min  & ":" & sec  )
          If DateDiff( "d", CurrentDatetime ,strDateTimeInFile ) > -1 Then
          WScript.Echo "CurrentTime is earlier than strDateTimeInFile"
          WScript.Echo strLine
          End If
          Loop
          on command line
          Code: [Select]
          c:\test> cscript /nologo test.vbs file

          Yogesh123

            Topic Starter


            Beginner

            Re: batch check codition & echo
            « Reply #4 on: November 17, 2009, 02:06:24 AM »
            Can it be done using batch dos commands?
            beacause i am not more concern about date comparison, comparison parameter could be no's also.

            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: batch check codition & echo
            « Reply #5 on: November 17, 2009, 02:09:50 AM »
            nice

            just one thing :P


            Quote
            strDateTimeInFile = CDate( dy & " " & mth & " " & theyr) &" "&CDate( hr & ":" & min  & ":" & sec  )

            Why use CDate and String concat? You could use DateSerial and TimeSerial; also, this will allow for the return type to be a Date, rather then a string. (although I imagine type coercion is making everything work as it is; you can never really be sure in different locales)

            Code: [Select]

            DateTimeInFile= DateSerial(theyr,mth,dy)+TimeSerial(hr,min,sec)

            OLE automation Dates are stored as "double" types; "1" is one day after the origin (I can't recall, I think it was in the 1700's or something); times are represented as fractional portions. Therefore, we can add a TimeSerial to a DateSerial to create an entire TimeStamp.

            Can it be done using batch dos commands?
            beacause i am not more concern about date comparison, comparison parameter could be no's also.

            You'll probably have to hope Salmon Trout or one of the other Batch Experts decides to tackle this one if you want a batch solution.
            I was trying to dereference Null Pointers before it was cool.

            gh0std0g74



              Apprentice

              Thanked: 37
              Re: batch check codition & echo
              « Reply #6 on: November 17, 2009, 02:22:57 AM »
              nice

              just one thing :P


              Why use CDate and String concat? You could use DateSerial and TimeSerial; also, this will allow for the return type to be a Date, rather then a string. (although I imagine type coercion is making everything work as it is; you can never really be sure in different locales)

              Code: [Select]

              DateTimeInFile= DateSerial(theyr,mth,dy)+TimeSerial(hr,min,sec)

              heh, i had always wanted to try using these 2 functions, but never really got to it. CDate is what i am familiar with so i just wrote the script without much thinking.  But good call though.

              gh0std0g74



                Apprentice

                Thanked: 37
                Re: batch check codition & echo
                « Reply #7 on: November 17, 2009, 02:25:49 AM »
                beacause i am not more concern about date comparison, comparison parameter could be no's also.
                why do you say its not about date comparison? didn't you say you need the 6th token to be LEQ current time or something.?? If it is, then IT IS date comparison. Unless your date format in the file is formatted to become like this
                YYYYMMDDHHMMSS, then it may be possible for comparison. (but still you need to format the current time and date also ), which is too much of a hassle to do in batch. Its time to move on.

                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: batch check codition & echo
                « Reply #8 on: November 17, 2009, 02:37:16 AM »
                heh, i had always wanted to try using these 2 functions, but never really got to it. CDate is what i am familiar with so i just wrote the script without much thinking.  But good call though.

                Basically, when dealing with Dates, I've always found it works best to have everything as a date, rather then a string, always best to avoid Locale issues. Although as you said you whipped it up without thinking and it is definitely possible to over-analyze any approach ;)
                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: batch check codition & echo
                « Reply #9 on: November 17, 2009, 04:24:02 AM »
                @echo off
                setlocal enable delayed expansio
                for /f "tokens=1-7" %%a in (file.txt) do (
                set tme1=!time::=!
                set tme2=%%f
                set tme2=!tme2::=!
                if !tme2! LEQ !tme1! echo %%a %%b %%c %%d %%e %%f %%g >> output.txt
                )
                Untested, might not work, but it will at least point you in the right direction.
                « Last Edit: November 18, 2009, 04:37:56 AM by Helpmeh »
                Where's MagicSpeed?
                Quote from: 'matt'
                He's playing a game called IRL. Great graphics, *censored* gameplay.

                Dusty



                  Egghead

                • I could if she would, but she won't so I don't.
                • Thanked: 75
                • Experience: Beginner
                • OS: Windows XP
                Re: batch check codition & echo
                « Reply #10 on: November 18, 2009, 01:35:10 AM »
                Yogesh - try this.  It is dependent on your %time% format being hh:mm:ss.ms, no allowance is made for AM or PM if that is included in the format.  If you use the %1 variable for testing purposes it must be entered in the full format as above.  The script is untested.

                Code: [Select]
                @echo off>output.txt
                setlocal enabledelayedexpansion

                :: set time=%1

                set time=%time::=%
                set time=%time:~0,6%

                for /f "delims=*" %%1 in (abc.txt) do (
                    set line=%%1

                        for /f "tokens=1-6" %%2 in ("!line!") do (
                            set filetime=%%7
                            set filetime=!filetime::=!

                                if !filetime! leq !time! echo %%1>>output.txt
                )
                  )

                type output.txt

                No doubt one of the scripting gurus will come up with something much more efficient.

                Good luck.
                « Last Edit: November 18, 2009, 01:46:14 AM by Dusty »
                One good deed is worth more than a year of good intentions.

                billrich

                • Guest
                Re: batch check codition & echo
                « Reply #11 on: November 18, 2009, 03:29:39 AM »
                We must use:
                setlocal enabledelayedexpansion
                Use !LD! not %LD% Salmon Trout provided this info a day or so ago


                rem I remarked set Ct=%time% out for a better test.
                Remove set Ctt=08  and the rem's in the code


                C:\batch>cat  yo3.bat

                Code: [Select]
                rem set Ct=%time%
                rem echo Ct = %Ct%
                rem set Ctt=%Ct:~0,2%

                set Ctt=08
                echo Ctt = %Ctt%

                @echo off
                setlocal enabledelayedexpansion
                for /f "tokens=1-7" %%i in (abc5.txt) do (
                set LD=%%n
                echo LD=!LD!
                set LDD=!LD:~0,2!
                echo LDD=!LDD!
                echo LDD less or equal Ctt
                if !LDD! LEQ !Ctt! (
                echo %%i %%j %%k %%l %%m %%n %%o
                ) else (

                echo.
                echo No result found

                echo.
                   
                )

                OUTPUT:

                C:\batch> yo3.bat
                Ctt = 08
                LD=12:00:57
                LDD=12
                LDD less or equal Ctt

                No result found

                LD=10:02:11
                LDD=10
                LDD less or equal Ctt

                No result found

                LD=11:02:12
                LDD=11
                LDD less or equal Ctt

                No result found

                LD=09:05:38
                LDD=09
                LDD less or equal Ctt

                No result found

                LD=05:06:13
                LDD=05
                LDD less or equal Ctt
                wsx.rfv 2 W0132017 Nov 26 05:06:13 2009
                LD=07:06:33
                LDD=07
                LDD less or equal Ctt
                xdr.cft 0 W0132017 Nov 16 07:06:33 2009
                LD=12:15:01
                LDD=12
                LDD less or equal Ctt

                No result found

                C:\batch>
                « Last Edit: November 18, 2009, 04:40:40 AM by billrich »