Creating daily log files

In the (wild) old days when the only thing we had to work with computers were the command line we use to store output of important processes to a (text)file so it can be viewed and worked on afterwards. Event today in a support and administration environment these skills are very handy.

The process of doing this was/is called ‘output direction’ or ‘pipe output to’ since the standard output ‘device’ was the console (command window). So taking the application output away from the console meant redirecting it to a ‘file’ – since in a sense the ‘console’ could also be seen as a file that gets stuff outputted to (a temporary one).

Anyway, say you want to save the output of a command line utility like ‘dir’ to a file – you would use the following:

dir *.txt > dir.txt

All this does is execute the normal ‘dir *.txt’ command but store the output to the file ‘dir.txt’. This only outputs the standard ‘console output’ to the text file. If the output file already exists it gets overwritten. Actually there are two types of ‘standard outputs’: 1 – the normal successful and 2 – the standard error output. Using only the ‘>’ operator is just a shortcut for ‘1>’ which only outputs the ‘standard successful’ output i.e. the previous example is the same as:

dir *.txt 1> dir.txt

The second type of output is the ‘standard error’ output. To get this output you use the ‘2>’ operator e.g.

Someexe.exe 2> errors.txt

If you want to combine the outputs – that is both normal and error outputs you can use this:

Someexe.exe > errors.txt 2>&1

Lets say you want to keep a history of all previous outputs – then you want to use the ‘append’ operator ‘>>’:

Someexe.exe >> output.txt

If the file ‘output.txt’ does not exist yet it will be created. Otherwise and new output will simply be appended. If you want to combine all of these – that is combine normal and error ouputs plus append to the output file you can use this:

Someexe.exe >> output.txt 2>&1

Now, say you want to use part of the date when creating, listing etc. files. Unfortunately this tip depends on the date-time format settings of the machine you are running it on. For my example I use the ‘yyyy/mm/dd‘ format. To get the year you can use the following:

set year=%date:~0,4%

To get the month you can use ‘%date:~5,2%’ or day ‘%date:~8,2%’ – again this only works for the ‘yyyy/mm/dd’ format. If your date-time format is different you have to change the ‘~x,y‘ parts where x is ‘from’ and y is ‘to’ character positions.

Now using the example above you can use:

dir %year%*.txt

to file all files with a name that start with the current year (e.g. 2012blablabla.txt)

You can use the same technique when creating output files, e.g.

dir *.txt > %year%List.txt

This way you can create daily log files e.g.

set year=%date:~0,4%
set month=%date:~5,2%
set day=%date:~8,2%
Someexe.exe >> %year%%month%%day%.log

Mission accomplished… for now.

Leave a Comment


NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>