UTILITIES WINCMD: A Windows Command Language

BY DOUGLAS BOLING

emember the DOS batch USING WINCMD Before discussing how WCM, the WINCMD default exten i files with which you once to us_e WINCMD, I must start with some in the edit box. In the Combo Box t proudly automated all your terminology. WI CMD.EXE is the Win­ lists the programs, enter the path to - routine tasks? If you've dows program I've written. It interprets filename of WINCMD.EXE. Click joined the mad rush to Win­ programs-which are ASCII files-that the OK button and you're set! dows, you've simply had to are written in the WINCMD language. From now on, any time you click learn to do without this use­ To keep the two kinds of programs sepa­ a WINCMD file in the ful and convenient facility. rate, I'll use the full name and extension, , Windows will am The DOS batch file lan­ WINCMD.EXE, when referring to the matically start WINCMD.EXE and p guage may have been limited, but Win­ utility itself. I'll refer to the ASCII files it the name of your WINCMD progra dows supported nothing at all in its that WINCMD.EXE interprets as When WINCMD.EXE is started with ~ place- until now. WINCMD programs. filename on the command line, it will im WINCMD.EXE, this issue's free util­ To install WINCMD.EXE, just copy mediately start executing that file. Whe­ ity, is a Windows-based command file in­ it, along with WINCMD.ICO (its icon WINCMD.EXE has finished executin" terpreter that lets you create simple Win­ file) and any .WCM files you have (two the WINCMD program, it will gracefull_ dows programs that perform the terminate. repetitive functions that were once the WNDCMD.EXE, a Since debugging is important even f domain of DOS batch files. WINCMD WINCMD programs, WINCMD.E goes far beyond the simple DOS batch Windows counterpart to provides help m this area. L file language, however. Its language in­ WI CMD.EXE is started with an l cludes looping and branching statements DOS's hatch language, lets command switch before the WINC along with user-defined subroutines and program name, WINCMD.EXE will di functions. Both numeric and string vari­ you automate tasks in the play a window with your WINCMD pro­ ables are supported, as is a full suite of gram loaded into memory, ready to ex - arithmetic and logical operators. environment and even goes cute. Be sure not to place the /L after th You can use any ASCII editor, such WI CMD program name, or it will as Notepad, to create WINCMD pro­ beyond its ancestor's considered an argument to the comman grams. Typically, these programs will file. When WINCMD.EXE starts, it per­ carry a .WCM extension, though this is capabilities. forms some preprocessing on your not required unless you want the pro­ WINCMD program. If it finds any line gram to be callable from another it does not understand, you'll get appro­ WINCMD program. A single .WCM file are supplied with the program), into a di­ priate print error messages with Jin can be up to 64K in size, though its exe­ rectory on your path. Select the File I numbers to help you find the problem. cutable parts (those not made up of com­ New menu item in the Program Manager Error messages are displayed for such ment lines) are limited to about 45K. and choose Program Item. Enter the faults as lines being too long or numbers Both the WINCMD program and its short title you want to appear under the too big. When an error message i5 C source files can be downloaded from icon as the Description, and enter the printed, WINCMD.EXE changes to di_­ PC MagNet as WINCMD.ZIP. See the complete path and WINCMD.EXE on play two windows. The listing windo " Utilities by Modem" sidebar for instruc­ the command line. displays the WINCMD program with th tions. You can also get the files by send­ To make it easy to run WINCMD pro­ line highlighted that is to be execut ing a postcard with your name, address, grams, you should then also use the File next. The output window displays the las and disk size preference (360K, 720K, Manager to associate the .WCM file ex­ 100 lines printed by the WINCMD pr 1.2MB, or 1.44M) to the attention of tension with WI CMD.EXE. To do this, gram as well as the error messages. Katherine West, PC Magazine, One Park start the Windows File Manager and se­ WINCMD.EXE allows you to run Ave., ew York, NY 10016-5802. No lect the File I Associate menu item. WINCMD program from the window · phone calls, please! When the dialog box appears enter two different ways. The program can •

302 PC MAGAZINE APRIL 27, 1993 U'!'ILITIES

