Writing Terminal Service Based Scripts (Part 8)

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

  • Writing Terminal Service Based Scripts (Part 2)
  • Writing Terminal Service Based Scripts (Part 3)
  • Writing Terminal Service Based Scripts (Part 4)
  • Writing Terminal Service Based Scripts (Part 5)
  • Writing Terminal Service Based Scripts (Part 6)
  • Writing Terminal Service Based Scripts (Part 7)
  • Writing Terminal Service Based Scripts (Part 9)
  • Writing Terminal Service Based Scripts (Part 10)
  • Writing Terminal Service Based Scripts (Part 11)
  • Writing Terminal Service Based Scripts (Part 12)
  • Writing Terminal Service Based Scripts (Part 13)
  • Writing Terminal Service Based Scripts (Part 14)
  • In the previous part of this article series, I showed you how to use the Echo command to make a script’s output more legible. I also talked about the need for creating log files whose names were based on the current date, so as to avoid overwriting log files that were previously created. Although I showed you the commands necessary for creating filenames based on the current date for your log files, I never got the chance to show you how those commands actually work. In this article, I want to continue the series by explaining the commands that we have been using thus far.

    Creating Date Based File Names

    As you may recall, we created the following script in the previous article:
    @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.

    The first two lines of this script are pretty cut and dry. The @Echo Off command tells the script not to display the commands as they execute, and the Echo Creating Log File command tells the script to display the phrase Creating Log File. The third line of the script is where things really start to get interesting though. This is the line where we are creating the filename.

    As you can see, the command starts out by using the For command. I have not talked about the For command yet because it isn’t directly related to the Terminal Services, but it is a good command for you to understand, because it allows you to create more powerful scripts.

    If you look at the line of code below, you will notice that there are two primary parts to the For command. There is the For section, and the Do section. The For section sets up the criteria that you need to meet. The Do section executes another command once the criteria specified by the for section is met.

    for /F “tokens=2,3,4 delims=/ ” %%i in (‘date /t’) do set datefile=%%i%%j%%k.log

    In this particular case, the For section of the command looks like this:

    for /F “tokens=2,3,4 delims=/ ” %%i in (‘date /t’)

    The Do section looks like this:

    Do set datefile=%%i%%j%%k.log

    Basically, the Do section is using the Set command to populate a variable named Datefile with the filename that was generated by the For section of the command.

    I realize that these commands probably still look like gibberish, so I want to break it down a bit more for you.

    The For Section

    For right now, let’s just completely forget about the Do portion of the command, and concentrate on the For section, shown below:

    for /F “tokens=2,3,4 delims=/ ” %%i in (‘date /t’)

    What this command is basically doing is importing specific tokens when the command DATE /T is executed. Of course this raises several questions, such as what does DATE /T do, and what in the world is a token?

    If you take a look at Figure A, you can see where I have manually executed the DATE /T command. As you can see in the figure, this command simply displays the day of the week, and then displays the month, day, and year in numerical format. As you may recall, we are using some of this information to create our filename, but we certainly can’t create a filename using this information in its raw form.


    Figure A: This is what it looks like when you manually enter the DATE /T command

    This is where the concept of tokens comes into play. Notice the word IN, just prior to the DATE /T command. This tells the For command that we are pulling information in from the DATE /T command. When we do that, Windows treats each individual piece of information as a token. In this case, there are four separate tokens that are being read in. Those tokens are:

    • Sun

    • 05

    • 11

    • 2008

    Now, you might have noticed that when I manually entered the DATE /T command, Windows displayed the actual date as one continuous string of information (05/11/2008), not as individual pieces of information (05, 11, 2008). The reason why it is possible to treat this string of text as three separate tokens is because we can specify a delimiter. A delimiter tells the command that any time it encounters a specific character, it should treat the text following that character as a separate token.

    If you look back at our original command, you will notice a portion of the command that looks like this:

    Delims=/%%i

    The first part of this command (delims=/) tells Windows that the delimiter is the / character. This is what breaks the string of text into three separate tokens. The %%i portion of the command assigns the token to a variable named %%I.

    Now take a look at the Tokens= portion of the command:

    tokens=2,3,4

    This portion of the command tells Windows to capture the second, third, and fourth tokens. In this case, the first token is Sun, which is the portion of the output that we aren’t interested in, so we are completely disregarding it. The second token is 05, and we are placing that value into a variable named %%I. Now you might have noticed that we are capturing two additional tokens (3 and 4), but we have not declared any more variables. The nice thing about this command is that we don’t have to. Windows treats %%I as a starting point. It will automatically use %%J as the variable to hold the value of token number 3, and %%K as the value to hold token number 4.

    OK, I know that this all probably seems a little abstract at best, so I wanted to demonstrate some of the concepts that I have been talking about. To help you to see what’s really going on, I have created the script below:

    @echo off
    for /F “tokens=2,3,4 delims=/ ” %%i in (‘date /t’) do set tkn2=%%i
    for /F “tokens=2,3,4 delims=/ ” %%i in (‘date /t’) do set tkn3=%%j
    for /F “tokens=2,3,4 delims=/ ” %%i in (‘date /t’) do set tkn4=%%k
    Echo Token number 2 = %tkn2%
    Echo Token number 3 = %tkn3%
    Echo Token number 4 = %tkn4%
    for /F “tokens=2,3,4 delims=/ ” %%i in (‘date /t’) do set datefile=%%i%%j%%k.log
    Echo The DATEFILE variable = %datefile%

    I mentioned earlier that the Do portion of the For command caused another command to be executed once the conditions in the For portion of the command had been met. In our original script, the Do portion of the command created a variable named Datefile that was made up of the values of the second, third, and fourth tokens. We then appended the .LOG extension to the end of the value so that we could use it as a filename.

    With that in mind, take a look at the sample script above. The first three commands are very similar to our original For command, except that rather than creating a filename, these commands dump the contents of the second, third, and fourth tokens into variables named TKN2, TKN3, and TKN4 respectively.

    The next three lines display the contents of these variables so that you can see what they really look like. Notice that any time you want to use the Echo command to display the contents of a variable, that variable must be surrounded by percentage signs on each end.

    The second to last command in the script above is our original command that compiles a filename based on the values of each token, and then appends the .LOG extension to the filename. As you can see in the command, the filename is stored in a variable named DATEFILE. The last line of the script above displays the contents of the DATEFILE variable for you to see.

    You can see what the script looks like when executed in Figure B.


    Figure B: This sample script shows how tokens and variables are being used

    Now, let’s go back to our original script, which is shown below:

    @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.

    As you can see, the second to the last line in this script calls the QUERY USER command, which is a terminal service specific scripting command. I  explained in the previous part of the article series that the greater than sign tells the script to dump the command’s output to a file. This greater than sign is then followed by %DATEFILE%, which is the variable containing the filename that we have compiled.

    Conclusion

    In this article, I explained how we used a sample script to create a date specific filename. In the next part of this article series, I will continue the discussion by showing you some more scripting techniques that you can use in a Terminal Service environment.

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

  • Writing Terminal Service Based Scripts (Part 2)
  • Writing Terminal Service Based Scripts (Part 3)
  • Writing Terminal Service Based Scripts (Part 4)
  • Writing Terminal Service Based Scripts (Part 5)
  • Writing Terminal Service Based Scripts (Part 6)
  • Writing Terminal Service Based Scripts (Part 7)
  • Writing Terminal Service Based Scripts (Part 9)
  • Writing Terminal Service Based Scripts (Part 10)
  • Writing Terminal Service Based Scripts (Part 11)
  • Writing Terminal Service Based Scripts (Part 12)
  • Writing Terminal Service Based Scripts (Part 13)
  • Writing Terminal Service Based Scripts (Part 14)
  • About The Author

    Leave a Comment

    Your email address will not be published. Required fields are marked *

    This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

    Scroll to Top