Writing Terminal Service Based Scripts (Part 7)

by [Published on 14 May 2008 / Last Updated on 14 May 2008]

This article continues with the series on creating terminal service based scripts by explaining the inner workings of the script that we created previously.

If you would like to read the other articles in this series please go to:

In the previous article in this series, I showed you how to redirect a command’s output to a text file, and then explained that each subsequent use of the file would cause the outputted information to be appended to the end of the file. I also explained that in the real world there would often be situations in which you may need to create a text file with a unique name rather than simply appending new information to the end of an existing log file.

Toward the end of my previous article, I offered the two lines of code below as a mechanism for producing log files with unique names:

for /F "tokens=2,3,4 delims=/ " %%i in ('date /t') do set datefile=%%i%%j%%k.log
Query User > %datefile%

Unfortunately, I did not really get much of a chance to explain what these lines of code are and what they do. Since creating log files is such an important concept, I want to spend this article explaining how the code shown above works, and how you would use it.

As I mentioned earlier, the sample code’s purpose is to produce a log file with a unique file name. Part of the problem with generating a unique file name is that if you make the file name completely random, then it is difficult to figure out which file contains which log. That being the case, I designed the code above to create a unique filename that was based on the current date. In a way, the filename is not truly unique, because if you were to run the commands above twice in one day, then both instances would produce the same filename. If you find yourself needing to create separate log files several times over the course of a day you can make the filename unique by appending the current time to the date.

Before I show you how the commands above work, let us take a look at how these commands function in a real world environment. As you can see in Figure A, I have created a text file named USER.BAT, and then incorporated the commands above into it. In case you are not familiar with the .BAT extension, .BAT files are known as Batch Files. Batch files are simply text files that contain one or more commands. The purpose of a batch file is to allow you to build scripts based on the commands that you would normally have to enter individually at a command prompt.


Figure A:
This is how the commands work in the real world

If you look at the figure above, you can see that the first thing that I have done is to use the TYPE command to display the contents of the USER.BAT file. This step isn’t really necessary, I just did it to show you that I was using the same code as what I introduced in the previous article.

Notice that at the next command prompt, I entered the command USER. Any time that you create a batch file, the name that you assigned to the batch file is treated as though it belonged to an executable file. In this case, the name of my batch file is USER.BAT, so when I enter the USER command, it causes Windows to execute the batch file.

The next three command prompts that you see in the figure above are followed by various commands. It is important for you to understand that I did not manually type these commands. Instead, Windows is executing each of the commands that are found in the batch file as though those commands had been manually typed.

One thing that I want to point out before I move on is that if you look at the top of the screen shot where I displayed the batch file’s contents, the last line of the batch file read:

Query User > %datefile%

However, when the batch file processes the Query User command, the command looks like this:

Query User   1>04122008.log

My point is that although batch files are designed to issue commands in sequence as though they had been typed, there is one important difference between a batch file command and a command that has been manually entered. When you process commands as a part of a batch file, you have the ability to base a command on the results of a previous command. In this case, the variable %datefile% is being replaced by 04122008. In case you are wondering where this number came from, it was generated based on today’s date, April 12, 2008, or 04-12-2008.

As you can see in the screen capture, I was able to use the date to create a file name. At the bottom of the screen capture, I used the TYPE command to display the contents of the log file that I created, just to prove that the technique works.

Before I start talking about how the individual commands work, I want to show you one more trick. When you look at the figure above, it is really difficult to differentiate between commands that were entered manually, and those that were processed as a part of a batch file.

Often times, batch files are much longer and more complex than the one that I have showed you here, and in such a situation seeing the actual commands that are bring used can make the script’s output scroll off of the screen, or can make it difficult to locate. As such, many programmers begin their batch files with the following command:

@echo off

Echo is a command that is normally used for displaying text on the screen or for adding a line of text to a file. For example, if you wanted to configure a batch file to display the phrase Hello World, you could use the following command:

Echo Hello World

We’ll be using the echo command to display text quite a bit throughout the rest of this article series, but for right now there is another use of echo that I want to tell you about. When you enter the command ECHO OFF, it tells Windows that you want to prevent the batch file’s remaining commands from being displayed, and that you only want to see the batch file’s output, not the commands themselves.

If you simply enter the ECHO OFF command at the beginning of a batch file, the Echo Off command itself will be visible, but the other commands won’t. You can see what I mean if you look at Figure B.


Figure B:
The Echo Off command prevents the remaining commands from being displayed

Using the Echo Off command works but seeing the words Echo Off appear is a little distracting. Therefore, I recommend that you precede the Echo Off command with the @ sign. Doing so prevents the Echo Off command from being echoed, as shown in Figure C.


Figure C:
The @Echo Off command causes all of the commands to be hidden

OK, so the @Echo Off command does a good job of hiding the individual commands that make up the batch file, but to the casual observer it looks as though nothing is happening. We can fix this “problem” by adding the Echo command to the batch file, followed by a line of text explaining what is going on. We can even create another line at the end of the file in which the Echo command is used to tell the user that the command has completed successfully. Such a script might look something like this:

@Echo Off
Echo Creating log file.
for /F "tokens=2,3,4 delims=/ " %%i in ('date /t') do set datefile=%%i%%j%%k.log
Query User > %datefile%
Echo Log file created successfully.

You can see what the output from this script looks like in Figure D.


Figure D:
This is what the output from the script above looks like

This type of output is obviously a lot easier to read, but there are a few problems with it. For one thing, we haven’t told the user where to look for the log file, or what the log file’s name is. For another thing, the command is simply echoing a line of text that says that the log file was created successfully. At this point, the script has no way of knowing if the log file was created successfully or not. I will address these types of issues, and much more as the series goes on.

Conclusion

In this article, I began showing you how to assemble the various commands into a script. I then went on to show you how to clean up the script’s output. In the next article in the series, I want to take a step back and talk more about how the log file name was actually generated.

If you would like to read the other articles in this series please go to:

Advertisement

Featured Links