Useful tools for scripts

One of my favorites are from SST (Shell Scripting Toolkit) from Bill Stewart (http://www.westmesatech.com/), but I dont understand why they are not really known in community.

So I decided to post a little bit about them, maybe someone could be interested.

Toolkit contains following utilities:

CCase.exe
ColorX.exe
DateX.exe
DriveX.exe
EchoX.exe
FInfo.exe
IfX.exe
LineX.exe
S2V.EXE
ShellEsc.exe
SleepX.exe
Str.exe
Tee.exe
TempName.exe

CCase

Change Case - allows you to change case using pipe, pretty simple and sometimes can be VERY useful. Just pipe output through ccase you you get results you want:

C:\Temp\BillStewart>echo Testing cHAnGe Case | ccase -l
testing change case

C:\Temp\BillStewart>echo Testing cHAnGe Case | ccase -u
TESTING CHANGE CASE

ColorX

Do you want to use custom color scheme in your scripts? Or manipulate with it? ColorX allows you to detect current settings or change them.
If you use -l switch, ColorX generates really nice overview of available colors, so you can quickly choose your combination. For example I like to have specific color of output to stress some facts (for example Success is light green, failure is red and important messages are orange), so you immediately see if there is anything wrong. Problem is that many people change default color, for example using red foreground (so all errors and warnings got lost).

I can build simple workaround using ColorX:

:: Store original settings
For /f "UseBackQ tokens=*" %%c IN (`ColorX`) Do Set Settings.OriginalColor=%%c

:: Change to MY settings (white frontground, black background)
ColorX -c 0F

:: Do something....

::Restore original settings
ColorX -c %Settings.OriginalColor%

DateX

One of the biggest pains when handling with batches are date\time manipulation. Problem is that they are dependant on your regional settings and if you need to manipulate with multiple countries, scripts are getting VERY complex. Not to mention problems if you want to increase or decrease values - my script for getting (universal) file creation time is 90 lines long. You will also probably (sooner or later) discover some cave-eats of batches - for example batch Set /a behavior. For example you want to store date one month ago in variable. It starts pretty simply - you just parse %Date% variable or use Date/t.

Today is Thu 03/27/2008 - and you would like to run something like Set /a 03 - 1 (March - one month) and then have some check like If %Result% LSS 1 set /a %Result% + 12 (that is probably easiest method how to handle with months). Still not see problem? If you would implement it today, you script will work for 5 months - and 1st of August it will stop working. Because we see "03", Set /a automatically consider number is octet - and 08 doesnt exist in octat, so you will receive error:
Invalid number.  Numeric constants are either decimal (17),
hexadecimal (0x11), or octal (021).

Well, to make long story short, just ignore batch functionality and use DateX instead. I am using DateX for two purposes - first one is for time comparison (here is it extremely useful) and also for predefining format I expect.

Example of time comparison:

For /f "usebackq tokens=*" %%x IN (`DateX -t`) Do Set Temp.Start=%%x

::Do something

For /f "usebackq tokens=*" %%x IN (`DateX -t`) Do Set Temp.End=%%x

Set /a Temp.Duration=%Temp.End% - %Temp.Start%

Echo Script was running for %Temp.Duration% seconds

Of course changing it to minutes\hours etc is not problem at all.

DriveX

Allows you to manipulate with drive and retrieve type of drive. Only problem I encountered (but there is probably nothing that can be done to fix this) is that it is not able to detect substed drives (mapped using Subst). Returned errorlevel identifies drive type - so 3 is fixed, 5 is CD-ROM etc..

It is most usefull if you want to process particular drive types. For example unmap all network drive command is pretty simple using DriveX:
For /f "UseBackQ Tokens=*" %%d IN (`DriveX -l 4`) Do Net Use %%d: /Delete

EchoX.exe

EchoX is probably my favorite utility and I use it with most of my scripts. What it allows you to do? Its pretty simple, you can change color of output (not WHOLE console window, only one line). Also I really like usage of escape characters that allows you to include line feed and return commands in one line.

Consider scenario where something goes reaaaaally wrong - and you want to notify user. You want to display following message:

:: Do something

WARNING: Problem was detected when using script X.
WARNING: Script will now abort.
WARNING: Please contact your administrator

:: Do some recovery

Using  batches, you must write following "code":

:: Do something

Echo.
Echo WARNING: Problem was detected when using script X.
Echo WARNING: Script will now abort.
Echo WARNING: Please contact your administrator
Echo.

:: Do some recovery

Which is not bad, problem is that same color is used and it is really easy to miss such important information. With EchoX you use

EchoX -c 0C "~nWARNING:Problem was detected when using script X.~nWARNING: Script will now abort.~nWARNING: Please contact your administrator~n"

Of course you can split it to three lines. What is important is that this message is obviously error. It is really nice if you use it together with If Else:

If /i #%Result%# EQU #OK# (
EchoX -c 0A "Operation finished successfully"
) Else (
EchoX -c 0C "WARNING: Something wrong happened"
)

