Re: Parsing file using FOR command not working asDave expects

From: Matthias Tacke (Matthias.Tacke_at_web.de)
Date: 07/12/04


Date: Mon, 12 Jul 2004 18:27:44 +0200

Dave Pink wrote:

>I'm using PsLogList from www.sysinternals.com to dump the contents of
>an Event Log. The output is saved to the file hotfix.txt.
>
>Contents of hotfix.txt:
>
>[3166] NtServicePack
>Type: INFORMATION
>Computer: PRDW2K-WEB109
>Time: 6/27/2004 2:02:37 AM ID: 4377
>Windows 2000 Hotfix KB837001 was installed.
>
>
>Using the FOR command, I'm trying to extract just the Event Log
>number; in this case it's 3166. I can't get it to work right.
>
>
>Here's my batch file:
>
>FOR /F "tokens=1 delims=[]" %%a in (hotfix.txt) DO SET Number=%%a
>@ECHO %Number%
>
<snip>
>How can I set the variable "Number" equal to "3166"? Any help would be
>appreciated. Thanks.
>
>Dave

The for command iterates through all lines, a leading delimiter is
ignored, multiple adjacent delimiters are reduced to one.
So the output is as expected (by me).

The regexp in this findstr will output only lines with a number
surrounded by square brackets. In a regexp square brackets mark a range,
the brackets to search for have to be escaped with a backslash, the
asterisk is for multiple occurences of the range 0 to 9 giving a valid
number.

Only lines according to that are passed to the for.

@echo off
FOR /F "tokens=1 delims=[]" %%a in (
'findstr "\[[0-9]*\]" ^<hotfix.txt') DO SET Number=%%a
echo %%a

-- 
Greetings
Matthias________________________________________
For help on nt commands enter in a cmd window:
W2K>HH windows.chm::ntcmds.htm  XP>HH ntcmds.chm


Relevant Pages

  • Re: Parsing file using FOR command not working correctly
    ... > Using the FOR command, I'm trying to extract just the Event Log ... ECHO %Num% ...
    (microsoft.public.win2000.cmdprompt.admin)
  • Re: Persisting env vars in cmd windows
    ... more about batch files as you go on, ... echo The time is %time::=.% ... But the piece de resistance was the "start" command, ... echo:: ERROR ERROR ERROR ERROR ...
    (microsoft.public.win2000.general)
  • Re: Persisting env vars in cmd windows
    ... But the piece de resistance was the "start" command, ... - Have all code for the one project in one single batch file. ... @echo off ... echo:: ERROR ERROR ERROR ERROR ...
    (microsoft.public.win2000.general)
  • Re: Persisting env vars in cmd windows
    ... even though it applies to any command, e.g. echo, copy, ... Similarly the syntax for the extremely powerful "for" command ... more about batch files as you go on, ... echo:: ERROR ERROR ERROR ERROR ...
    (microsoft.public.win2000.general)
  • Re: Persisting env vars in cmd windows
    ... even though it applies to any command, e.g. echo, copy, ... Similarly the syntax for the extremely powerful "for" command ... more about batch files as you go on, ... echo:: ERROR ERROR ERROR ERROR ...
    (microsoft.public.win2000.general)

Loading