Scripting Language Basics

The TCC-RT command processor is highly upwardly compatible with (and a replacement for) the default Windows command processor CMD.EXE. It is suitable for creating both simple and highly sophisticated batch programs. The language can also be used at the command prompt to create very powerful real-time manipulation of your computer.

 

Overview

 

TCC-RT has a huge set of capabilities. These capabilities are grouped into three categories:

 

Internal Commands - These are the primary language constructs. Common commands include, DIR, COPY, MOVE, etc. TCC-RT gives you instant access to more than 230 internal commands. (Microsoft's CMD.EXE has fewer than 40 internal commands.).

 

Internal vs. External Commands

 

When we talk about an internal command, we mean the command is built into the Take Command program. With CMD, some commands, like XCOPY are actually separate programs. In PowerShell, the commands are generally external programs. PowerShell requires a separate .NET Framework to be installed on the computer for the commands to work.

 

Internal Variable - Internal variables are special variables built into TCC-RT to provide information about your system. They are not stored in the environment, but can be accessed as if they were environment variables in interactive commands, aliases, and batch files. Take Command provides more than 270 internal variables that can tell you a great deal about your computer and how it is operating. These include installed hardware, hardware status, operating system and software status, etc.

 

Variable Functions - Variable functions are very similar to internal variables, but they take one or more parameters (which can be environment variables or even other variable functions). Variable functions are useful at the command prompt as well as in aliases and batch files to check on available system resources, manipulate strings and numbers, and work with files and filenames. There are more than 360 variable functions built into TCC-RT.

 

We are not going to talk about all of the features of the TCC-RT Language in this tutorial. (The manual is 1,300 pages long!) We are going to assume you know the basics of CMD and point you at a few of things that TCC-RT does better with less work than CMD.

 

Internal Commands

 

There are several aspects of TCC-RT’s internal command set that are definitely worth looking at:

 

Switches

Flow of Control Commands

KEYSTACK Command

HTTP and FTP

Event Monitoring Commands (Triggers) -- We made this into a separate tutorial

 

Each of these is covered below:

 

1. Switches

 

Switches modify commands by giving them special instructions. TCC-RT has a superset of the CMD switches and is generally compatible. We say generally, because unfortunately, CMD has not been consistent from version to version.

For example, in CMD, the COPY command has 7 switches (XCOPY has more). The TCC-RT COPY command has 34 switches. Examples of a few of the switches that the CMD COPY command does not have include:

 

/N Executes the copy command and shows you what the output would be, but does not actually execute the command

/O Copy the source file only if the target does not exist

/S Copy the subdirectory tree starting with the files in the source directory plus each subdirectory

/H Copy all matching files including those with a hidden or system attribute set

/W Delete files in the target directory that don’t exist in the source directory

 

These switches allow you to custom tailor the language in ways that you cannot do with CMD. They perform very powerful operations with only two or three keystrokes.

 

 

2. Flow of Control Commands

 

One of the weakest areas of CMD is flow of control. These are the constructs like IF..THEN..ELSE or DO LOOPS that allow you to develop sophisticated batch programs. If you are creating data center batch processes, the limitations in CMD keep you from doing anything sophisticated.

 

TCC-RT provides a very rich set of constructs that allow you to duplicate (or exceed!) the capabilities of the typical Linux shells.

 

The following examples shows some of the types of DO Loops you can create:

 

Do Loops

 

DO count

DO FOREVER

DO varname = start TO end [BY step]

DO WHILE condition

DO UNTIL condition

DO UNTIL DATETIME date time

DO FOR n [SECONDS | MINUTES | HOURS]

DO varname IN [range...] [/I:"text" /S[n] /A:[-|+]hsad] fileset

DO varname IN [/T"delimiters"] /L stringset

DO varname IN /C stringset

DO varname in /P command

DO varname IN @file

 

TCC-RT also provides a very powerful IF..THEN..ELSE construct through the IFF command.

 

If...Then...Else Constructs

 

IFF condition1 THEN

commandset1

[ELSEIFF condition2 THEN commandset2 ]

...

[ELSE

commandset3 ]

ENDIFF

 

The alias in this IFF example checks to see if the parameter is a subdirectory. If so, the alias deletes the subdirectory's files and removes it (enter this on one line):

 

alias prune `iff isdir %1 then & del /s /x /z %1 & else & echo %1 is not a directory! & endiff`

 

This example shows how a SWITCH construct works. The batch file fragment below displays one message if the user presses A, another if the user presses B or C, and a third one if the user presses any other key:

 

Switch Constructs

 

inkey Enter a keystroke: %%key

switch %key

case A

echo It's an A

case B .or. C

echo It's either B or C

default

echo It's none of A, B, or C

 

3 .KEYSTACK

 

KEYSTACK takes a series of keystrokes and feeds them to a program or command as if they were typed at the keyboard. (It has no equivalent in CMD.) KEYSTACK is most often used for programs started from batch files. For example, to start Word and open the last document you worked on, you could use the command :

 

start word & keystack /w54 alt-f "1"

 

This causes the following:

 

Starts Word,

The /w switch causes a delay of about three seconds (54 clock ticks at about 1/18 second each) for Word to get started,

Places the keystrokes for alt-F (File pulldown menu), and 1 (open the most recently used file) into the buffer.

 

Word receives these keystrokes and performs the appropriate actions. Notice that the two commands, START and KEYSTACK are issued on a single command line. This ensures that the keystrokes are sent to Word's window, not back to Take Command.

 

4. FTP and HTTP

 

TCC-RT’s FTP and HTTP commands allow you to treat http and ftp sites as if they were local disk drives. This is a huge advantage over CMD. In Working in the Internet World, we show you how to use these commands to create practical remote monitoring applications.

 

In simplest form, you can act as if an FTP or HTTP site is a local disk. For example, to get a directory of the JP Software FTP site, you could use this command:

 

Dir ftp://ftp.jpsoft.com/*

 

The following example shows how to include an ftp user name and password:

 

Dir ftp://username:[email protected]/mydir/*

 

You can reference internet sites for DIR, COPY, MOVE, DEL and other commands. These commands also work with secure versions of FTP and HTTP.

 

5. Event Monitoring Commands (Triggers)

 

One of the most powerful features in TCC-RT are the event monitoring commands. They allow you to watch a wide variety of activities on your computer and “trigger” processes into action to deal with or report on issues.

 

This is described fully in Using Triggers in Take Command. It’s well worth the read.

 

Internal Variables

 

Internal variables are special variables built into TCC-RT to provide information about your system. They are not stored in the environment, but can be accessed as if they were environment variables in interactive commands, aliases, and batch files.

 

There are more than 280 of them (CMD has less than 10). Key types of variables include:

 

Hardware status

Operating system and software status

Dates and times

Drives and directories

Error codes

Screen, color, and cursor

Take Command status

Compatibility

 

Here is a simple example of how to use a common variable called _DOW (Day Of Week):

 

if "%_DOW" == "Mon" call c:\cleanup\weekly.bat

 

This example calls another batch file if today is Monday.

 

Before we go on...

 

A Quick Note:

 

One of the great mysteries of the command line is the % sign. What does it do? When you see a % sign in front of a variable or function, it means that the parser should evaluate the function and replace the variable or function with its text value. So, in the last example, %_DOW is replaced with the result, which in this case is MON, TUE or whatever.

 

How about something more real-time that you can run in the background:

 

DO FOREVER

if "%_BATTERYPERCENT" LT 25" MSGBOX Battery is low

ENDDO

 

This command will loop forever checking the battery status and popup a message box if the battery charge is getting low. MSGBOX is actually a very powerful command in TCC-RT. Check it out in the help file.

Here is an example that checks to see if there are enough resources free before running an application.

 

iff %_GDIFREE lt 40 then

echo Not enough GDI resources!

quit else

d:\mydir\myapp

endiff

 

Take a look at the list of internal variables by category in the help file.

 

Variable Functions

 

Variable functions are one of the most powerful features of TCC-RT. Variable functions are very similar to internal variables, but they take one or more parameters (which can be environment variables or even other variable functions).

 

Variable functions are useful at the command prompt as well as in aliases and batch files to check on available system resources, manipulate strings and numbers, and work with files and filenames.

 

There are more than 380 Variable Functions grouped into 13 categories. They allow you to gather and manipulate system information in very powerful ways. (CMD has no variable functions.). Remember...they are all built-in.

 

Binary buffers

Dates and times

Drives and devices

File content

File names

File properties

Input dialog boxes

Monitoring

Network properties

Numbers and arithmetic

Strings and characters

System status

Utility

 

Using functions, TCC-RT can read and write text files, as well as some specialty files, such as the Windows Registry or .ini files. In the example below, we are going to read a .csv file called names.csv (which is a text file with fields separated by commas). The file looks as follows:

 

Joe,100,[email protected]

Jane,200,[email protected]

Peter,400,[email protected]

 

Our example will read this file a line at a time, and select the email address in each line. It will then echo them to the console.

 

set filename=%@expand[names*.csv]

do record in @%filename

set email=%@field[",",3,%record] echo %email

enddo

 

This code does the following:

 

The first line of the example creates a variable with the full file and pathname of the .csv file using the @expand function.

The second line uses a special case of the DO command to:

Open the filename we set in the first line with an @filename function

Create a line counter that it sets to one

Set up a new variable called “record”

Read the first line of text up to the CR and returns it to “record”

The @field function picks the third field in the line (which contains the email address) using a “,” as the field delimiter and places it in a variable called “email”. The delimiter could be anything you wanted.

The ECHO command outputs the email address to the console

ENDDO returns the loop to the do statement, which increments the line counter to the next line. If it’s the end of the file, it terminates the loop.

 

This particular example seems sort of limited, but we use a variant of it to process our orders, construct registration keys and email them to our users with a remarkably small amount of code.