Parsing file using FOR command not working correctly

D

Dave Pink

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%


Here's the output of my batch file:

C:\>FOR /F "tokens=1 delims=[]" %%a in (hotfix.txt) DO SET Number=%%a

C:\>set Number=3166

C:\>set Number= Type: INFORMATION

C:\>set Number= Computer: PRDW2K-WEB109

C:\>set Number= Time: 6/27/2004 2:02:37 AM ID: 4377

C:\>set Number=Windows 2000 Hotfix KB837001 was installed.

Windows 2000 Hotfix KB837001 was installed.


How can I set the variable "Number" equal to "3166"? Any help would be
appreciated. Thanks.

Dave
 
M

Matthias Tacke

Dave said:
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%
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
 
J

Jerold Schulman

@echo off
setlocal
for /f "Tokens=1* Delims=[]" %%a in ('type hotfix.txt^|findstr /i /L
/c:"NtServicePack"') do (
@echo %%b
)


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%


Here's the output of my batch file:

C:\>FOR /F "tokens=1 delims=[]" %%a in (hotfix.txt) DO SET Number=%%a

C:\>set Number=3166

C:\>set Number= Type: INFORMATION

C:\>set Number= Computer: PRDW2K-WEB109

C:\>set Number= Time: 6/27/2004 2:02:37 AM ID: 4377

C:\>set Number=Windows 2000 Hotfix KB837001 was installed.

Windows 2000 Hotfix KB837001 was installed.


How can I set the variable "Number" equal to "3166"? Any help would be
appreciated. Thanks.

Dave


Jerold Schulman
Windows: General MVP
JSI, Inc.
http://www.jsiinc.com
 
F

foxidrive

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.


How can I set the variable "Number" equal to "3166"? Any help would be
appreciated. Thanks.

@echo off
FOR /F "tokens=1 delims=[] " %%a in (hotfix.txt) DO SET Num=%%a&goto skip
:skip
ECHO %Num%
 
G

guard

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%


Here's the output of my batch file:

C:\>FOR /F "tokens=1 delims=[]" %%a in (hotfix.txt) DO SET Number=%%a

C:\>set Number=3166

C:\>set Number= Type: INFORMATION

C:\>set Number= Computer: PRDW2K-WEB109

C:\>set Number= Time: 6/27/2004 2:02:37 AM ID: 4377

C:\>set Number=Windows 2000 Hotfix KB837001 was installed.

Windows 2000 Hotfix KB837001 was installed.


How can I set the variable "Number" equal to "3166"? Any help would be
appreciated. Thanks.

Dave

An alternative approach (parsed as one line of code by cmd.exe):

@(
(SET Number=)
(
FOR /F "tokens=1 delims=[]" %%a IN (hotfix.txt) DO @(
IF NOT DEFINED Number (
(SET "Number=%%a")
(ECHO:%%a)
)
)
)
)


*******

You could also use the :System_Info procedure from the Expert Command
Library.

%.Call% :SI | %.Silent% %.Find% /i "KB837001" && ECHO:KB837001 was found

*******

Expert NT/2K/XP/K3 Command Library
(http://NTCmdLib.com)

:System_Info Procedure (see Example 1)
(http://TheSystemGuard.com/Procedures/SI.htm)

Overview of the Command Library Architecture
(http://TheSystemGuard.com/NTCmdlib.asp#LibraryArchitecture)

*******

-tsg

/-----------------+---------------+----------------------\
| COMPATIBILITY | CLARITY | SPEED |
| Write code ONCE | Make it clear | THEN...Make it fast! |
\-----------------+---------------+----------------------/
400+ command-line resources using ONLY native NT commands!
(http://TheSystemGuard.com/default.asp#MasterCommandList)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top