Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1514
  • Last Modified:

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.

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
1 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%
eshapleyAuthor Commented:

Thanks for the speedy help.

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now