FOR command question

D

djc

I have a batch file that appends to a log file. The file has multiple fields
and uses a ',' as a delimiter. The first 2 fields are %date% and %time%. My
windows 2000 and XP machines fill in these fields fine however my NT4
machines do not. So for all lines from an NT4 machine the first two fields
are blank like so:

, , computername, operating system, other stuff etc..

I use the 'find' and 'for' commands on this log file a lot. However the
results when using FOR get messed up due to the NT4 machines leaving blanks.
Say I want to display only computername field. The token would be 3, which
works fine except for the NT4 lines which return the operating system in
example above.

questions:
1) is there a date and time variable that will work for both NT4 and
2000/XP?

2) Is there a way to tell the FOR command to somehow count the first , as 1
so my tokens don't get off? Or do I need to add a space before the first
comma on every log entry? that seems sloppy.

Any input is appreciated. Thanks.
 
D

David Candy

Don't use %date% but create a variable

FOR /F "usebackq tokens=5" %i IN (`echo. ^|time`) DO set adate=%i

puts the time in a variable called adate. However I'm unsure if the above is supported by NT4. In which case go via a temp file.
 
W

wadester

I have a batch file that appends to a log file. The file has multiple fields
and uses a ',' as a delimiter. The first 2 fields are %date% and %time%. My
windows 2000 and XP machines fill in these fields fine however my NT4
machines do not. So for all lines from an NT4 machine the first two fields
are blank like so:

, , computername, operating system, other stuff etc..

I use the 'find' and 'for' commands on this log file a lot. However the
results when using FOR get messed up due to the NT4 machines leaving blanks.
Say I want to display only computername field. The token would be 3, which
works fine except for the NT4 lines which return the operating system in
example above.

questions:
1) is there a date and time variable that will work for both NT4 and
2000/XP?

What about

for /f "tokens=*" %I in ('date /t') do set newdate=%I
for /f "tokens=*" %I in ('time /t') do set newtime=%I
2) Is there a way to tell the FOR command to somehow count the first , as 1
so my tokens don't get off? Or do I need to add a space before the first
comma on every log entry? that seems sloppy.

Implmenting item #1 should alleviate having to do this.

ws
 
M

Matthias Tacke

djc said:
I have a batch file that appends to a log file. The file has multiple fields
and uses a ',' as a delimiter. The first 2 fields are %date% and %time%. My
windows 2000 and XP machines fill in these fields fine however my NT4
machines do not. So for all lines from an NT4 machine the first two fields
are blank like so:

, , computername, operating system, other stuff etc..

I use the 'find' and 'for' commands on this log file a lot. However the
results when using FOR get messed up due to the NT4 machines leaving blanks.
Say I want to display only computername field. The token would be 3, which
works fine except for the NT4 lines which return the operating system in
example above.

questions:
1) is there a date and time variable that will work for both NT4 and
2000/XP?

2) Is there a way to tell the FOR command to somehow count the first , as 1
so my tokens don't get off? Or do I need to add a space before the first
comma on every log entry? that seems sloppy.

Any input is appreciated. Thanks.
See GetDate and GetTime from Ritchie Lawrence's batch library at
http://www.commandline.co.uk/ provided you have all english locales
they work also with nt4.

Quote your entry's, the for is then able to distinguish your empty
fields. To dequote use a tilde when using the vars %%~A for example.

HTH
 
D

David Candy

I just looked at the Win 95 port of NT4's cmd.exe to see if /t was supported ny NT4. Last I looked there was a W2k and a NT4 version available - maybe an XP one by now - somewhere in the platform SDK. It's help claims to have the %date% variable (although it might be picking up XP's help as I can't find any differences - ver says Windows NT ver 5.1 which is correct (for the OS) but isn't the terminology that W2K and XP use).

Though I monitored it's file access for help commands and their were none. So date and time musr be in NT4. Look under the set /? page.
--
----------------------------------------------------------
http://www.g2mil.com/Dec2003.htm
Don't use %date% but create a variable

FOR /F "usebackq tokens=5" %i IN (`echo. ^|time`) DO set adate=%i

puts the time in a variable called adate. However I'm unsure if the above is supported by NT4. In which case go via a temp file.
 
G

guard

I have a batch file that appends to a log file. The file has multiple fields
and uses a ',' as a delimiter. The first 2 fields are %date% and %time%. My
windows 2000 and XP machines fill in these fields fine however my NT4
machines do not. So for all lines from an NT4 machine the first two fields
are blank like so:

, , computername, operating system, other stuff etc..

I use the 'find' and 'for' commands on this log file a lot. However the
results when using FOR get messed up due to the NT4 machines leaving blanks.
Say I want to display only computername field. The token would be 3, which
works fine except for the NT4 lines which return the operating system in
example above.

questions:
1) is there a date and time variable that will work for both NT4 and
2000/XP?

2) Is there a way to tell the FOR command to somehow count the first , as 1
so my tokens don't get off? Or do I need to add a space before the first
comma on every log entry? that seems sloppy.

Any input is appreciated. Thanks.

Try using GetLogDate and GetLogTime from the Advanced Command Library at
ntlib.com (free for personal or commercial use). These commands work
CONSISTENTLY across NT4, 2000, XP and Server 2003.

(http://TheSystemGuard.com/MtCmds/GetValue/GetLogDate.htm)
(http://TheSystemGuard.com/MtCmds/GetValue/GetLogTime.htm)

-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