Help with multiple tokens in a DOS batch file

480 pts.
Tags:
DOS
DOS Batch
DOS program
Windows Server 2008
Windows XP
I need some help using multiple tokens in a DOS batch file. This is what I am trying to do;

Software/Hardware used:
Windows XP, WIndows server 2008

Answer Wiki

Thanks. We'll let you know when a new response is added.

I encountered internet problem, so the details of my question never got posted in the original post for this topic. I will try again.

I need some help using multiple tokens in a DOS batch file. This is what I am trying to do;
1) I am trying to loop through a set of files within a folder.

2) I only want the files that has _ORD_ in the name.

3) Within each file are a set of 3 lines that has the same name (below is a sample of the file), but I am only interested in the one that has the letters MT in the second token.

4) I also want to see if the data in the 3rd token = “92095276680067”. If it is, then add 1 to the counter.

I cannot get the correct syntax when using more than 1 tokens. Any help will be greatly appreciated.

This is a copy of the DOS batch:
<pre> @echo off
setlocal EnableDelayedExpansion
set dir=c:\temp
SET counter=0

for /f “tokens=2,3 delims= ” %%a in (‘find /I “MT” “%dir%\*_ORD_*”‘) do (
set Line=%%b
if /i “!Line:~0,13!”==”92095276680067″ set /a counter+=1
)

echo counter = %counter%</pre>

This is a sample of the file structure:

CONTACT Sam W
DATESHIPPED 20110324
DATEORDER 20110228
DUNNS RT 90124264390034
DUNNS MT 92095276680067
DUNNS VT 9115923262SSSS

—————————————————————
Here is one way to evaluate which simplifies the script. Note since the split is on the space character (white spaces are the default), you do not need to specify delims.

<pre>@echo off
set dir=c:\temp
SET /a counter=0
for /f “tokens=3″ %%a in (‘type “%dir%\*_ORD_*”^|findstr /i “MT”‘) do if /i “%%a”==”92095276680067″ set /a counter+=1
ECHO counter = %counter%</pre>

Pjb0222:
I also was able to try your codes using the FINDSTR and happy to report that it worked also. I now have learned another new way of achieving the same result.

Thanks again for your assistance.

Discuss This Question: 11  Replies

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
  • DoneThat
    I'm sure a DOS guru will answer your question directly. But just in case, check out Agent Ransack. This program does what you want in gui AND can be command line driven. Works great. The lite version is free for both personal and commercial use. http://www.mythicsoft.com/page.aspx?type=agentransack&page=home
    830 pointsBadges:
    report
  • carlosdl
    Are you getting errors ? What results are you getting ?
    69,065 pointsBadges:
    report
  • jutpro
    I am not currently receiving any errors, just receiving the incorrect result. It is returning, counter = 0. The correct result should be, counter = 2, there are 2 files in the folder with the string, "92095276680067"
    480 pointsBadges:
    report
  • carlosdl
    How about something like this ?
    @echo off
    setlocal EnableDelayedExpansion
    set dir=c:temp
    SET counter=0
    for /f "tokens=2,3 delims= " %%a in ('find /I "MT" "%dir%*_ORD_*"') do (
    if /i "%%b"=="92095276680067" set /a counter+=1)
    echo counter= %counter%
    69,065 pointsBadges:
    report
  • Pjb0222
    Issue 1: You have to initialize COUNTER as an advanced set arithmetic value to use the arithmatic on the value.
    SET /A COUNTER=0
    Issue 2: You have an unknown pattern from the FIND command for your IF to evaluate. To remove the complexity, don't use the file option of FIND. This removes the filename from the line being part of the information being evaluated. I prefer FINDSTR for this as well.
    ('type "%dir%*_ORD_*"^|findstr /i "MT"')
    There is no need for delayed expansion to evalute the value found by the IF in the "DO" section. One item is that "type" will echo the file name to the console. So long as that is not an issue for you, this should work well. If not you may need two IF statements to pull the file name out of the return from the FIND or FINDSTR to make targeting for the information you desire predictable. I added the script to the answer as well.
    @echo off
    set dir=c:temp
    SET /a counter=0
    for /f "tokens=3" %%a in ('type "%dir%*_ORD_*"^|findstr /i "MT"') do if /i "%%a"=="92095276680067" set /a counter+=1
    ECHO counter = %counter%
    3,310 pointsBadges:
    report
  • jutpro
    Hi Carlosdl, Thanks you so much for your suggestion. I tried your suggestion but it did not work. I did a little more debugging with what I had originally done and found that the syntax is correct the problem was the line position number was off by one. This is what I did wrong; if /i "!Line:~0,13!"=="92095276680067" set /a counter+=1 The above line should be: if /i "!Line:~0,14!"=="92095276680067" set /a counter+=1 I could kick myself when I realize the resolution was so simple. Thanks again you have been a great help.
    480 pointsBadges:
    report
  • carlosdl
    Great Jutpro, thanks for sharing your solution. I don't know if the OS makes a difference, but I tested the code I posted, and it worked for me (on xp) As for Pjb0222's comments, 1) My script initializes counter in the same way as the one posted by Jutpro, and it worked well for me, 2) I used the same pattern as the one posted by Jutpro withe the FIND command, and it worked well for me as well. However, it is always interesting to see different (and maybe better) ways of doing things.
    69,065 pointsBadges:
    report
  • jutpro
    Thank you Pjb0222 for your comments. I do agree with Carlosdl, it is always good to see a different way of doing things. I am greatful for both your inputs. Thanks again.
    480 pointsBadges:
    report
  • jutpro
    Carlosdl: I had some time to spare this morning when I got into work, so I decided to take another look at the codes you gave me. I wanted to let you know that your codes did work fine. The reason they did not work the first time was because I did not notice that you left off the “” between %dir% and *_ORD_* in the statement; for /f “tokens=2,3 delims= ” %%a in (’find /I “MT” “%dir%*_ORD_*”‘) do ( The above statement should be; for /f “tokens=2,3 delims= ” %%a in (’find /I “MT” “%dir%*_ORD_*”‘) do ( Thanks.
    480 pointsBadges:
    report
  • carlosdl
    Thanks for the feedback, Jutpro.
    69,065 pointsBadges:
    report
  • jutpro
    [...] Help with multiple tokens in a DOS batch file [...]
    0 pointsBadges:
    report

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Thanks! We'll email you when relevant content is added and updated.

Following