That way you can change your batch script to appear more like utilities. Highly recommended, when I was searching for something like this I found SST. You can find (very few) tools that do the same, but all I tested had problems with buffer (text was truncated, sometimes corrupted etc)

FInfo.exe

FInfo allows you to dump some basic information about files. You specify file and you can get path only, file name etc. This functionality is not that important - you can achieve the same by implementing %~ (if you are interested, leave some comment and I can write how to use it).

What is more interesting is option to get last modification time in same format as DateX (universal, no parsing needed).
Also option to retrieve file size is really nice - output is obviously made for parsing (BTW Bill, if you read it, I cant stress enough how I appreciate parseable output ;)).

IfX.exe

Not really useful today, because built-in If contains same functionality. Obviously it was build for 9x OSs, so dont expect anything really fancy here. It would be nice if it would contain features like IfX Starts With, IfX Ends With, IfX Contains etc...

LineX.exe

Allows you to handle with output, generally Windows tails\head tools. Nice feature is that you can retrieve line n (so if your second line is always ::Created by, you can easily retrieve this). Usually you need to write some For /f with counter that tells you when to stop parsing - which is quite time consuming and not really reliable, so LineX can really help.

S2V.EXE

MS-DOS utility to store output in variable. Replaced in NT by For /f, I never had chance to try it.

ShellEsc.exe

Could be really nice :( You can shellescaped any text - which is really great if you automate automation like I do ;) Problem is that ScellEsc was never really working for me - when it encountered empty line, it always crashed with message

Runtime error 216 at $0040107F
  $0040107F
  $0006FFA8
  $FFFFFFFF

I completely forgot about this tool and I will try to write Bill if he still supports SST and if he could fix it - because I like to organize my code to make it easily readable.

SleepX.exe

Nice replacement of original Sleep. While original sleep allows you to specify time only, SleepX allows you to display message also and add option (and this is why I like it) to abort sleep by any key. One feature I am missing is that I would like SleepX to sleep forever if no timeout is specified and option to abort was enabled. I know I could use Sleep & Pause combination, but the idea is that you could allow people to define timeouts for different messages like this and use one code for all configurations.

Str.exe

Str allows in general two operations - first one is to change to uppercase\lowercase  and echo number of characters in string. For upper\lower case I prefer to use CCase, because it accepts pipe, but number of characters is sometimes very useful (especially if you want to do something where limited characters are allows). Second functionality is to detect string inside string and return starting position. I prefer using Str to handling with Find (you can detect if string exists by checking errorlevel), also because it allows me to easily detect if string STARTS with different string (which is sometimes quite important).

Tee.exe

Tee allows you to split output - so you can create log file that contains everything that is displayed to console. I like this concept, but prefer MTee to Tee because it contains more functionality.

TempName.exe

This tool could be quite useful, but I am still using script to do this:

:GenerateCacheFolder
Set Temp.ID=%Random%
If Exist "%Folder.Root%\Cache\%Temp.ID%" Goto GenerateCacheFolder
Set Temp.Cache=%FolderRoot%\Cache\%Temp.ID%
MkDir "%Temp.Cache%"

 

Published Thu, Mar 27 2008 11:09 by martin
Filed under: ,

Comments

# re: Useful tools for scripts

Hi Martin, thank you for this blog post. I think that ShellEsc is fixed in v2.6. Regards, Bill

Monday, June 08, 2009 8:50 PM by Bill Stewart