I currently have a DOS script read a file and count the number of times it encounters the character string HDR.  Need to look in 1st position.

Posted on 2005-04-15
Last Modified: 2008-01-09
Here is the script.  I need to modify this script to read for the text string "HDR" in the first three positions of each record of the file.  Currently it looks for it anywhere in the record.

set COUNT=0
rem *** Count number of header records in inbound file for AUDITW2 check ***
@echo on
cd /d g:
for /F "tokens=1-3" %%a IN ('FIND /C "HDR" %2.%3') DO set COUNT=%%c
Question by:eshapley
    LVL 82

    Accepted Solution

    Assuming that with "the first three positions of each record" you mean the first three characters in a line, you can use this (on W2k or later):

    @echo off
    setlocal EnableDelayedExpansion
    set RecordFile=D:\Temp\test.txt
    set Count=0
    for /f "tokens=1" %%a in ('type "%RecordFile%" ^| find /i "HDR"') do (
      set Header=%%a
      if /i "!Header:~0,3!"=="HDR" set /a Count+=1
    echo %Count%

    If the "HDR" is even a separate string, separated by a space or whatever from the rest of the line, this can even be shortened to

    @echo off
    set RecordFile=D:\Temp\test.txt
    set Count=0
    :: *** In the next line, define the character that separates the HDR from the rest of the line after 'delims='; currently, it's a space.
    for /f "tokens=1 delims= " %%a in ('type "%RecordFile%" ^| find /i "HDR"') do if /i "%%a"=="HDR" set /a Count+=1
    echo %Count%

    Author Comment

    BEAUTIFUL !! :-)

    Thanks for the speedy help.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    Suggested Solutions

    The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
    Using dates in 'DOS' batch files has always been tricky as it has no built in ways of extracting date information.  There are many tricks using string manipulation to pull out parts of the %date% variable or output of the date /t command but these r…
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    794 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now