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