TechRepublic Tutorial: Scripting in Windows NT, part 4: Start, Title, and other statements

The Start, Title, Pushd and Popd, Mkdir, Rdir, Time and Date, At, and Xcopy statements

In "Scripting in Windows NT, part 1 ," I looked at special characters, subroutines, and the if statement. In part2 , I examined the for and replace statements. In part 3 , I explained three commands: set, call, and shift. In the final installment of this series of Daily Drill Downs, I’ll cover lots of ground: the start, title, pushd and popd, mkdir, rdir, time and date, at, and xcopy statements. As you’ll see, these statements are not as innocuous as they seem.

The start statement
The start statement can be used to control how a command is executed. It has several switches, some you may find useful and several you’ll probably never use. I’ll describe the syntax and explain all the switches, but it’s up to you to decide what’s useful and what’s not.

The syntax of the start statement is
START ["title"] [/Dpath] [/I] [/MIN] [/MAX] [/SEPARATE | /SHARED] [/LOW | /NORMAL | /HIGH | /REALTIME] [/WAIT] [/B] [command/program] [parameters]
These command-line switches give you a lot of flexibility over how the command runs. The switches function as follows:
  • "title"—Title to display in window title bar.
  • Path—Starting directory.
  • I—The new environment will be the original environment passed to the Cmd.exe and not the current environment.
  • MIN—Start window minimized.
  • MAX—Start window maximized.
  • SEPARATE—Start 16-bit Windows program in separate memory space.
  • SHARED—Start 16-bit Windows program in shared memory space.
  • LOW—Start application in the IDLE priority class.
  • NORMAL—Start application in the NORMAL priority class.
  • HIGH—Start application in the HIGH priority class.
  • REALTIME—Start application in the REALTIME priority class.
  • WAIT—Start application and wait for it to terminate.
  • B—Start application without creating a new window. The application has ^C handling ignored. Unless the application enables ^C processing, ^Break is the only way to interrupt the application.
  • command/program—If it is an internal cmd command or a batch file, then the command processor is run with the /K switch to Cmd.exe. This means that the window will remain after the command has been run.
    If it is not an internal cmd command or batch file, then it is a program and will run as either a windowed application or a console application.
  • parameters—These are the parameters passed to the command/program.

The most useful switch is /WAIT. This is the switch I use most often when I use the START command. The /SEPARATE switch came in handy for me one time when I had issued a command in one of my scripts that appeared to eat up memory. After a number of iterations, the script would not run, but the /SEPARATE switch helped solve the problem.

The title statement
The title statement is a very simple statement but it’s one of my favorites. There are no command extensions for this statement. The syntax is
TITLE text
You use this statement to change the text in the blue bar at the top of the DOS window in which the current script is running. Here’s an example: A colleague had written the following script to keep track of the number of print jobs being handled by our print server (we had in excess of 450 printer queues running on this server):
@echo off
::> code to count the number of .spl files in the spool directory on the print server
echo Last: %count%   max: %max%
if %count% GTR %max% set max=%count%
sleep 5
goto Loop
So, every five seconds the script would display the last count and the maximum count. To monitor this, my colleague had to have that window open and in front of all other windows—a difficult prospect considering he was also watching some stats on the same server using Performance Monitor, watching other servers using HP Overview, keeping up with his mail, and producing graphs in Excel, among other things. When I saw this, I quietly went away and modified his script as follows:
@echo off
::> code to count the number of .spl files in the spool directory on the print server
Title Last: %count%  Max: %max%
if %count% GTR %max% set max=%count%
sleep 5
goto Loop
Did you spot the difference? Actually, I did a bit more than change echo to title, but for the purpose of this example I’ve limited it to that. What happened was that the next time he looked at his window he saw his counts in the title area, and he was able to reduce the size of his window so that only the title was showing. However, I then showed him that when he minimized it, he could still see the counts on the icon in the start bar (providing he didn’t have too many things running, of course). Simple, but very effective.

The pushd and popd statements
The pushd and popd statements can be very useful, especially when you’re not sure where your script will be run from.

The pushd statement accepts a path as a parameter. It will remember the current drive or directory and change to the path specified.

The popd statement simply changes back to the directory that pushd remembered and, obviously, needs no parameters.

Here’s an example of how this might be used:
@echo off
pushd c:\winnt\system32\repl\export\script
::> do some processing on files in the above directory
::> run another script that is in the original directory
If you're going to process more than one file, then pushd saves you from having to type the path over and over again when referring to each file. It also enables you to easily return to the original directory to continue processing there.

The md or mkdir statement
The md command is probably one of the first DOS commands you ever used. The command has now been enhanced by command extensions.

The mkdir command’s extensions create any intermediate directories in the path. For example, let’s assume \a does not exist; this means that
mkdir \a\b\c\d
is the same as the following sequence of commands:
mkdir \a
chdir \a
mkdir b
chdir b
mkdir c
chdir c
mkdir d
The rd or rmdir statement
These two statements are interchangeable. You probably used them in the early days of DOS. However, in NT there are a couple of switches you can use:
RMDIR [/S] [/Q] [drive:]path
  • /S—Removes all directories and files in the specified directory in addition to the directory itself. Used to remove a directory tree.
  • /Q—Quiet mode (use this switch if you don’t want confirmation when you remove a directory tree with /S).

The time and date commands
We have all used the time and date commands on the command line, and you could even use them in a batch file by piping echo. to them. However, doing so produces a slightly messy output:
Current time is 13:06:52.36Enter new time:
With command extensions, date and time have been given a switch, /t, which displays the time without prompting for a new date or time. So, you won’t need to use the echo. trick.

