Text file parsing in batch files

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • watertraveller
    New Member
    • Sep 2008
    • 4

    Text file parsing in batch files

    Hi. I'm new to batch files, and relatively new to the Windows command line in general.

    I'm making a batch file for the Windows XP command line. I want to examine, for each line of a text file, what the first few characters are. I want to count up how many lines started with one set of characters, how many started with another, etc. And I want to output that count into a different text file.

    I know how to read in the text and output it to a new file, but I don't know how to check just the first few characters. I also don't know how to store the count in a variable in the batch file. (Do batch files even have variables? They must, right?)

    Thank you for your time; I appreciate it.
    Last edited by watertraveller; Sep 17 '08, 10:23 PM. Reason: I can't get my signature to show up.
  • Nepomuk
    Recognized Expert Specialist
    • Aug 2007
    • 3111

    #2
    Hi watertraveller! Welcome to bytes.com!

    It's great to have you here!

    When you post, please always keep to the Posting Guidelines and when you post code, please post it in [code] ... [/code] tags.

    About your question: Yes, you can set variables in batch files. For example:
    Code:
    set countA=0
    set /a countA+=1
    echo There were %countA% a's in that text.
    will return the current value of the variable countA (which is 1).

    I'm not sure how to actually extract the first character from a line, but that should certainly help a bit.

    Otherwise, I'll just wish you the best and hope you enjoy being part of bytes.com!

    Greetings,
    Nepomuk

    PS.: I've started a thread in the Feedback Forum to find out, why that signature wont show up.

    Comment

    • jg007
      Contributor
      • Mar 2008
      • 283

      #3
      one command that might help you is ' FOR ' , just type ' for /? ' to see the syntax

      I don't really know if you can do exactly what you want from a batch file / dos and I have not tried to do anything similar but I suspect that you might have to resort to using VBScript to achieve this.

      also a little more complicated but you might want to look at Microsoft's Powershell as it is VERY powerful once you work out how to use it :)

      Comment

      • watertraveller
        New Member
        • Sep 2008
        • 4

        #4
        Thanks, I'll take a look at vbscript and, if I have to, PowerShell.

        Comment

        • jg007
          Contributor
          • Mar 2008
          • 283

          #5
          post back if you have any problems and if I can find time I will see if I can work out some vbs code.

          Comment

          Working...