button will cause WINCMD.EXE to exe­ WI NCM D Statements and Functions cute one line of the WINCMD program. Statement Description The listing window will highlight the next IF{expression} Branching statement line to be executed. [THEN] WINCMD VARIABLES AND ARITHMETIC {statement} The WINCMD syntax combines the [ELSE] power of a high-level language with the {statement}] simple command line operation of a DO Block statement batch language. Before discussing the dif­ {statement} ferent WINCMD statements in detail, I {statement} should explain how WINCMD programs use variables. The WINCMD language makes no distinction between string vari­ ables and numeric variables. That is, a {statement}END variable that has been assigned a string WHILE {condition} Loop statement value can later be assigned a number and {statement} vice versa. Variables do not have to be EXIT [return code] Terminates a WINCMD program declared before they are used. As you RETURN [return code] Terminates a subroutine would expect, variables are assigned LEAVE Exits from a loop using the equal sign. PRINT {expression} Displays a line of text to the user The following three statements are ex­ SAY {expression} Displays a line of text to the user amples of valid WINCMD assignments. REM {text} Comment, remainder of text on the line ignored

// {text} Comment, remainder of text on the line ignored var= 1 • {text} Comment, remainder of text on the line ignored var= magazine var= "I like PC Magazine" Function Description LENGTH(string) Returns the length of a string . In the first assignment above, the vari­ UCASE(string) Returns a copy of the string in upper case characters. able VAR is given the value 1. The sec­ SUBSTR(string, start, length) Returns a part of a string. ond assignment has the variable assigned ARG(number) Returns an argument to program or a subroutine. the string " magazine." In the third as­ DELAY(number) Halts program execution for a number of milliseconds. signment, VAR is assigned the string " I APPACTIVATE(win title text) Activates the window with the matching title text. like PC Magazine." The quotes allow GETAPPACTIVE (no parameters) Returns the title text of the currently active window. spaces to be included in the string as­ GETAPPEXE(win title text) Returns the EXE file name for a window. signed to VAR. If the quotes were not SENDKEYS(send key string) Sends a series of keys to the active application. used, WINCMD would attempt to assign MSGBOX(message, title, flags) Displays a message box. the variable VAR with the string " I" , but ASKBOX(text, default answer) Displays a dialog box so that the user can enter a value . would then find the additional characters TICKS(no parameters) Returns the number of milliseconds since Windows was on the line. WINCMD would then stop started. execution and print the error message Figure 1: These statements and functions are supported in the WINCMD language. "Extra characters on line. " Variable names may contain numbers, run normally either by pressi ng the Run ton. If you want to start the program from letters. or the characters _, $, % , and #, button or by selecting the Run I Run Pro­ the beginning. first click on the Reset but­ but the first character in a variable must gram menu selection. The WIN­ ton to reset the program and then hit the always be either a letter or underscore CMD.EXE window will then change to Run button to start the program on its (_). Variable names are not case-sensi­ display only the output window and run way. Note that when WINCMD resets a tive; the names BOB, Bob, and bob all the WINCMD program. The Run button program, it rereads the program from the refer to the same variable. Variable text will change to Stop. disk. This means that you can make names can be up to 253 characters long. Pressing the Stop button will cause changes to the program and then you can Variables can be used anywhere strings WINCMD.EXE to stop executing the have WINCMD load those changes by or numbers can be used in the program. INCMD program and to display both pressing the Reset button. The numbers you use may range from .he output and listing, windows. Again, The other way to run a WINCMD pro­ -2,147,483,648 to 2,147,483,647, and can the line in the program that wou\d have gram is 'oy stepping, through i.t one \i.ne be expressed in binary, octal, decimal, or been executed next is highlighted. The at a time. Single stepping is very handy hexadecimal format. (ln entering num­ program can then be restarted from this to see exactly how a WINCMD program bers, do not type in the commas shown point simply by clicking on the Run but- is being executed. Each press of the Step above, however.) The WINCMD defa ult

APRIL 27, 1993 PC MAGAZINE 303 UTILITIES