The at statement
If you have the Schedule service running on NT (which I recommend you do), you can schedule events (usually the running of scripts or commands) by using the at statement. The syntax of the at statement is
AT [\\computername]AT [\\computername] [[id] [/DELETE] | /DELETE [/YES]] AT [\\computername] time [/INTERACTIVE] [/EVERY:date[,...] | /NEXT:date[,...]] "command"
Without any switches, at will list all the scheduled jobs. Here are the switches you can use with at:

\\computername Specifies a remote computer. Commands are scheduled on the local computer if this parameter is omitted.
Id An identification number assigned to a scheduled command.
/delete Cancels a scheduled command. If id is omitted, all the scheduled commands on the computer are canceled.
/yes Used with the cancel all jobs command when no further confirmation is desired.
time Specifies the time when command is to run.
/interactive Allows the job to interact with the desktop of the user who is logged on at the time the job runs.
/every:date[,...] Runs the command on each specified day(s) of the week or month. If date is omitted, the current day of the month is assumed.
/next:date[,...] Runs the specified command on the next occurrence of the day (for example, next Thursday). If date is omitted, the current day of the month is assumed.
"command" The Windows NT command or batch program to be run.

As an NT administrator, you’ve probably used this command a lot. The first and second forms—at [\\computername] and at [\\computername] id /delete—will almost always be run from the command line.

One drawback of this command is the granularity. If you want a job to run several times a day, you must enter a separate at statement for each time. You can say every Monday, but you cannot say every half hour. However, you can use a script to do that:
For /L %%i in (0,23,1) do call :Schedule %%I
goto :EOF
for %%j in (00,30) do at %1:%%j /every:m,tue,wed,th,f c:\batch\task.bat
goto :EOF
There’s nothing new here, and only one at statement.

One of the switches that requires further explanation is /every:. Here are a few examples to show how this switch is used:
1. at 12:00 /every:28 c:\batch\task.bat2. at 12:00 /every:mon c:\batch\task.bat3. at 12:00 /every:7,14,21,28 c:\batch\task.bat
We already have seen one form, in the sample script above. That script means Schedule c:\batch\task.bat to run at the specified time every Monday, Tuesday, Wednesday Thursday, and Friday.

Example 1 above means Schedule c:\batch\task.bat to run at 12:00 on the 28th of every month.

Example 2 means Schedule c:\batch\task.bat to run at 12:00 every Monday.

Example 3 means Schedule c:\batch\task.bat to run at 12:00 on the 7th, 14th, 21st, and 28th of every month.

As you saw from the syntax, the at command can be used to schedule jobs on the local computer, or you can name a computer to schedule the jobs on. Of course, the Schedule service must be running on any computer on which you attempt to run at.

The xcopy statement
The xcopy statement is a very powerful form of the simple copy command. It allows you to copy only files that are newer, to ignore any errors, to copy hidden and system files, and to overwrite read-only and copy-only files that already exist in the destination directory, among other things.

The syntax of the xcopy command is as follows:
xcopy source [destination] [/A | /M] [/D[:date]] [/P] [/S [/E]] [/V] [/W] [/C] [/I] [/Q] [/F] [/L] [/H] [/R] [/T] [/U] [/K] [/N] [/Z]
As you can see, xcopy has a lot of different switches that you can use. The switches you can use are as follows:

Source Specifies the file(s) to copy.
Destination Specifies the location and/or name of new files.
/A Copies files with the archive attribute set; doesn't change the attribute.
/M Copies files with the archive attribute set; turns off the archive attribute.
/D:m-d-y Copies files changed on or after the specified date.
  If no date is given, copies only those files whose source time is newer than the destination time.
/P Prompts you before creating each destination file.
/S Copies directories and subdirectories, except empty ones.
/E Copies directories and subdirectories, including empty ones. Same as /S /E. May be used to modify /T.
/V Verifies each new file.
/W Prompts you to press a key before copying.
/C Continues copying even if errors occur.
/I If destination does not exist and copying more than one file, assumes that destination must be a directory.
/Q Does not display filenames while copying.
/F Displays full source and destination file names while copying.
/L Displays files that would be copied.
/H Copies hidden and system files also.
/R Overwrites read-only files.
/T Creates directory structure, but does not copy files. Does not include empty directories or subdirectories. /T /E includes empty directories and subdirectories.
/U Copies only files that already exist in destination.
/K Copies attributes. Normal xcopy will reset read-only attributes.
/N Copies using the generated short names.
/Z Copies networked files in restartable mode.

I’d like to emphasize that even if you are now well versed in the commands I’ve described here, what you’ve learned here is only the first step. The most powerful and most productive commands for Windows NT script writers are not the ones found in NT, but those provided in the Windows NT Resource Kit. If you don’t have a copy installed, install it now.

Richard Charrington’s computer career began when he started working with PCs—back when they were known as microcomputers. Starting as a programmer, he worked his way up to the lofty heights of a Windows NT Systems Administrator, and he has done just about everything in between. Richard has been working with Windows since before it had a proper GUI and with Windows NT since it was LANManager. Now a contractor, he has slipped into script writing for Windows NT and has built some very useful auto-admin utilities.

The authors and editors have taken care in preparation of the content contained herein, but make no expressed or implied warranty of any kind and assume no responsibility for errors or omissions. No liability is assumed for any damages. Always have a verified backup before making any changes.

Editor's Picks

Free Newsletters, In your Inbox