Are you Booming or Fuming?
A PLAIN PATH THRU FOREST FINDSTR - 20030212!
This week we're going to venture into Forest FindStr, one of the richest (and least understood) areas on Mount Knowledge!
While many have traveled along the borders, we'll be taking you deep into the Regex Woods, in the very heart of the forest!
Come join us!
Let's look at a simple example first:
The Mount/\Command .NowTime displays the current time as hhmnss, works with most regional date formats, and adds a leading "0" to the hour if needed.
Remember the "Threefold Cord of Knowledge", the foundation of all .Mount/\Commands:
CORD of COMPATIBILITY
Here's the command source for .NowTime WITHOUT any embedded Mount/\Commands:
Here it is WITH embedded Mount/\Commands:
Now the breakdown:
Lines 01 and 14 are used to wrap the entire statement in ()'s so it can be cut and pasted as a single entity. This format helps us to compress the code to a .M/\C.
Lines 02 and 03 parse the output of the "TIME" command
The current time is:<space><space>3:54:46.65
and display only the fifth through seventh tokens of lines that contain at least one numeric character ("FindStr/br "[0-9]"). The delimiters "<dot><colon><comma><space>" account for most variations in regional time formats.
When we get to Line 04, %%A is now equal to:
Still in Line 04, we check to make sure that our retrieved time (%%A) begins with TWO CONSECUTIVE NUMBERS (FindStr /b "[0-9][0-9]"). If it does not, as in our example, we know to add a "0" prefix, maintaining the CONSISTENT six-digit output.
Lines 06 and 07 are processed if the hour is two digits (10:00:00 to 23:59:59)
Lines 09 and 10 are processed if the hour is one digit (0:00:00 to 9:59:59)
To save the code as .NowTime,
@SET ".NowTime=@(FOR /F "tokens=5-7 delims=.:, " %%A IN ('ECHO:^|TIME^|FindStr "[0-9]"') DO @((ECHO:%%A|FindStr/br "[0-9][0-9]" 2>NUL>NUL)&&((ECHO:%%A:%%B:%%C)&(VER>NUL))||((ECHO:0%%A:%%B:%%C)&(MD 2>NUL))))"
We'll also need an "escapified" version (.eNowTime) to use inside the ()'s of a FOR loop.
@SET ".eNowTime=@^(FOR /F "tokens=5-7 delims=.:, " %%A IN ^('ECHO:Y^^^|TIME^^^|%.FindStr% [0-9]'^) DO @^(ECHO:%%A^|%.FindStr%/br [0-9][0-9] 2^>NUL^>NUL^&^&^(^(ECHO:%%A:%%B:%%C^)^&^(VER^>NUL^)^)^|^|^(^(ECHO:0%%A:%%B:%%C^)^&^(MD 2^>NUL^)^)^)^)"
Now we'll use this new command (.eNowTime) and one more (.ForAll) to go deeper into the Forest and build an even more powerful command (.TimeEcho).
A quick description of .ForAll:
If you don't yet have .Mount/\Commands or one of our command libraries, you can manually create the .ForAll command like so:
We can now construct .TimeEcho using .eNowTime and .ForAll.
Echoes "[TIME] Text" to STDOUT. Time is hh:mm:ss with
The actual code for .TimeEcho is clear and concise.
Note that we can safely reuse the %%A variable in .TimeEcho even though we also use it in .eNowTime. This in ONLY because the FOR command executes whatever is inside the ()'s in a separate, or child, command processor, returning only the output to the DO section.
We use CALL ECHO rather than ECHO to expand any variables in the optional text.
%.TimeEcho% %computername% Job Started
Save this code as .TimeEcho (the following is all on one line):
SET ".TimeEcho=@%.ForAll% %%A IN ('%.NowTime%') DO @CALL ECHO:[%%A]"
For all of it's power, FindStr is not without
FindStr /r "[A-Z]"
would find only UPPERCASE letters and
FindStr /r "[a-z]"
would find ONLY lowercase.
Well...that thought might send you FUMING someday!
The FindStr that comes with NT is the ONLY VERSION that properly differentiates between UPPER and lower case when using the Regular Expression ranges of [A-Z] or [a-z]! For more details, see the !GuardNotes section on the FindStr Page.
This is one of hundreds, maybe even thousands (we're still discovering new ones), of "Fuming Special Cases" that must be remembered IF YOU DON'T USE .Mount/\Commands and Command Libraries to smooth out the rough places.
Climbing Mount/\Knowledge is about solving a problem ONCE, and then going on to challenge NEW PROBLEMS!. The Fuming Foothills are about re-working the same problems over(!) and Over(!!) and OVER(!!!).
We're sure glad that we don't have to write our scripts in binary or assembly language. The reason we don't is not because we can't, but rather because THERE ARE BETTER TOOLS AVAILABLE NOW!. Windows NT/2K/XP/K3 provide a powerful, but INCONSISTENT, set of internal commands and external utilities for shell scripting.
MountCommands extend the Common Command Set with OVER 200 additional commands, all of which PERFORM CONSISTENTLY across NT/2K/XP/K3. Each one provides COMPATIBILITY, CLARITY and SPEED! And you can own them for less than it would cost you for ONE FUMING HOUR of "re-inventing the wheel".
You can try out .Mount/\Commands for FREE by requesting the Advanced NT/2K/XP Command Library. This library has over 155 fully functional .M/\Cs including the .TimeEcho command described above.
The "Almost Free" Expert NT/2K/XP Command Library includes over 200 M/\Cs plus dozens of other Resources including expandable !GuardNotes.
The latest .Mount/\Command Set (Release 2003.02.04) contains over 200 commands in a highly compressed script (less than 25K) starting at $7 or less with quantity discounts beginning at 5 or more.
All of our products are constructed using ONLY what is available in ALL INSTALLATIONS of Windows NT4 SP6a+, Windows 2000, Windows XP and Windows Server 2003.
Each Command Set or Library is COMPLETELY SELF CONTAINED, and requires nothing that you don't already have! See the Compatibility page for details.
Space is limited and time is short, so reserve your spot today!