is base 10. To enter a nondecimal num­ bt r, start the number with a zero, then us~ b, o, or x as the second character, •Utilities by Modem indicating a binary, octal. or hexadecimal m mber, respectively. The following four The PC Magazine utilities are avail­ Register your name and enter your Ii res demonstrate different ways to write able by modem ~rom PC MagNet, a American Express, MasterCard, or th: decimal value 100. ZiffNet service hosted by Compu­ Visa number. (If you'd like to have Serve. your company billed instead, call v, r = 100 To find the phone number nearest CompuServe at 800-848-8990.) Your vc r = 0x64 you, set your communications soft­ personal user ID and a temporary v, r = 00144 ware to 300, 1,200, 2,400, or 9,600 bits password will be displayed. A new vc r = 0b1100100 per second, 7 data bits, even parity, password will arrive in the within J stop bit, and full duplex, then dial ten days to confirm your subscription. WINCMD uses the standard set of 800-346-3247 with your modem. ZiffNet membership costs $2.50 ari .hmetic operators for addition ( + ), When the modem connects, press per month. This includes access to PC sul ,traction(-), multiplication(*), and di­ Enter. At the HOST NAME prompt, Magazine Editors' Choice awards, vis on(/). Arithmetic comparisons can be enter PHONES. Follow the menus Product Reviews Index, Weekly pe1formed for greater than (>),less than and note the number closest to you. News from PC Week, Buyers' Market. ( <, , greater than or equal to (>= ). less Or you can call 800-635-6225 (voice) ZiffNet Highlights, and the Support than or equal to ( <=), and not equal ( <>) and follow the instructions and note Forum (which also includes the cur­ opvrators. Comparison operators return the number. rent utility). CompuServe members a nonzero value if the condition is true To obtain the current issue's utility can join by entering GO PCMAG at anc a zero if the condition is false. free of charge: Dial the local number; any CompuServe ! prompt. \sin BASIC, the equal sign(=) per­ at the HOST NAME prompt, type Outside of these areas, PC MagNet forms double duty in WINCMD. When CIS; and at the USER ID prompt, costs $6.30 per hour for 300-bps ser­ pla,:ed alone on a line to the right of a enter 60116,l. Then at the PASS­ vice; $12.80 for 1,200 or 2,400 bps; and var able, it indicates an assignment, as in WORD prompt, enter PCMAG $22.80 for 9,600 bps. Billing is based va1 = 5 . In any other location, however, UTIL. on 1-minute increments. it irdicates a test for equality. WINCMD To join ZiffNet: At the USER ID These programs can be copied but alsc supports double equals ( ==) as an prompt, type 177000,5000. Then, at are copyrighted. You may make cop­ equ 1lity operator. The equality test the PASSWORD prompt, enter PC* ies for others as long as no charge is wo1 ks for both numbers and strings. For MAGNET. Finally, at the ENTER involved, but making copies for any example, to test if the variable MONTH AGREEMENT NUMBER prompt, commercial purpose is strictly prohib­ is "February", simply use the expression type PCMAG93. ited.

mon :h = "February" pie, 5 ANDB 20 returns a value of 4. If no extension for the program to ( 0 0101 ANDB 1010 0 = 0 010 0 ) . launched is specified, WINCMD \\ WINCMD also includes a set of logi­ When placed between two strings, the look for each of the extensions listed cal ;.nd bitwise operators. The logical op­ plus sign ( +) indicates that the strings the Programs key in the [Windows] s erat,)rs AND, OR, XOR, and OT act should be concatenated. For example. tion of WIN.IN! until the particular pr on t 1e logical value. If a variable is 0, its gram is found. If no path is specifit: logit al value is FALSE; otherwise, its log­ ClTYSTATE - "Knuxville " + WINCMD follows the same search pr ical value is TRUE. For example, the "Tent1EJ.=iec' cedure Windows uses when starting num :,ers 5 and -3 are TRUE values while program from the Program Manag 0 is a FALSE value. The logical operators will assign the variable CITYSTA TE to What this means is that WINCMD fi allm , you to combine other tests into one the string "Knoxville Tennessee". looks in the current directory, folio,\ con ition. For example, the expression WINCMD STATEMENTS A list of the by the Windows and Windows Syst statements and functions in the directories, then the WINCMD dim. DAY= "Mond a y " AND WINCMD language is shown in Figure tory, the directories listed in the PAT:­ MO~ rH <> "Feb r u a ry " l. To cause WINCMD to launch a pro­ statement, and finally any directories t gram or to execute a DOS command, sim­ are mapped on a network. will c valuate to TRUE only if both DAY ply include that program or command on Executing DOS internal command is eq ial to Monday and MO TH is not a separate line. For example, to start the as easy as launching programs. To c ·'February". Windows Notepad editor, simply type the file WINCMD.C to WINCMD.BA: Tl1e bitwise operators ANDB, ORB, the name of the program on a line, as in just include the command on the line XORB, and NOTB perform logical oper­ you would if you were typing it in fr ation; on each bit in a number. For exam- NOTEPAD the DOS command line:

304 PC MAGAZINE APRIL27. 1993 UTILITIES

