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

Author Topic: Batch - How to Read text file variables in a loop  (Read 7635 times)

0 Members and 1 Guest are viewing this topic.

Yogesh123

    Topic Starter


    Beginner

    Batch - How to Read text file variables in a loop
    « on: November 13, 2009, 07:13:09 AM »
    Dear Experts,
    I am having a list of computer names in the text file which has computer name & time against it,
    ex. ABC.txt has,
        WST123    01:27 pm
        WST456    02:27 pm
        WST789    11:27 pm
        WST123    05:27 pm
        WST567    12:27 pm
        WST123    08:27 pm
        WST567    07:27 pm
        WST456    03:27 pm
        WST123    11:27 pm
        WST789    12:27 pm


    from that i wanted to read last entry of every specific computer and transfer that entry into the logfile.txt
        ex. for WST123 last entry is  "WST123    11:27 pm"
        for WST567 last entry is "WST567    07:27 pm"
        for WST456 last entry is "WST456    03:27 pm"
        for WST789 last entry is "WST789    12:27 pm"

    and finally logfile.txt should contain
        WST123   11:27 pm
        WST567   07:27 pm
        WST456    03:27 pm
        WST789    12:27 pm


    please advance,
    thank you in advance.

    gh0std0g74



      Apprentice

      Thanked: 37
      Re: Batch - How to Read text file variables in a loop
      « Reply #1 on: November 13, 2009, 07:20:42 AM »
      if you can download and use gawk for windows(see my sig)
      Code: [Select]
      c:\test> gawk "{a[$1]=$2}END{for(i in a)print i,a[i]}" file >logfile.txt
      WST123 11:27
      WST567 07:27
      WST789 12:27
      WST456 03:27
      next time, try writing the code yourself first.

      billrich

      • Guest
      Re: Batch - How to Read text file variables in a loop
      « Reply #2 on: November 13, 2009, 11:38:05 AM »
       8)
      « Last Edit: November 14, 2009, 06:22:05 PM by billrich »

      Salmon Trout

      • Guest
      Re: Batch - How to Read text file variables in a loop
      « Reply #3 on: November 13, 2009, 01:42:26 PM »
      This actually does what the OP asked for.

      Code: [Select]
      @echo off
      if exist *.pqr del *.pqr
      for /f "tokens=1,2 delims= " %%A in (ABC.txt) do echo %%B>%%A.pqr
      for /f "delims=" %%B in ('dir /b *.pqr') do (
      for /f "delims=" %%C in ('type %%B') do echo %%~nB %%C>>logfile.txt
      )
      del *.pqr

      logfile.txt

      Code: [Select]
      WST456 03:27
      WST567 07:27
      WST789 12:27
      WST123 11:27
      « Last Edit: November 13, 2009, 02:14:13 PM by Salmon Trout »

      Yogesh123

        Topic Starter


        Beginner

        Re: Batch - How to Read text file variables in a loop
        « Reply #4 on: November 14, 2009, 04:59:59 AM »
        Dear Salmon Trout,
        Thanx a lot,
        This thing is really working great,

        but how does it works exactly?
        what is *.pqr & 'type %%B' & %%~nB

        billrich

        • Guest
        Re: Batch - How to Read text file variables in a loop
        « Reply #5 on: November 14, 2009, 05:15:34 AM »
         8)
        « Last Edit: November 14, 2009, 06:23:14 PM by billrich »

        Salmon Trout

        • Guest
        Re: Batch - How to Read text file variables in a loop
        « Reply #6 on: November 14, 2009, 06:12:06 AM »
        if you are so clever, Billrich, explain to Yogesh123 what the code that I posted does, instead of wasting everybody's time with your childish nonsense, which actually is nonsense because it does not mean anything. I supplied code which provided what the OP asked for. As so often happens, you did not.

        Salmon Trout

        • Guest
        Re: Batch - How to Read text file variables in a loop
        « Reply #7 on: November 14, 2009, 06:43:56 AM »
        I thought you wouldn't

        Code: [Select]
        @echo off
        REM The extension .pqr is one I chose at random
        REM it can be any extension which is not found in the folder

        REM delete any files with that extension
        if exist *.pqr del *.pqr

        REM for each line in ABC.txt in turn,
        REM split the line into 2 tokens %%A and %%B

        REM %%A is the computer name %%B is the time

        REM you create a file:
        REM whose name is the the computer name
        REM whose contents is the time

        REM Use the > redirection operator so that
        REM each new file overwrites the previous file with that name

        REM after this loop you have one .pqr file for each computer
        REM containing the last time entry for that computer
        REM
        for /f "tokens=1,2 delims= " %%A in (ABC.txt) do echo %%B>%%A.pqr

        REM Now for each file with the extension .pqr
        REM put the filename into %%B

        for /f "delims=" %%B in ('dir /b *.pqr') do (

        REM  Echo the name part of the filename and the contents into logfile.txt
        for /f "delims=" %%C in ('type %%B') do echo %%~nB %%C>>logfile.txt
        )

        REM remove all the .pqr files
        del *.pqr
        « Last Edit: November 14, 2009, 08:24:46 AM by Salmon Trout »

        billrich

        • Guest
        Re: Batch - How to Read text file variables in a loop
        « Reply #8 on: November 14, 2009, 07:46:04 AM »
        if you are so clever, Billrich, explain to Yogesh123 what the code that I posted does, instead of wasting everybody's time with your childish nonsense, which actually is nonsense because it does not mean anything. I supplied code which provided what the OP asked for. As so often happens, you did not.



        I have no idea what you are talking about.  No one reading this thread has any idea what you are talking about.  But the Fishman is  from up north and the fishman  knows everything.  Is ST and Yogest123 the same person?


        gh0std0g74



          Apprentice

          Thanked: 37
          Re: Batch - How to Read text file variables in a loop
          « Reply #9 on: November 14, 2009, 08:03:08 AM »
          C:\batextra>type   vosloop.bat
          Code: [Select]
          @echo off

          for /f "tokens=1,2,3 delims= " %%a in (abc2.txt) do (echo %%a %%b %%c)

          type abc2.txt  >>  voslog.log

          OUTPUT:

          C:\batextra> vosloop.bat
          WST123 01:27 pm
          WST456 02:27 pm
          WST789 11:27 pm
          WST123 05:27 pm
          WST567 12:27 pm
          WST123 08:27 pm
          WST567 07:27 pm
          WST456 03:27 pm
          WST123 11:27 pm
          WST789 12:27 pm

          Log:


          C:\batextra>type voslog.log
          WST123    01:27 pm
          WST456    02:27 pm
          WST789    11:27 pm
          WST123    05:27 pm
          WST567    12:27 pm
          WST123    08:27 pm
          WST567    07:27 pm
          WST456    03:27 pm
          WST123    11:27 pm
          WST789    12:27 pm
          C:\batextra>
          what are you doing ? this is not what OP asked for. look at what we wants as output again in his first post.

          Salmon Trout

          • Guest
          Re: Batch - How to Read text file variables in a loop
          « Reply #10 on: November 14, 2009, 08:20:17 AM »
          is Billrich mentally ill, or "special" or something?

          gh0std0g74



            Apprentice

            Thanked: 37
            Re: Batch - How to Read text file variables in a loop
            « Reply #11 on: November 14, 2009, 08:36:27 AM »
            Dear Salmon Trout,
            Thanx a lot,
            This thing is really working great,

            but how does it works exactly?
            what is *.pqr & 'type %%B' & %%~nB

            the trick is this part
            Code: [Select]
            for /f "tokens=1,2 delims= " %%A in (ABC.txt) do echo %%B>%%A.pqr
            its echoing the time portion to a file, whose file name is the computer name. the rest you go figure out. Note that this method creates extra i/o processes, like the creation of .pqr files. If your ABC.txt has many entries, this will be a slow batch process due to i/o. Its probably more efficient to use memory (delayed expansion??? ) than i/o.

            billrich

            • Guest
            Re: Batch - How to Read text file variables in a loop
            « Reply #12 on: November 14, 2009, 08:39:39 AM »
            if you can download and use gawk for windows(see my sig)
            Code: [Select]
            c:\test> gawk "{a[$1]=$2}END{for(i in a)print i,a[i]}" file >logfile.txt
            WST123 11:27
            WST567 07:27
            WST789 12:27
            WST456 03:27
            next time, try writing the code yourself first.

            Casper,

            Casper gave the same answer as Billrich did.  And now Casper questions Billrich's answer?

            This a Board   for Children who fake their answers?

            Salmon Trout

            • Guest
            Re: Batch - How to Read text file variables in a loop
            « Reply #13 on: November 14, 2009, 09:00:02 AM »
            If your ABC.txt has many entries, this will be a slow batch process due to i/o. Its probably more efficient to use memory (delayed expansion??? ) than i/o.

            ABC.txt with 1500 lines (XP SP3, 3. 0 GHZ P4 Prescott, Seagate Free Agent 320GB usb external HDD)

            Code: [Select]
            S:\Test>(
            More? echo %date% %time%
            More? ltime3.bat
            More? echo %date% %time%
            More? )
            14/11/2009 15:57:24.06
            14/11/2009 15:57:32.41

            gh0std0g74



              Apprentice

              Thanked: 37
              Re: Batch - How to Read text file variables in a loop
              « Reply #14 on: November 14, 2009, 09:00:39 AM »
              Casper,

              Casper gave the same answer as Billrich did.  And now Casper questions Billrich's answer?

              This a Board   for Children who fake their answers?
              '
              you sh**head, my answer has the same output as OP's , although the "PM" is not there, but its a minor fix.
              but yours is totally different. don't be an S.

              Salmon Trout

              • Guest
              Re: Batch - How to Read text file variables in a loop
              « Reply #15 on: November 14, 2009, 09:05:12 AM »
              What he's doing is pure trollery. Needs banning.

              gh0std0g74



                Apprentice

                Thanked: 37
                Re: Batch - How to Read text file variables in a loop
                « Reply #16 on: November 14, 2009, 09:06:58 AM »
                ABC.txt with 1500 lines (XP SP3, 3. 0 GHZ P4 Prescott, Seagate Free Agent 320GB usb external HDD)

                Code: [Select]
                S:\Test>(
                More? echo %date% %time%
                More? ltime3.bat
                More? echo %date% %time%
                More? )
                14/11/2009 15:57:24.06
                14/11/2009 15:57:32.41
                if you see my gawk code, its makes use of associative arrays.  every time the same entry is encountered, the key is "overwritten" and current value is stored. At the end of file iteration, all it has is just latest entry of each computer name. I think batch can "simulate" this kind of behaviour using delayed expansion. Just feel that its nicer to go through memory than file i/o.

                billrich

                • Guest
                Re: Batch - How to Read text file variables in a loop
                « Reply #17 on: November 14, 2009, 09:12:37 AM »
                What he's doing is pure trollery. Needs banning.


                No, the fishman is the fake and Troll.  

                And now Casper is using vulgar language.

                Again no one  reading this thread knows what Salmon Trout is talking about.

                 Yogesh123, the original poster, wrote:

                "How does it works exactly?
                what is *.pqr & 'type %%B' & %%~nB"
                « Last Edit: November 14, 2009, 09:49:35 AM by billrich »

                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 - How to Read text file variables in a loop
                « Reply #18 on: November 14, 2009, 09:25:39 AM »
                The original post seems pretty clear that it indeed the last time entry for each machine name that they wanted It is in bold.

                Of course we could simply print out the very same thing being used as input and call it a day, but that's treading awfully close to what a politician might do (which is quite close to nothing).

                This is all well and good everybody makes mistakes such as this, or misunderstands posts. But to then turn around, and for the sole reason that one was unable to understand the query, accuse others of having multiple accounts to justify in their mind that they were in fact at a disadvantage is nothing short of childish.


                Billrich: your claims that they are the same person are based on a self-deluded fantasy that the provided solution does not meet the request put forth by the OP, when, in fact, it does. Additionally, the very portion that you missed in your solution was bold.

                And lastly, Yes, I know I provided no solution. But is it not true that no solution is better then a wrong one? At least I admit that I did not post a solution, there is no admittance to the clearly obvious fact that you omitted a few key requirements from your original response. This is no problem, a good number of your solutions, while provided in an unorthodox way do solve the OP's problem and they are content. However one cannot say they are perfect all the time, especially when one tries to cover so many threads; as you've said yourself, you are learning, we all are. It never hurts to concede when somebody is better at something, and I have to say from reading many of his posts (I think it's technically in the tens of thousands now, actually) I am not ashamed to say that his abilities with batch(especially regarding the NT extensions), and I'm sure many other things, is far greater then mine; the same goes for ghostdog and his reportoire of script languages; for me to not concede these truths is not a sign of strength in my abilities but rather a folly in that I cannot see my own weaknesses. When one cannot see their own weaknesses, they do not know whereupon to build strength.

                hmm, that got a little weird... anyway, basically, when one finds one solution to be inadequate to the original query, it is not a sign of weakness to say, "hey, I suppose I read wrong or was mistaken", and the same goes for solutions that do work but a better alternative is presented (I believe ghostdog has outscripted me on one or two threads ;), there is no reason the defend a inferior solution unless it has distinct advantages that are applicable to the Original Posters question.

                curses, why must my posts be so long...  

                and lastly, the original posters follow-up question was answered.
                I was trying to dereference Null Pointers before it was cool.

                Salmon Trout

                • Guest
                Re: Batch - How to Read text file variables in a loop
                « Reply #19 on: November 14, 2009, 09:26:12 AM »
                if you see my gawk code, its makes use of associative arrays.  every time the same entry is encountered, the key is "overwritten" and current value is stored. At the end of file iteration, all it has is just latest entry of each computer name. I think batch can "simulate" this kind of behaviour using delayed expansion. Just feel that its nicer to go through memory than file i/o.

                I agree; my code is, I freely admit, nothing more or less than a quick hack. Personally, I would rather tackle the requirement using VBscript.

                Salmon Trout

                • Guest
                Re: Batch - How to Read text file variables in a loop
                « Reply #20 on: November 14, 2009, 09:32:01 AM »
                When one cannot see their own weaknesses, they do not know whereupon to build strength.

                That is an extremely true and profound remark. Further, a person who is (obviously!) painfully aware of their own weaknesses, but out of pig-headedness and vanity refuses to see or address them is in an even more pitiful position.

                gh0std0g74



                  Apprentice

                  Thanked: 37
                  Re: Batch - How to Read text file variables in a loop
                  « Reply #21 on: November 14, 2009, 09:36:22 AM »
                  Again no one  reading this thread knows what Salmon Trout is taking about.
                  i do.

                  Salmon Trout

                  • Guest
                  Re: Batch - How to Read text file variables in a loop
                  « Reply #22 on: November 14, 2009, 10:26:58 AM »
                  Got it! I knew there must be a way that does not involve files. You have environment space to play with. Remembered about the 'am' & 'pm' part of the time as well. (Why won't people use the 24 hour clock like we do here in Europe? makes things so much simpler.)

                  Code: [Select]
                  @echo off
                  for /f "tokens=1,2,3 delims= " %%A in (ABC.txt) do call set lastentry_%%A=%%B_%%C
                  for /f "tokens=1,2,3,4 delims=_=" %%A in ('set ^| find "lastentry"') do echo %%B %%C %%D >> logfile.txt

                  logfile.txt (after 1 run) - note that using append >> operator will make logfile.txt grow each time the batch is run

                  Code: [Select]
                  WST123 11:27 pm
                  WST456 03:27 pm
                  WST567 07:27 pm
                  WST789 12:27 pm
                  « Last Edit: November 14, 2009, 11:32:06 AM by Salmon Trout »

                  gh0std0g74



                    Apprentice

                    Thanked: 37
                    Re: Batch - How to Read text file variables in a loop
                    « Reply #23 on: November 14, 2009, 04:33:06 PM »
                    Quote from: Salmon Trout
                    Got it! I knew there must be a way that does not involve files.
                    congrats. now how about one that uses delayed expansion. would like to see how it can be used to solve this.