{"id":318,"date":"2006-11-20T06:02:19","date_gmt":"2006-11-20T14:02:19","guid":{"rendered":"http:\/\/testblog.sapien.com\/index.php\/2006\/11\/20\/create-time-stamped-logs\/"},"modified":"2006-11-20T06:02:19","modified_gmt":"2006-11-20T14:02:19","slug":"create-time-stamped-logs","status":"publish","type":"post","link":"https:\/\/dev.sapien.com\/blog\/2006\/11\/20\/create-time-stamped-logs\/","title":{"rendered":"Create Time Stamped Logs"},"content":{"rendered":"<p>In my VBScript work I often create log files, either for debugging or as a main component of the script. I&#8217;m fond of a format like this:<\/p>\n<p>6\/15\/2000 8:49:29 AM &#8211; Administrator Administrator from LISBON<br \/>6\/15\/2000 8:49:29 AM &#8211; Account Change Aborted.<\/p>\n<p>In a batch file I could never figure out how to get this same type of result. You can use commands like date \/t and time \/t to get a date stamp or time stamp but combining the two on one line was practically impossible. Until now.&nbsp; The hack I came up with was to use the FOR command to pass the result of the date \/t and time \/t commands. Take a look at this expression:<\/p>\n<p><span style=\"font-family: Courier New,Courier,mono;\">for \/f &#8220;tokens=*&#8221; %i in (&#8216;date \/t&#8217;) do @for \/f &#8220;tokens=*&#8221; %j in (&#8216;time \/t&#8217;) do @echo %i%j &#8220;Logging has begun&#8221;<\/span><\/p>\n<p>Normally you think of using FOR \/f to process a list but there&#8217;s no rule saying the list can&#8217;t consist of only one item.&nbsp; That&#8217;s what is happening here. The first FOR command produces the same result as simply running date \/t. The advantage here is that I can save the output as %i and also call another FOR command to do the same thing. I can then use the DO portion of the second FOR command to create my log file entry.&nbsp; Here&#8217;s the result you get from this command:<\/p>\n<p>Mon 11\/20\/2006 08:51 AM &#8220;Logging has begun&#8221;<\/p>\n<p>I can&#8217;t get the seconds component like I can in VBScript, but I can live with that. <\/p>\n<p>The next step is to put this is a function of sorts that I can use in my batch files. Here is a sample script that uses this routine:<\/p>\n<p><span style=\"font-family: Courier New,Courier,mono;\">@echo off<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">::LogEntryDemo.bat<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">REM Jeffery Hicks&nbsp; jhicks@sapien.com<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">REM This script doesn&#8217;t really do much other than demonstrate<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">REM how to use the LogEntry routine to create a <\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">REM log file with a timestamped entry for each line.<\/span><br style=\"font-family: Courier New,Courier,mono;\"><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">REM define path to log file<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">set myLog=e:\\temp\\log.txt<\/span><br style=\"font-family: Courier New,Courier,mono;\"><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">REM Delete the logfile if it already exists.<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">if exist %myLog% del %myLog% &gt;NUL<\/span><br style=\"font-family: Courier New,Courier,mono;\"><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">Echo Working&#8230;<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">REM When sending something to the log routine, enclose the<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">REM message in quotes.<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">Call :LogEntry &#8220;Starting %0&#8221;<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">Call :LogEntry &#8220;Running DIR&#8221;<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">DIR %windir%\\*.* \/s &gt;NUL<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">Call :LogEntry &#8220;Finished DIR&#8221;<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">Call :LogEntry &#8220;Sleeping for 30 seconds&#8221;<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">Sleep 30<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">Call :logEntry &#8220;Getting NETSTAT information&#8221;<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">REM Sending results of a NETSTAT command to the log. Notice<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">REM I&#8217;m enclosing the output in quotes. Otherwise, only the<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">REM first part of the output would be recorded.<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">for \/f &#8220;tokens=*&#8221; %%t in (&#8216;netstat ^|find \/i &#8220;TCP&#8221;&#8216;) do @Call :LogEntry &#8220;%%t&#8221;<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">Call :LogEntry &#8220;Finishing logging and opening %myLog%&#8221;<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">REM Display the log<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">start Notepad %MyLog%<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">GOTO :EOF<\/span><br style=\"font-family: Courier New,Courier,mono;\"><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">:LogEntry<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">REM Output will be like: Wed 11\/15\/2006 05:27 PM &#8220;Starting LogEntryDemo.bat&#8221; <\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">for \/f &#8220;tokens=*&#8221; %%i in (&#8216;date \/t&#8217;) do @for \/f &#8220;tokens=*&#8221; %%j in (&#8216;time \/t&#8217;) do @echo %%i%%j %1 &gt;&gt;%myLog%<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">GOTO :EOF<\/span><br style=\"font-family: Courier New,Courier,mono;\"><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">:EOF<\/span><\/p>\n<p>Whenever I want to log something I call <span style=\"font-family: Courier New,Courier,mono;\">:LogEntry <\/span>and pass a parameter for whatever message I want logged. The value must be enclosed in quotes so it can be treated as a single entry.&nbsp; Again, a compromise that I can live with.<\/p>\n<p>Let me know what you think of this solution.<\/p>\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In my VBScript work I often create log files, either for debugging or as a main component of the script. In a batch file I could never figure out how to get this same type of result. You can use commands like date \/t and time \/t to get a date stamp or time stamp but combining the two on one line was practically impossible. Until now.<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[18,2],"tags":[],"class_list":["post-318","post","type-post","status-publish","format-standard","hentry","category-batchcmd","category-general"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/318","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/comments?post=318"}],"version-history":[{"count":0,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/318\/revisions"}],"wp:attachment":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/media?parent=318"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/categories?post=318"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/tags?post=318"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}