Sometimes, more importantly than an application that can run, is an application that can ‘remember’. It is often the very purpose of an application to gather information from or about a user, but when environmental variables are only stored in system memory temporarily, where can you turn to for a long-term solution? That is where logging information would come into play. Logging information utilizes your computer’s hard memory, whereas environmental variables are generally stored within your computer’s RANDOM ACCESS MEMORY; or RAM.
This information is generally stored within a specific file, in a specific location, likely but not limited to a text (TXT) file. But how can you get a program to write a .TXT file all by itself. We’ll show you. Just sit down, and buckle up.
Priming a log file
It is important for you to declare all variables that will be needed by any log file in the future BEFORE declaring or ECHO-ing anything out to a log file. This means, make sure that all mathematical equations have been performed, variables declared or anything else that we’ll need to get that log file off of the ground.
To actually log the file out, we will be using our handy ECHO command. This command is universally known for displaying strings but is NOT widely known for its resourceful quiet functions.
Logging information
For our purposes in this article, we will log only the most basic information, but the truth is that there is no limit to what one can produce using this process. I have seen an entire web-server that self refreshes ALL programed within the confines of the Window’s shell environment. Also note that not just .TXT files are able to exported. The command shell has no software provisioning it against writing certain types of files, which means you can write any type of file using this process as long as it is composed linearly (which means it has to be written line by line in a row). To get started, the only extra expression that you will need to know (assuming that you know how to ECHO) is the gt; (greater than sign). This will instruct the shell that instead of ECHO-ing your statement to the display to write it to a given file. In order to write the second and furthermore lines we will need to double-stack our gt; gt;s. That simply tells the shell that this line should be produced in succession to the primary. To utilize this handy tool, simply type ECHO followed by the string, or statement that you would like written, the greater than sign, and the file in which you would like it written to. For example:
ECHO HELLO, WORLD gt; LOG.TXT
This command line will write the message, “HELLO,WORLD” within a file named LOG.TXT in the directory of your specification (or your location if there is no specification). Let’s examine a little more complex example.+
ECHO HELLO, WORLD! gt; LOG.TXT
ECHO HOW ARE YOU? gt; gt; LOG.TXT
This means that the message:
HELLO, WORLD!
HOW ARE YOU?
will be displayed within the LOG.TXT file. In this last example, I will demonstrate a bit more complex version of the former by producing an HTML file with the same process. Do keep in mind that the ^ (carrot) symbol is used to preface obscure characters for the shell.
ECHO ^ gt; TEST.HTML
ECHO ^ gt; gt; TEST.HTML
ECHO ^ gt; gt; TEST.HTML
ECHO ^ gt; gt;TEST.HTML
ECHO HELLO, WORLD! gt; gt; TEST.HTML
ECHO ^ gt; gt; TEST.HTML
ECHO ^ gt; gt; TEST.HTML
What we’ve done here is created a new HTML file from scratch. This file, although perhaps complex to look at, will display the simple message, “Hello, World!” in your Internet browser. Remember also that none of this information will be displayed publicly. It will just appear in some newly written file (unless you wanted to ECHO that the file has been written).
Incorporating variables
Here’s that word! Variables came up again! That’s right! You can incorporate variables with this process as well. Like I said earlier, just make sure that everything is done before hand, and not after. If the shell is supposed to use it, make sure that it’s done before any of this takes place. To incorporate these processes, simply add the %variable% in like so:
ECHO This is a variable %variable% gt; log.txt
A more complex example of this would be (for more information on variables read my article on VARIABLES):
set /p color= What color would you like the page to be?
set /p msg= What message would you like displayed in your site?
ECHO ^ gt; TEST.HTML
ECHO ^ gt; gt; TEST.HTML
ECHO ^ gt; gt; TEST.HTML
ECHO ^ gt; gt;TEST.HTML
ECHO ^ECHO %msg% gt; gt; TEST.HTML
ECHO ^ gt; gt; TEST.HTML
ECHO ^ gt; gt; TEST.HTML
ECHO Your website is done!
In other words, what happened here is that you collected data about specific user preferences, and then utilized that data in the new web page. Cool, huh?
Logging time and date
The last, but certainly not least important thing that we’ll go over on our logging journey here today is the time and date stamp. Don’t forget it, because it’s sort of the point. Isn’t it? Anyway, fortunately for you, %date% and %time% are environmental variables already set by the shell. Window’s resets them every ten-thousandth of a second in fact. So your job is tremendously easy there. Simply, type ECHO %date% or ECHO %time% (or both), followed by the greater than sign and the location of the new or edited file. Like this:
ECHO %date% %time% gt; log.txt
Keep in mind with the date and time stamp (as with every component of this process) that these files will not self-refresh. This means that if you would like to rewrite the file, that it is important to delete it THEN rewrite it. Otherwise, the shell will just continue to append the same file, leaving you with countless, unorganized information.
All of this is so easy and useful to incorporate into any program that there simply is no reason not to. Have fun with this and experiment, experiment, experiment! Also, make sure to remember to specify file locations, or you’ll have a SYSTEM32 folder full of this stuff! Keep practicing, have fun and HAPPY PROGRAMMING!
Thanks for reading, and don’t forget to continue on to learn more about command-line and batch process programming by referring to The Principles of Batch Process Program Development series. Proper development will be provided through wavemaker rad development application. All the variables will be provided equal importance through the agency with international coverage. a survey about the developments can be taken through the person.