----^--->
/\

//\\
//The\
/Sy
stem\
////
Guard
\

In the mount of the LORD it shall be seen. Genesis 22:14

A PLAIN PATH THRU FOREST FINDSTR - 20030212

Booming Or Fuming? | MasterCatalog | ntlib (FREE) | NTCmdLib | MtCmds | Scripts | Almost Free | TheGuardBook | Help

 

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?

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!


Joe Fumer is wary as we enter the forest.  He's never been past this first line of trees and hedges at the entrance.  Searching for strings is one thing, but all that "RegEx" stuff makes Joe dizzy.

Forest Findstr IS a fearful place for any Fumer to be without a Knowledgeable  Guide.  But the secret is to stay on the "Plain Path".

We've been here many times before, Joe!  And...WE KNOW THE WAY!  Stay close and you'll learn to harness the power of these trees to do things that you never thought possible!


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
    -Works CONSISTENTLY across NT/2K/XP/K3.
    -Output is CONSISTENTLY six digits (hhmmss).

CORD of CLARITY

    -The entire code segment is invoked as a one-word
     mnemonically named command.
    -Scripts written using these Mounted commands
     are FAR EASIER TO MAINTAIN than The Old Way.

CORD of SPEED

    -Calls to External Commands (Findstr) are replaced
     with the Mounted Version (%.FindStr%), which
     expands to the fully qualified drive, path, filename
     and extension (i.e., c:\winnt\system32\findstr.exe).
    -The code is pre-loaded into the local environment,
     allowing the command to behave and perform like
     an "internal command".
   
 NO DISK ACCESS IS NEEDED TO LOAD THE CODE!

Here's the command source for .NowTime WITHOUT any embedded Mount/\Commands:

01. @(
02.    (FOR /F "tokens=5-7 delims=.:, " %%A IN (
03.       'ECHO:^|TIME^|FindStr/r "[0-9]"') DO @(
04.          (ECHO:%%A|FindStr/br "[0-9][0-9]" 2>NUL>NUL
05.             )&&(
06.                (ECHO:%%A:%%B:%%C)
07.                (VER>NUL)
08.             )||(
09.                (ECHO:0%%A:%%B:%%C)
10.                (MD 2>NUL)
11.          )
12.       )
13.    )
14. )
 

Here it is WITH embedded Mount/\Commands:

01. @(
02.    (FOR /F "tokens=5-7 delims=.:, " %%A IN (
03.       'ECHO:^|TIME^|
%.FindStr%/r "[0-9]"') DO @(
04.          (ECHO:%%A|
%.FindStr%/br "[0-9][0-9]" %.Silent%
05.             )&&(
06.                (ECHO:%%A:%%B:%%C)
07.                (
%.SetEL0%)
08.             )||(
09.                (ECHO:0%%A:%%B:%%C)
10.                (
%.SetEL1%)
11.          )
12.       )
13.    )
14. )


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
   Enter the new time:<space>

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:

   3:54:46

To find out if the hours portion contains two columns or not, we again use FindStr.

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:

Uses the power of the internal command FOR to process
the ENTIRE OUTPUT of whatever command[s] are
between the ()'s.  Of course, we "could" use

    FOR /F "tokens=* delims="

but that leaves us wide open for Fumer's Disease.

.ForAll does what it says, and provides CLARITY.

If you don't yet have .Mount/\Commands or one of our command libraries, you can manually create the .ForAll command like so:

@SET ".ForAll=FOR /F "tokens=* delims=""

We can now construct .TimeEcho using .eNowTime and .ForAll.


.TimeEcho

    Echoes "[TIME] Text" to STDOUT.  Time is hh:mm:ss with
    leading 0 added to hours less than 10.  Text is optional.

The actual code for .TimeEcho is clear and concise.

@%.ForAll% %%A IN ('%.eNowTime%') DO @CALL ECHO:[%%A]

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]"


NT
2K
XP
K3
C:\GuardPost>%.TimeEcho% My optional time stamped message here
[03:54:46] My optional time stamped message here

For all of it's power, FindStr is not without many pitfalls.
One example is the processing of alpha characters.  You would think that

    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!


X-------strings.txt-------X
ABC
abc
123
X-------strings.txt-------X

NT C:\GuardPost>FindStr /r "[A-Z]" strings.txt
ABC
2K C:\GuardPost>FindStr /r "[A-Z]" strings.txt
ABC
abc
XP C:\GuardPost>FindStr /r "[A-Z]" strings.txt
ABC
abc
K3 C:\GuardPost>FindStr /r "[A-Z]" strings.txt
ABC
abc

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. 


Writing scripts is NOT our hobby, it's our BUSINESS
.

Writing RELIABLE CROSS-PLATFORM scripts that can be
maintained over time...
is GOOD BUSINESS.

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".


"Well, I once, was BLIND but now I SEE!", says Joe.

"This CRYSTAL KNOWLEDGE and the PLAIN PATH has made
 Forest FindStr 'Crystal Clear' for me!"


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.

There is NO BINARY CODE, ONLY SCRIPTING COMMANDS!!!

Each Command Set or Library is COMPLETELY SELF CONTAINED, and requires nothing that you don't already have!   See the  Compatibility page for details.


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.

/\
//\\
//
The\
/Sy
stem\
////
Guard
\
COME ON UP WITH US!
and
Congratulations On Your Decision
to
RISE ABOVE
THE REST!

^
/
!\
/LIB\

TheSystemGuard.com [Revised: 2007-03-29]
Copyright 1995-2007, JWC
Computer Communications,
All Rights Reserved, Worldwide.
 Disclaimer       Privacy       About Us       More


Communication Is Business!

We Master It With Knowledge!

^
/
.\
/M^C\