{"id":691,"date":"2012-01-13T09:44:25","date_gmt":"2012-01-13T07:44:25","guid":{"rendered":"http:\/\/hen.co.za\/blog\/?p=691"},"modified":"2013-10-29T09:25:54","modified_gmt":"2013-10-29T07:25:54","slug":"creating-daily-log-files","status":"publish","type":"post","link":"https:\/\/hen.co.za\/blog\/2012\/01\/creating-daily-log-files\/","title":{"rendered":"Creating daily log files"},"content":{"rendered":"<p>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.<\/p>\n<p>The process of doing this was\/is called &#8216;output direction&#8217; or &#8216;pipe output to&#8217; since the standard output &#8216;device&#8217; was the console (command window). So taking the application output away from the console meant redirecting it to a &#8216;file&#8217; &#8211; since in a sense the &#8216;console&#8217; could also be seen as a file that gets stuff outputted to (a temporary one).<\/p>\n<p>Anyway, say you want to save the output of a command line utility like &#8216;dir&#8217; to a file &#8211; you would use the following:<\/p>\n<blockquote><p><strong>dir<\/strong> *.txt <strong>&gt;<\/strong> dir.txt<\/p><\/blockquote>\n<p>All this does is execute the normal &#8216;dir *.txt&#8217; command but store the output to the file &#8216;dir.txt&#8217;. This only outputs the standard &#8216;console output&#8217; to the text file. If the output file already exists it gets overwritten. Actually there are two types of &#8216;standard outputs&#8217;: 1 &#8211; the normal successful and 2 &#8211; the standard error output. Using only the &#8216;&gt;&#8217; operator is just a shortcut for &#8216;1&gt;&#8217; which only outputs the &#8216;standard successful&#8217; output i.e. the previous example is the same as:<\/p>\n<blockquote><p><strong>dir<\/strong> *.txt 1<strong>&gt;<\/strong> dir.txt<\/p><\/blockquote>\n<p>The second type of output is the &#8216;standard error&#8217; output. To get this output you use the &#8216;2&gt;&#8217; operator e.g.<\/p>\n<blockquote><p><strong>Someexe.exe<\/strong> <strong>2&gt;<\/strong> errors.txt<\/p><\/blockquote>\n<p>If you want to combine the outputs &#8211; that is both normal and error outputs you can use this:<\/p>\n<blockquote><p><strong>Someexe.exe<\/strong> <strong>&gt;<\/strong> errors.txt <strong>2&gt;&amp;1<\/strong><\/p><\/blockquote>\n<p>Lets say you want to keep a history of all previous outputs &#8211; then you want to use the &#8216;append&#8217; operator &#8216;&gt;&gt;&#8217;:<\/p>\n<blockquote><p><strong>Someexe.exe<\/strong> <strong>&gt;&gt;<\/strong> output.txt<\/p><\/blockquote>\n<p>If the file &#8216;output.txt&#8217; does not exist yet it will be created. Otherwise and new output will simply be appended. If you want to combine all of these &#8211; that is combine normal and error ouputs plus append to the output file you can use this:<\/p>\n<blockquote><p><strong>Someexe.exe<\/strong> <strong>&gt;&gt;<\/strong> output.txt <strong>2&gt;&amp;1<\/strong><\/p><\/blockquote>\n<p>Now, say you want to use part of the date when creating, listing etc. files. Unfortunately this tip depends on the <em>date-time format<\/em> settings of the machine you are running it on. For my example I use the &#8216;<em>yyyy\/mm\/dd<\/em>&#8216; format. To get the year you can use the following:<\/p>\n<blockquote><p>set <span style=\"color: #0000ff;\">year<\/span>=%<span style=\"color: #0000ff;\">date<\/span>:~0,4%<\/p><\/blockquote>\n<p>To get the month you can use &#8216;%date:~5,2%&#8217; or day &#8216;%date:~8,2%&#8217; &#8211; again this only works for the &#8216;yyyy\/mm\/dd&#8217; format. If your date-time format is different you have to change the &#8216;<strong>~x,y<\/strong>&#8216; parts where x is &#8216;from&#8217; and y is &#8216;to&#8217; character positions.<\/p>\n<p>Now using the example above you can use:<\/p>\n<blockquote><p><strong>dir<\/strong> %<span style=\"color: #0000ff;\">year<\/span>%*.txt<\/p><\/blockquote>\n<p>to file all files with a name that start with the current year (e.g. 2012blablabla.txt)<\/p>\n<p>You can use the same technique when creating output files, e.g.<\/p>\n<blockquote><p><strong>dir<\/strong> *.txt &gt; %<span style=\"color: #0000ff;\">year<\/span>%List.txt<\/p><\/blockquote>\n<p>This way you can create daily log files e.g.<\/p>\n<blockquote><p><strong>set<\/strong> <span style=\"color: #0000ff;\">year<\/span>=%<span style=\"color: #0000ff;\">date<\/span>:~0,4%<br \/>\n<strong>set<\/strong> <span style=\"color: #0000ff;\">month<\/span>=%<span style=\"color: #0000ff;\">date<\/span>:~5,2%<br \/>\n<strong>set<\/strong> <span style=\"color: #0000ff;\">day<\/span>=%<span style=\"color: #0000ff;\">date<\/span>:~8,2%<br \/>\n<strong> Someexe.exe<\/strong> <strong>&gt;&gt;<\/strong> %<span style=\"color: #0000ff;\">year<\/span>%%<span style=\"color: #0000ff;\">month<\/span>%%<span style=\"color: #0000ff;\">day<\/span>%.log<\/p><\/blockquote>\n<p>Mission accomplished&#8230; for now.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 &hellip;<\/p>\n<p class=\"read-more\"><a href=\"https:\/\/hen.co.za\/blog\/2012\/01\/creating-daily-log-files\/\">Read more &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16,14],"tags":[113,111,56,270,112,114,45,24],"class_list":["post-691","post","type-post","status-publish","format-standard","hentry","category-reference","category-technology","tag-batch-files","tag-command-line","tag-console","tag-date-formatting","tag-dos","tag-files","tag-technology-2","tag-tip"],"_links":{"self":[{"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/posts\/691","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/comments?post=691"}],"version-history":[{"count":3,"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/posts\/691\/revisions"}],"predecessor-version":[{"id":694,"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/posts\/691\/revisions\/694"}],"wp:attachment":[{"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/media?parent=691"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/categories?post=691"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/tags?post=691"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}