The beauty of commandline utilities is that they are available to everyone by default for free, so to speak, and yet provide a fairly powerful scripting language to automate tasks. In our Pietermaritzburg Computer Club's "Windows Know-How" series we shouldn't, in my opinion, pay attention only to GUI tools, but also to the commandline which is in many ways more powerful and adaptable. In the DOS days there were many, many demonstrations of the complex tasks one could perform with commandline scripts or batch files because that was all there was. The proliferation of GUI replacements shouldn't obscure that the commandline is still often the most effective tool.
Most people are probably only aware of the GUI developments of succeeding Windows versions, but the commandline utilities have also gained new and more powerful features, particularly in the NT line of operating systems. (Because they became more un*x-like, I'd suggest ;-) This demonstration was built on Windows XP Pro and might depend on features which were not yet in NT or Win2k. Windows 9x/ME do not have some of the capabilities of the NT line used here, but older DOS tricks could be adapted to achieve similar ends with a bit more trouble.
You can download a zip file containing the batch file with a small set of directories and files to work upon (and this page). Unzip it, with paths enabled, to the folder of your choice and experiment with it and pick it apart as much as you like. There are indications in the batch file how to modify it for simpler and clearer experimentation. The NT "cmd" file extension, equivalent to "bat", allows it to run only on appropriate operating systems. Any comments would be welcome in the PCC's discussion list.
The batch file makes a backup (xcopy) of a directory (hardcoded subdirectory "eg" for this demo) to a backup directory which is named according to systems date and time, and type of backup. The backup is incremental by default and can be full with the optional switch "f". It also logs time-stamped backup information to a daily log file, named according to date with optional comments.
The syntax is
STAMP [f] [Comment words]
The optional parameter f makes a full backup, none (default) makes an incremental backup. Comments are optional. STAMP /? displays simple help.
The name "Stamp" may seem odd in view of the actions performed, but the focus of this demo was not the backup routines, however interesting they might be, but the process of using the system date and time to automate file and directory naming and also using these techniques for writing file content.
NT offers three different possible techniques which can be used for the purposes demonstrated here.
Since DOS DATE and TIME have been interactive commands to set the system date and time. But a batch file should run without user intervention. There were various tricks to mimick user intervention and to convert the screen output of the current date or time into a useable file or environment variable.
The simple trick of using the undocumented ECHO. to pipe a hard return to the commands, mimicking the user hitting the ENTER key, has been made redundant by NT's /t switch for the DATE and TIME commands, which merely displays the current date and time on the screen without further user intervention. The demo script takes advantage of this new feature: DATE /t, which returns the system date in short format, is used to begin creating the name of the backup directory and TIME /t, which returns the time in hours and minutes is used to log the backup in the daily log file. A quirk in the date, but not the time, version of the command puts a gratuitous space at the end of the line, which I also parse out because I didn't want it.
These commands return the information to the screen, so it is not immediately available for use in a script. FOR loops - using features not available in the past - are used to parse invalid filename characters out of the output and to store the data in environment variables which the script can access.
There is, however, a feature in NT commandlines which makes it possibly even easier to access the date or time as a variable in scripts: Both date and time are available as dynamic environment variables without further ado. They are dynamic, which means they can be expanded on demand, for example with ECHO %date%, returning the date as before but the time in greater detail including seconds and hundredths of seconds. The variables do not exist statically in the environment - SET date or SET time (also new to display contents of variables named with that beginning string) indicate they do not exists, but they are calculated on demand. FOR is again used to parse the output into suitable formats. This variation is illustrated where the time is added to the directory name and the daily log file is named/created.
The third technique, which is a variation of an old DOS trick with suitable adaptations to the new quirks and limitations of NT, takes advantage of the ability to set the prompt to display date and time, PROMPT $D $T. The crucial problem to be overcome here is that output from a batch file can only be redirected when it is run in a subshell, whose output can be redirected. This is used to write the date into the log files header section.
That is the core of this demonstration of using the commandline to automate the use of system date and time, the rest is incidental, however interesting or boring it might be. There are some limitations to the way the script is written. It is likely to give unexpected results or problems if the language and date/time formats differ from those on my system, and earlier versions of NT may also cause differences.
I'd welcome it if anyone wanted to contribute adaptations to make it work more universally or expressed interest to pursue this in more demos by email in our discussion list. We could examine the detailed workings of the FOR commands or the command sequencing used in this script, for example. I am sure the boffins amongst us could improve the elegance and efficiency of this example, and I'd welcome such contributions. It occurs to me, for example, that my use of ECHO as in echo %date% may be redundant? If the dynamic variable can be expanded on demand, would it suffice to simply say %date% instead? Would that work and would it require some adaptations? Could you adapt the demo to make it independent of hardcoded paths?
There is a fair amount of self-documentation in the batch file which might spark other discussions. I mention, for example, that the use of FOR is overkill. My (limited) unix experience suggests that there ought to be ways of using redirection and/or command nesting and sequencing to achieve the same ends more simply. Or is the Windows commandline just too limited? Your contributions could to help confirm or deny this hunch.
Much of my pleasure in doing these talks and demos lies in the new learning I gain in their preparation, and you might be able to make more of that possible through discussions in the list or by expressing interest in future, more detailed demos of these and other commandline utilities.