launched. In DOS, if a gant way for WINCMD programs to WlnCmd MsgBox Functions batch file starts a pro­ to the user, the simplest way is to use Value Produces gram, it waits until SAY and PRINT statements. (SAY 0x0000 OK Button that program termi­ PRINT perform the same functi 0x0001 OK and Cancel Buttons nates before continu­ While the SAY/PRINT function is 0x0002 . Abort, retry and Ignore Buttons ing. In the example lar in the function of the SAY statem_ 0x0003 Yes,No,CancelButtons that follows, how­ in REXX, I have included the PRI'.' 0x0004 Yes and No Butto ns ever. a WIN CMD alias for all those users familiar , 0x0005 Retry and Cancel Buttons program launches BASIC. 0x0010 Hand Icon three programs: Note­ The SAY statement displays a 0x0020 Question mark Icon pad, Word for Win­ that appears in the output win 0x0030 Exclamation Icon dows, and WordPer­ of the WINCMD.EXE window. 0x0040 Asterisk Icon fect for DOS. WINCMD.EXE is minimized whee 0x0OOO Sets default button to button 1 SAY statement is encountered, the, Ox0100 Sets default button to button 2 NOTEPAD Ox0200 Sets default button to button 3 WinCmd Key Aliases WINWORD OxlOO0 Key Alias Creates System modal message box WP Ox2000 Creates Appl ication modal message box BACKSPACE {bksp} BREAK {break} Return Value Meaning Since WINCMD programs do not wait CAPS LOCK {capslock} OK button pressed CLEAR {clear} 2 Cancel button pressed for a program to finish before continuing to DEL {delete} 3 Abort button pressed DOWN ARROW {down} 4 Retry button pressed the next line, all three programs will start at END {end} Ignore button pressed - ENTER {enter} 6 Yes button pressed approximately the same time. WINCMD ESC {esc} 7 No button pressed programs continue HELP {help} Figure 2: These are the values that can be passed to the MsgBox function until there are no HOME {home} and the meaning of the values returned. more lines in a pro­ INS {insert} gram or an EXIT LEFT ARROW {left} {numlock} COPY WINCMD . C WINCMD . BAK statement is encountered. NUM LOCK The EXIT statement is used to stop PAGE DOWN {pgdn} When WINCMD sees a DOS internal execution of a WINCMD program. In the PAGE UP {pgup} command, such as COPY or DEL, it following example, therefore, Notepad PRINT SCREEN {prtsc} launches the command processor listed in and WordPerfect for DOS will be execu­ RIGHT ARROW {right} the COMSPEC statement in the ted, but Word for Windows will not, since TAB {tab} WINCMD environment. its line comes after the EXIT statement. UPARROW {up} If Windows is running in Enhanced Fl {Fl} mode and your _DEFAULT.PIF file is NOTEPAD F2 {F2} configured to run DOS programs in a WP F3 {F3} window, DOS internal commands will be EXIT F4 {F4} executed in the background. Since the WINWORD F5 {F5} WINCMD language uses some of the F6 {F6} same special characters-such as the as­ Comments can be included in F7 {F7} terisk (*)-as the DOS command line WINCMD files by using the REM state­ FB {FB} does, you will need to enclose any file­ ment. WINCMD ignores any characters F9 {F9} names in quotation marks. For example, from the REM statement to the end of Fl0 {FlO} to copy all files with a .C extension to the line. To provide a syntax similar to Fll {Fl 1} .BAK, just enclose the filenames in quo­ that of BASIC and C, comments can also F12 {F12} tation marks as follows. be indicated by the single quote (') or by To shift a key precede the key with one of th e ·_ double forward slashes (//): lowing three characters. COPY "*.C D:\BACKUP" SHIFT + (plus sign) rem This is a comment line in WINCMD ALT % (percent sign ) One major difference between the ac­ // This is too . CTRL /\ (caret) tion of a WINCMD file and a DOS batch Don't forget me ! Figure 3: The aliases above are used by the file is that WINCMD programs continue SENDKEYS() function to send "special key" to execute after a program has been While I will later discuss a more ele- keystrokes to applications.

306 PC MAGAZINE APRIL 27, 1993 UTILITIES

IF BOB > 5 SEARCH.WCM THEN Complete Listing BOB= BOB , 2 // ------// WinCmd program that uses file manager to Here. if the variable BOB is gre:ner than / / search a disk for a set of files . //------5, BOB will be set to its current value plus // 2. Jf you added an ELSE st;Hemcnt, the / / Ask user for file spec. Default to all files example might become II ans= AskBox ("Enter the file name to look for " , "* . *") II IF BOB> 5 / / If user pressed the Cancel button, quit II THEN if ans+a == a BOB BOB+ 2 exit I I ELSE // If the file manager isn't running, stare it BOB= BOB+ 1 I I if (AppActivate ( "File Manager'') = 0) win file ln this case, if the variable BUB is greater I I than 5, BOB wi ll be increased by 2: other­ // Send keys to File Manager to search disk. I I wise BOB will be incre<1se 5 sendkeys I" {enter}") //Start search sendkeys ( "%-x") //Maximize search results window BOB BOB+ 2 exit ELSE BOB= BOB+ 1 Figure 4: SEARCH.WCM is a WinCmd program that uses the File Manager to search for files on a disk. Note that the IF statement will only dow will be restored so that the user can statement. The IF statement will execute execute one statement line for a THEN read the text of the message. The SAY the next line if the condition included in or ELSE clause. To get around this. you statement first evaluates whatever is on the IF statement is true ( or non-zero). can use the DO and END statements, the line beyond the SAY statement, and While not required. th e THEN statement which permit grouping multiple state­ then it displays the results in the output can be used either on the same line as ments as if written on a single line. window. The following two statements the IF or on the next line. If the optional DO ... EN.D blocks of statements ...-:m be will provide an example of SAY and ELSE keyword is used and the expres­ inclu 5 AND DAY= "Monday" marks is lowercase. Unlike BASIC's with any variable or equation, and {state­ DO PRINT statement, the different expres­ ment} represents any valid WINCMD BOB= BOB+ 2 sions do not have to be separated by com­ lin e. FRED= BOB - 2 mas or semicolons. As a concrete example of the IF state­ J OE= BOB - F' ED Branching is performed with the IF ment at work. consider the following: END

APRIL 27, 1993 Pl'MAC,AZlNI:' 307 UTILITIES

ELSE will thus continue to test the condition after it. The next statement to execu DO and execute the statement until the con­ will be the first one beyond the loop. I COPYWINCMD.C WINCMD.BAK dition evaluates to zero. Like the IF state­ the next example, the loop terminates JOE= BOB/2 + 2 ment. the WHILE statement works only the variable ERROR becomes non-zer END on the statement line that immediately follows the WHILE, but this statement WHILE FRED<> BOB Here, DO END statements enclose the line can be the start of a DO ... END block. DO three statements of the THEN clause and Thus. multiple statements can be execu­ BOB= BOB+ 1 the two statements of the ELSE clause. ted in the loop. Note also that a more complex condition The example below sets the variable REM is used. In this example, the THEN clause COUNT to 10. and then enters a WHILE REM INSERT OTHER LINES HERE will be executed only if BOB is greater loop that decrements COUNT each time REM than 5 and DAY equals the string " Mon­ through until COUNT equals zero. day. " IF ERROR Loops are performed by the WHILE COUNT = 10 LEAVE statement, whose syntax is WHILE COUNT> 0 DO REM WHILE {condition} COUNT= COUNT - 1 REM ANY STATEMENTS AFTER THE {statement} FRED= FRED* FRED REM LEAVE WILL NOT BE EXECUTED IF END REM ERROR NOT 0 The WHILE statement evaluates the REM condition and, if the condition is non­ If you want to leave a WHJLE loop END zero (that is, if it is TRUE), the line indi­ before its condition evaluates to false, cated by (statement) is executed. The you can use the LEA VE statement. SUBROUTINES AND FUNCTIONS Tl WHILE statement then re-tests the con­ When WINCMD.EXE sees a LEA VE WINCMD language allows yo u to crea dition and, if the result is still non-zero, statement, the loop is immediately ex­ subroutines and functions. Actual it executes the statement again. WHILE ited, even if there are other statements WINCMD makes no distinction betwe Introducing three ne You'll find th

Speed Price

~ 1993 3Com Corp. 3Com. Networks That Go 1he Di ~lance. EtherLink. LinlWatch, and Pa rallel Td!lki ng are tradema rk:-. of 3Com Corp. Micro Channel i!i a registered trademar i... of lnt ernat1onal Busi ness Machine~ C i""""'------•11111!

UTILITIES

functions and subroutines. If a user­ EXIT BOB= BOB+ 4 defined routine is called while evaluating EXIT an expression, the routine is treated as WASTETIME : a function and the value returned by the COUNT= 1000 WASTETIME : routine replaces the function name. If the WHILE COUNT> 0 COUNT= ARG(l) user-defined routine is called on a sepa­ COUNT= COUNT - 1 WHILE COUNT> ARG (2) rate line, it is treated as a subroutine and RETURN COUNT= COUNT - 1 any value returned by the routine is sim­ RETURN ply ignored. In this example the variable BOB is in­ The start of a subroutine is defined by cremented, after which the subroutine is Subroutines retrieve their arguments a label, which is quite simply the name called. The loop is executed in the sub­ by using the built-in ARG() function. of the subroutine. A label must be a valid routine and the subroutine returns, upon ARG() returns the subroutine parameter variable name; that is, it must start with which BOB is increased by 4. The EXIT indicated by the number passed to it; that a letter or underscore and it must be less statement then prevents the program is, ARG(l) returns the first parameter, than 253 characters in length. The last from continuing down through the ARG(2) the second, and so on. If a sub­ character of a label must be a colon (:). W ASTETIME subroutine again. You routine attempts to read a parameter that A subroutine is terminated by a RE­ must always EXIT a subroutine. was not passed, ARG() returns a null TURN statement. There is no limit to the You can pass parameters to subrou­ string. If ARG() is cailed with no argu­ number of statements that can be used tines by enclosing the parameters in pa­ ments, it returns the number of parame­ in a subroutine. rentheses after the variable name. Multi­ ters passed to the subroutine. ARG (0) In the following example, the subrou­ ple parameters are separated by commas. returns the name of the routine that was tine W ASTETIME is called to waste a Below, I've modified the former example called. little time in the program. to pass the number 1000 and the variable If the ARG() function is called out­ BOB to the WASTETIME routine. side a subroutine or function, it returns BOB= BOB+ 1 the command line arguments to the pro­ WAST ET I ME BOB= BOB+ 1 gram. Just as the %1 and %2 return the BOB= BOB+ 4 WASTETIME (1000, BOB) first and second arguments in a DOS arallel Tasking adapters. ines familiar When we introduced EtherLink®III , the world's fastest Ethernet adapter, your response was equally quick: You bought it in rec­ ord numbers. Now EtherLink III 's unique Parallel Tasking" technology includes a 32-bit EISA and 16/32-bit Micro Channel®for twisted­ pair, thin, or thick coax, and a 16-bit ISA combo with all three media on the same board. You get the same pipelining design, the same software driv­ ers, and the same automatic self-configuration that made EtherLink Ill a runaway success. Plus, complete SNMP man­ ageability with LinkWatch '." And like all 3Com®adapters, the new EtherLink III family comes with a lifetime warranty. To learn more about this remarkable family of Ethernet adapters, and its surprisingly low price, call l-800-NET-3Com. Reaction Or see your nearest 3Com reseller. Because now there's even more to smile about. Networks That Go the Oistance'M CIRCLE 293 ON READER SERVICE CARD UTILITIES

batch file, ARG(l) and ARG(2) will re­ parameters. which determine the portion because during a D ELAY() functio turn those parameters for a WINCMD of the string returned. The first argument WINCMD.EXE is idle and does not tak file. ARG(0) returns the name of the is the original strin g: the second argu­ any processor time away from other ap­ WINCMD program, and ARG() returns ment is the starting character: and the last plications. the number of parameters on the com­ argument is the number of characters to The MSGBOX() function lets mand line. return. In the following example, the var­ WINCMD program display a standar In the example below, the user-de­ iable FRED is assigned the string Windows Message Box. The functi fin ed function MAX returns the va lue of " bcdef', which represents the second takes three parameters: the text of t the largest argu ment passed to it: through the seventh characters of the message box; the title of the inessage bo string "abcdefghi." and a configuration parameter that te BOB= MAX (300, FRED, SAM, JOE) * 3 Windows which buttons and icon to di:. EXIT FRED = SUBSTR ("abcdefghi", 2, 5) play in the box. The usable configurati parameters are shown in Figure 2. TI MAX : The UCASE() function returns an up­ various values listed can be combined COUNT = ARG ( ) percase version of the string passed as its using the ORB operator. MAXVAL = ARG(l) parameter. The string passed is not MSGBOX() function also returns a val WHILE COUNT> 0 changed. In the next example, BOB is as­ that depends on which button you pre DO signed " BCDEF" while FRED remains Figure 2 also shows what these return IF MAXVAL < ARG(COUNT) " bcdef". values mean. MAXVAL = ARG (COUNT) The fo ll owing example displays ave­ END FRED = "BCDEF" disturbing message box. RETURN MAXVAL BOB= UCASE (FRED) REPLY = MsgBox ( "Your disk is In the code above. MAX uses ARG() to The DELAY() function holds up the trashed" , "Disk Error " , 2 ORB 0x3e determine how many arguments have execution of the next line in the program ORB 0xl000) been passed to it. It then compares each for a specified number of milliseconds. argument and returns the val ue of MAX­ While the delay time can range from 1 This message box displays "Your dis Y AL on the RETURN line. When MAX to 65 ,355 ms, in practice numbers less trashed" in a message box titled "D has finished. the va riable BOB will be as­ than lO0 are not useful. Using the Error," together with an Exclama signed the hi ghest value of al l the argu­ DELAY() function is preferrable to icon. The box will have three butt ments passed, multiplied by 3. simple looping as a way of killing time, Abort, Retry, and Ignore, and will be WlNCMD variables are global: If a variable is modified in a subroutine, the change will also be reflected in the varia­ • PC Magazine Utilities Updates ble when the subroutine is finished. WINCMD does not support any form of As with all good software, the pro­ EMMA.COM, Version 2.2 local variables, nor does it currently let grams presented in PC Magazine are EMS40.SYS, Version 1.1 you get rid of a variable that has been upgraded and improved. Here's a par­ PCACCESS.EXE, Version 1.1 defined. tial list of the programs on PC MagNet RN.COM, Version 3.0 WINCMD BUILT - IN FUN CTIONS WIN- that have been updated. To download SLICE.COM, Version 1.3 CMD has a number of built-in fu nctions these files from the U tilitiesfTips Fo­ SNIPPER.COM, Version 1.2 that make it easier to create useful pro­ rum, type GO ZNT:TIPS, type LIB ZCOPY.COM, Version 1.4 grams. These functions can be divided or select Libraries from the menu, For a list of the programs that are into two types: general programming then select Library 2 PCMAG available on PC MagNet, download functions and functions designed to UTILS. Type DOW and the filename the file PCMCAT.ZIP from Librai: control Windows applications. The listed below (for example, DOW ( General Info) of the UtilitiesfTip LENGTH() function returns the number ANSI.COM), or select Download a Forum. of characters in a string. For example, in File from the menu. A downloadable index to PC Ml _ the line ADDIT.COM, Version 1.1 azine's product reviews is also avail­ ANSI.COM, Version 1.3 able in Library 1 ( General Info). BOB= LENGTH ("HolstonHills") BAT2EXEC.COM, Version 1.5 PCM.EXE is a self-extracting file c1.. ­ BCOPY, Version 1.2 taining the Computer Library PC BOB would be assigned 13. .COM, Version 1.1 Magazine R eviews Index for Janua The SUBSTR() function , which is CHKFRAG.EXE, Version 1.7 1988 through D ecember 1991. lt re­ simi lar to the like-named functions in CONFIG.CTL, Version 3.0 quires the search files that are in BASIC and RE XX. returns a portion of DIRMATCH.COM, Version 3.1 PCSRCH.EXE. a string. SUBSTR() is called with three

310 PC MAGAZINE APRIL 27, 1993 UTILITIES tern-modal. The value in the variable RE­ successful, otherwise it returns 0. tively. The shifting prefixes can be com­ PLY depends on which button the user In using APPACTIVATE you must bined, so you can create combinations presses. bear in mind that many programs add such as Ctrl-Shift-Alt-W. The ASKBOX() function displays a things to their window titles.Notepad, for If you want to hold the shifting key dialog box with a message and an entry example, adds the current filename; Pro­ down for a number of keys, enclose the field. This function allows WINCMD gram Manager adds the name of a pro­ keys in parentheses. For example, to send programs to ask the user for input. The gram group that's maximized, and so on. the f and x keys while holding down the two parameters to the function are the Unfortunately, APPACTIVATE can't Alt key, use the command question to ask the user and the default recognize these expanded titles. answer for the entry field. ASKBOX will The SENDKEYS() function provides SENDKEYS ( "% ( fx) ") display a dialog box with OK and Cancel a way for WINCMD programs to control buttons. If the user presses OK, the func­ Windows applications by sending key­ Special keys, such as Enter, Escape, tion returns the text in the entry field. If strokes to the application that has the and Tab, can be sent using an alias for the user presses the Cancel button, the current input focus. SENDKEYS takes the key, enclosed in brackets{). A list of function returns a null string. only one string argument, but that string the recognized key aliases is shown in The GETAPP ACTIVE() function re­ can simulate every key on the keyboard, Figure 3. For example, to send Enter and turns the text of the active window on the including the Ctr!, Alt, and Shift keys. Tab keys to an application, use the com­ Windows desktop. (This is the window on The format of the SENDKEYS() in­ mand the desktop that has the active colored put string is similar to the strings used by title bar.) The text returned is the text the SENDKEYS functions in WordBA­ SENDKEYS({enter}{tab}) from the title bar of this window. For ex­ SIC and Visual BASIC. Unlike Word­ ample, if Notepad has just been started BASIC and Visual BASIC, however, in To send the such keys as +, ", and %, and has the input focus, the text returned a WINCMD program you set the active enclose the key in brackets thus: by GETAPPACTIVE() will be " Note­ application before sending the keys, not pad - (Untitled)." The GETAPPAC­ after. Most keys can be sent to an applica­ SENDKEYS ( " { +} •• ) TIVE() function has no input paramters. tion simply by including the letter in the The APP ACTIVATE() function acti­ input string. For example, to send the Note that I have enclosed most of the vates the window whose title bar text three keys a , b, and c to the Program SENDKEY strings in quotes. This is be­ matches that supplied as the single pa­ Manager, just use the following two lines cause many of the characters in a SEND­ rameter to the function. For example, to in your WINCMD program: KEY string may be characters that set the input focus to the Program Man­ WINCMD would otherwise interpet for ager, the function would look like this: AP PACT IVATE ("Program Manager") its own use. The plus key and the paren­ SENDKEYS ( "abc" ) thesis keys are examples. To avoid having APPACTIVATE("Program Manager") WINCMD do something you do not To send a sh ifted key, simply precede want, simply enclose the string in quotes. The function returns the text of the previ­ the letter with ", %, or+ character to simu­ Since there is no harm in using the quotes, ously active window if the function was late the Ctr!, Alt, or Shift key, respec- I recommend you use them consistently.

2CLIP.WCM Complete Listing

I I ------II ! I Wincmd program that copies a text file into the // see if notepad read the file. If it didn't, the warning box I I Windows clipboard // for the "Open" dialog will be active, and GetAppActive() will 11------// return "Open". In that case, close notepad and tell the user II II I I Ask user for file name . Default to blank winact = Getappactive () II if (winact = "Open") do ans = AskBox ("Enter the name of the file to copy into the clipboard•," • J sendkeys ("{enter}") / /Close the warning box II , sendkeys (" {esc}") / /Close the file open box I I If user pressed the Cancel button, quit ok = fail II end if a ns+a == a else do exit sendkeys ("%ea" ) I /Select. all t.he c.ext II sendkeys ( "%ec •) I /Copy the data into the clipboard I I Start NotePad ok = pass II end notepad sendkeys ( • %fx•) //Close notepad if (ok = fai l) II m.sgbox ( "Notepad could not access the file you requested. I I Send keys to notepad to read in the file Nothing was copied into the clipboard", "Error!", 0) II sendkeys { "lfo") / /Display the file open dialog box exit sen dkeys (ans) / / Enter file spec sendkeys ( • {enter} ") I /Open the file.

Figure 5: 2CLIP.WCM uses the NOTEPAD to copy a file onto the Windows Clipboard.

APRIL 27. 1993 PC MAGAZINE 311 UTILITIES

One additional caution is that SEND­ KEYS() works by placing simulated key­ C the Future. strokes in Windows' system event queue. Since this queue can become filled if you send too many characters at once, it's a good idea to break up the keys that you send into blocks of fewer than five keys. Since WINCMD.EXE yields to other ap­ plications between each line of a WINCMD program, sending keys on consecutive lines will not present a problem. The GETAPPEXE() function returns the name of the program that created the window with the matching title text. GETAPPEXE() is handy for determin­ ing whether a particular program is run­ ning. For example,

GETAPPEXE ( " Program Manager")

will return PROGMAN.EXE. Note again, however, that the match to the ISBN 1-56276-040-8 Price $39.95 ISBN 1-56276-069-6 window title text must be exact. Price $29.95 The final WINCMD function, TICKS() returns the number of millisec­ C and C++ have become the languages of choice among onds since Windows was started. today's programmers and future programmers. Whether TICKS() takes no arguments. WINCMD EXAMPLES The power of you're just entering the world of C or moving to the power WINCMD is best demonstrated by ex­ of C++, the authorities at PC Magazine have the knowledge ample. SEARCH.WCM, the first of the to get you where you want to go. two sample programs I've included with this utility, uses the File Manager to If you're new to C programming or looking to sharpen search a disk for matching files. The WINCMD program is listed in Figure 4. your skills, PC Magazine Guide to C Programming will get The second program, 2CLIP.WCM, uses you the results that you want. Internationally acclaimed Notepad to copy a file into the Windows Clipboard. Both programs use features of author and C instructor, Jack Purdum, provides a solid the standard Accessories bundled with foundation in all aspects of C programming. Windows to help perform the function of the WINCMD program. The programs World-renowned expert William Roetzheim shows you also demonstrate the power of the how to make Windows Graphical application SENDKEYS function in controlljng other programs. development a productive and rewarding experience in PC WINCMD.EXE allows you to write Magazine Programming Windows with Borland C++. This command files with which you can con­ unique book/disk package covers object-oriented program­ trol your favorite Windows applications. However, WINCMD.EXE is not the last ming, the Windows Application Program Interface, and the that you will hear about the WINCMD Object Windows Library. language. In the next installment of this column, I will present a DLL that hooks into WINCMD.EXE to provide dozens of helpful functions for your WINCMD Available at fine bookstores, or call programs, so be sure to stay tuned! □

1-800-688-0448 DOUGLAS BOLING I S A CONTRIBUTING ext. 8311 EDITOR TO PC MAGAZ INE . © 1992 Ziff-Davis Press 312 PC MAGAZINE APRIL 27. 1993