|
THE ANNOUNCING ISSUE - 20030101 | THE FIRING ISSUE - 20030108 | A TRIP UP THE CLIFFS OF IF - 20030115 | AIRBORNE ON SET RIDGE - 20030122 | DOWN IN THE DETAILS - 20030129 | TREASURES IN THE SAND - 20030205 | A PLAIN PATH THRU FOREST FINDSTR - 20030212
Are
you
Booming
or
Fuming?
THE FIRING ISSUE - 20030108!
We're all FIRING UP before we hit the cold peaks of
Mount Knowledge! All
Windows System Administrators (WSAs) are strongly encouraged to join us!
Let's check in with
Joe Fumer
before we start. As you remember, his backup
script crashed last night and he doesn't know why. What he does know is
that he's got to get it fixed ASAP!
Joe is fuming greatly as he studies the code in his backup script,
trying desperately to see what went wrong! But at this point,
he can't see the Mountain through the fumes!
.............................Are
You One Of The Joe's?! |
In
THE ANNOUNCING ISSUE - 20030101!, we introduced .Mount/\Commands to you.
The vision behind M/\Cs is to replace a complex series of obscure commands
with a single, clearly worded command. Each Mount/\Command is built with a
"ThreeFold Cord of Knowledge". The first one is the "Cord of
Compatibility".
The source of much Fuming for
WSAs is the
seemingly endless array of
"special cases" to remember when trying to make scripts portable among
multiple operating systems. Many solutions and workarounds are of the form
"for NT use this, for 2K/XP use that".
Let's take a look at the M/\C for Operating System detection, ".GetOS".
This command displays the current OS, saves the value to variable
%#os%, and
then sets an errorlevel based on what it found.
To understand how this works, we have to go down into "The Labyrinths of
FOR". Follow closely and stay with the group. It's easy to get "fumingly
lost" in here. You will want to open another window with the
"Guarded Help
Text" for the internal command "FOR"
as you read this
next section.
The "FOR" command is often used to parse (or interpret) the output of
another command. FOR is what we need to answer the common question:
"How do I extract
xxx value(s)
from the output of command(s)
yyy,
and reformat them as
zzz,
so I can use them in my script?"
In this case, the command to be parsed is "VER" and the value(s) to extract
are whatever can uniquely identify the Operating System.
The output of
VER under NT, 2K, XP and K3 (Release Candidate 2) is as
follows:
Windows NT Version 4.0<space><space>
Microsoft Windows 2000 [Version 5.00.2195]
Microsoft Windows XP [Version 5.1.2600]
Microsoft Windows [Version 5.2.3718]
The variation of
FOR used to process this output is:
| FOR
/F "tokens=n delims=d" %%A
IN ('Command1')
DO Command2 |
Let's break it down.
FOR
/F
This is the FOR command with the
/F(ile) switch, which tells FOR to process the "File" between the parentheses, one line
at a time, executing Command2 once for each line found.
The single-quotes ' around
Command1, tell
FOR to process the
-OUTPUT- of Command1 -AS-IF-IT-WERE- already in a file. This
avoids having to save this OUTPUT to a temporary file, and
then reading the temporary file back to execute
Command2.
tokens=n delims=d
The tokens and delims options tell FOR how to interpret each
line of text that it receives. Think of tokens as words and
delims as the characters that separate words.
When you read the sentence below, your mind will interpret it as
having 12 tokens, with <comma>, <period> and <space> as delims:
| My decision is made, I am going to climb up Mount Knowledge. |
To extract ONLY the 2nd token ("decision"),
| FOR
/F "tokens=2 delims=,. " %%A
IN ('ECHO:[Sentence]')
DO
ECHO:%%A |
%%A is replaced with the value of Token 2.
To extract ONLY the 11th and 12th tokens ("Mount"
and "Knowledge"),
| FOR
/F "tokens=11-12 delims=,. " %%A
IN ('ECHO:[Sentence]')
DO
ECHO:%%A %%B |
%%A is replaced with the value of Token 11, %%B with Token 12.
%%A
This is the variable used to hold the FIRST EXTRACTED TOKEN. If
more than one number is specified by "tokens=n", additional
variables are assigned, in alphabetical order, beginning with the
letter you specify. In this case, A=token1, B=token2, etc.
Now, let's look at the output of
VER again and apply what we've learned.
Our objective is to find the most efficient combination of tokens and delims
to uniquely identify the OS.
Using <space> as a delimiter yields five tokens
|
FOR
/F "tokens=1-5 delims= " %%A
IN ('VER')
DO (ECHO:%%A
%%B %%C %%D %%E) |
| Token1 |
Token2 |
Token3 |
Token4 |
Token5 |
| Windows |
NT |
Version |
4.0 |
|
| Microsoft |
Windows |
2000 |
[Version |
5.00.2195] |
| Microsoft |
Windows |
XP |
[Version |
5.1.2600] |
| Microsoft |
Windows |
[Version |
5.2.3718] |
|
Using <period> as a delimiter yields three tokens
| FOR
/F "tokens=1-3 delims=." %%A
IN ('VER')
DO (ECHO:%%A
%%B %%C) |
| Token1 |
Token2 |
Token3 |
| Windows NT Version 4 |
0<space><space> |
|
| Microsoft Windows 2000 [Version 5 |
00 |
2195 |
| Microsoft Windows XP [Version 5 |
1 |
2600 |
| Microsoft Windows [Version 5 |
2 |
3718 |
The <period> gives us a concise and unique string to compare in Token2.
Since this represents the minor version number (NT=v4.0, 2K=v5.00, XP=v5.1,
K3=v5.2), it is unlikely to change. Token3, while also unique and short,
represents the "Build Number" (in 2K/XP/K3), and will change before Windows Server
2003 is shipped.
So now we have successfully navigated the "Labyrinths of FOR" and emerged
with a command to uniquely identify the OS:
|
FOR
/F "tokens=2 delims=." %%A
IN ('VER')
DO
ECHO:%%A |
Having firmly grasped the Cord of Compatibility in our
ThreeFold Cord of
Knowledge, we're ready to go on.
The Cord of Clarity is the second cord, and it is equally as important as
the Cord of Compatibility.
The unique strings that we receive from our
FOR command do not CLEARLY tell
us the OS. If we stop here, we will have to remember yet another series of
"special cases" ("0<space><space>"=NT,
"00"=2K, "1"=XP and "2"=K3).
This will cause minor fuming now (while it's fresh in our minds) and
MAJOR
FUMING later (when we look back at our script 6 months from now, or when
someone else has to modify our code).
To grasp the Cord of Clarity, we need to pitch our tent on the "Cliffs of
IF". Next week, we'll do just that.
Let's see how Joe's doing now.
Joe has been reading intently and he has already lost some of his
fuming and felt a few BOOMs. Especially when we escaped the
"Labyrinths of FOR" with our unique OS command!
He's beginning to see the Mountain through his
fumes.
.GetOS and 50 other .Mount/\Commands are included in the
FREE
Advanced
NT/2K/XP Command Library. This library also
includes the
:FNL (File_Number_Lines) procedure to number your scripts (or
any text file) and the
$FCOUNT function to quickly count the number of lines
in a file.
The
"Almost Free"
Expert NT/2K/XP Command Library includes over
300 M/\Cs
plus dozens of other Resources --AND- an Interactive Mode to help you learn
the power of this mighty tool. The Expert Library is available for
$9 or less (quantity discounts start at 5 or more).
The complete .Mount/\Command Set is also available separately for
$7 or less.
All of our products are completely written using ONLY what is available in
ALL INSTALLATIONS of Windows NT 4 SP6a, Windows 2000, Windows XP and Windows
Server 2003. There is
no binary code, only scripting commands. They are a
perfect fit for those "locked down" machines that are increasingly common in
today's security-conscious world.
They perform CONSISTENTLY across all supported platforms (see the
Compatibility page) and require no installation or
uninstallation.
Be sure to prepare for next week's trip to the "Cliffs of
IF"
by reviewing the "Guarded Help Text" for
IF.
To start receiving
BoomingOrFuming?,
first register, then follow
the instructions on the
contact page.
Space is
limited and time is short, so
reserve your spot today!
We'll take you up
the Mountain of Knowledge, which rises higher than all
the other Hills of Confusion.
|