{"id":2085,"date":"2017-11-01T20:49:16","date_gmt":"2017-11-01T20:49:16","guid":{"rendered":"http:\/\/blog.paranoidprofessor.com\/?p=2085"},"modified":"2018-06-13T19:37:16","modified_gmt":"2018-06-13T19:37:16","slug":"windows-batch-processing","status":"publish","type":"post","link":"https:\/\/blog.paranoidprofessor.com\/index.php\/2017\/11\/01\/windows-batch-processing\/","title":{"rendered":"windows batch processing"},"content":{"rendered":"<p>I really didn&#8217;t want to fool around with that little windows task that has been laying on my boss&#8217;s desk. It was planned that Robert would do it after the new year started but then he decided to quit. Now it has been given to me.<\/p>\n<p>The task is actually pretty simple, migrate that setup from the Hartmut&#8217;s old computer to a virtual machine. The task itself is just to automate the extracting of some data from MQ and writing to the file system with it&#8217;s original filename. The marketing guys will deal with the files after that.<\/p>\n<p>I would rather use cron and bash scripts but that isn&#8217;t an option on Windows server 2008, so instead I will get to use the Windows task scheduler and a few cmd scripts. \u00a0The Windows scheduler is just as powerful and is standard for that platform.<\/p>\n<h2>Task scheduler (taskschd.msc)<\/h2>\n<p>Most of the task scheduler is pretty easy to guess. \u00a0The main tricks is to use a user that has admin privileges and to select run whether or not the user is logged in &#8211; well assuming that is correct for your situation.<\/p>\n<p><a href=\"https:\/\/blog.paranoidprofessor.com\/wp-content\/uploads\/2017\/02\/task-scheduler1.png\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-2091\" src=\"https:\/\/blog.paranoidprofessor.com\/wp-content\/uploads\/2017\/02\/task-scheduler1.png\" alt=\"\" width=\"936\" height=\"691\" srcset=\"https:\/\/blog.paranoidprofessor.com\/wp-content\/uploads\/2017\/02\/task-scheduler1.png 936w, https:\/\/blog.paranoidprofessor.com\/wp-content\/uploads\/2017\/02\/task-scheduler1-300x221.png 300w, https:\/\/blog.paranoidprofessor.com\/wp-content\/uploads\/2017\/02\/task-scheduler1-768x567.png 768w\" sizes=\"(max-width: 936px) 100vw, 936px\" \/><\/a><\/p>\n<p><a href=\"https:\/\/blog.paranoidprofessor.com\/wp-content\/uploads\/2017\/02\/task-scheduler-general-1.png\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-2105\" src=\"https:\/\/blog.paranoidprofessor.com\/wp-content\/uploads\/2017\/02\/task-scheduler-general-1.png\" alt=\"\" width=\"651\" height=\"491\" srcset=\"https:\/\/blog.paranoidprofessor.com\/wp-content\/uploads\/2017\/02\/task-scheduler-general-1.png 651w, https:\/\/blog.paranoidprofessor.com\/wp-content\/uploads\/2017\/02\/task-scheduler-general-1-300x226.png 300w\" sizes=\"(max-width: 651px) 100vw, 651px\" \/><\/a><\/p>\n<div id=\"attachment_2093\" style=\"width: 661px\" class=\"wp-caption alignnone\"><a href=\"https:\/\/blog.paranoidprofessor.com\/wp-content\/uploads\/2017\/02\/task-scheduler-trigger.png\"><img aria-describedby=\"caption-attachment-2093\" decoding=\"async\" loading=\"lazy\" class=\"wp-image-2093 size-full\" src=\"https:\/\/blog.paranoidprofessor.com\/wp-content\/uploads\/2017\/02\/task-scheduler-trigger.png\" width=\"651\" height=\"490\" srcset=\"https:\/\/blog.paranoidprofessor.com\/wp-content\/uploads\/2017\/02\/task-scheduler-trigger.png 651w, https:\/\/blog.paranoidprofessor.com\/wp-content\/uploads\/2017\/02\/task-scheduler-trigger-300x226.png 300w\" sizes=\"(max-width: 651px) 100vw, 651px\" \/><\/a><p id=\"caption-attachment-2093\" class=\"wp-caption-text\">Triggers<\/p><\/div>\n<div id=\"attachment_2094\" style=\"width: 620px\" class=\"wp-caption alignnone\"><a href=\"https:\/\/blog.paranoidprofessor.com\/wp-content\/uploads\/2017\/02\/task-scheduler-trigger2.png\"><img aria-describedby=\"caption-attachment-2094\" decoding=\"async\" loading=\"lazy\" class=\"wp-image-2094 size-full\" src=\"https:\/\/blog.paranoidprofessor.com\/wp-content\/uploads\/2017\/02\/task-scheduler-trigger2.png\" width=\"610\" height=\"528\" srcset=\"https:\/\/blog.paranoidprofessor.com\/wp-content\/uploads\/2017\/02\/task-scheduler-trigger2.png 610w, https:\/\/blog.paranoidprofessor.com\/wp-content\/uploads\/2017\/02\/task-scheduler-trigger2-300x260.png 300w\" sizes=\"(max-width: 610px) 100vw, 610px\" \/><\/a><p id=\"caption-attachment-2094\" class=\"wp-caption-text\">New trigger<\/p><\/div>\n<div id=\"attachment_2095\" style=\"width: 658px\" class=\"wp-caption alignnone\"><a href=\"https:\/\/blog.paranoidprofessor.com\/wp-content\/uploads\/2017\/02\/task-scheduler5-1.png\"><img aria-describedby=\"caption-attachment-2095\" decoding=\"async\" loading=\"lazy\" class=\"wp-image-2095 size-full\" src=\"https:\/\/blog.paranoidprofessor.com\/wp-content\/uploads\/2017\/02\/task-scheduler5-1.png\" width=\"648\" height=\"489\" srcset=\"https:\/\/blog.paranoidprofessor.com\/wp-content\/uploads\/2017\/02\/task-scheduler5-1.png 648w, https:\/\/blog.paranoidprofessor.com\/wp-content\/uploads\/2017\/02\/task-scheduler5-1-300x226.png 300w\" sizes=\"(max-width: 648px) 100vw, 648px\" \/><\/a><p id=\"caption-attachment-2095\" class=\"wp-caption-text\">Action<\/p><\/div>\n<div id=\"attachment_2096\" style=\"width: 477px\" class=\"wp-caption alignnone\"><a href=\"https:\/\/blog.paranoidprofessor.com\/wp-content\/uploads\/2017\/02\/task-scheduler5-2.png\"><img aria-describedby=\"caption-attachment-2096\" decoding=\"async\" loading=\"lazy\" class=\"wp-image-2096 size-full\" src=\"https:\/\/blog.paranoidprofessor.com\/wp-content\/uploads\/2017\/02\/task-scheduler5-2.png\" width=\"467\" height=\"512\" srcset=\"https:\/\/blog.paranoidprofessor.com\/wp-content\/uploads\/2017\/02\/task-scheduler5-2.png 467w, https:\/\/blog.paranoidprofessor.com\/wp-content\/uploads\/2017\/02\/task-scheduler5-2-274x300.png 274w\" sizes=\"(max-width: 467px) 100vw, 467px\" \/><\/a><p id=\"caption-attachment-2096\" class=\"wp-caption-text\">New action<\/p><\/div>\n<p><a href=\"https:\/\/blog.paranoidprofessor.com\/wp-content\/uploads\/2017\/02\/task-scheduler6.png\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-2098 size-medium\" src=\"https:\/\/blog.paranoidprofessor.com\/wp-content\/uploads\/2017\/02\/task-scheduler6-300x228.png\" width=\"300\" height=\"228\" srcset=\"https:\/\/blog.paranoidprofessor.com\/wp-content\/uploads\/2017\/02\/task-scheduler6-300x228.png 300w, https:\/\/blog.paranoidprofessor.com\/wp-content\/uploads\/2017\/02\/task-scheduler6.png 648w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a>\u00a0 \u00a0 \u00a0<a href=\"https:\/\/blog.paranoidprofessor.com\/wp-content\/uploads\/2017\/02\/task-scheduler7.png\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-2099 size-medium\" src=\"https:\/\/blog.paranoidprofessor.com\/wp-content\/uploads\/2017\/02\/task-scheduler7-300x227.png\" width=\"300\" height=\"227\" srcset=\"https:\/\/blog.paranoidprofessor.com\/wp-content\/uploads\/2017\/02\/task-scheduler7-300x227.png 300w, https:\/\/blog.paranoidprofessor.com\/wp-content\/uploads\/2017\/02\/task-scheduler7.png 652w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>I did my testing on windows 7 and the task manager works just fine if you have enough privileges.<\/p>\n<p>&nbsp;<\/p>\n<h2>Cmd Shell<\/h2>\n<p>My script doesn&#8217;t need to do more than setup a class path and call our Java program. \u00a0The output will be written to the current directory and pretty much everyone will be happy.<\/p>\n<p>Well, almost everyone. \u00a0Not all that long ago one of our Unix test environments filled up with junk and I received a lot of status emails. \u00a0I don&#8217;t want something similar to happen on windows, and if it does, I don&#8217;t want it to be because of our log files.<\/p>\n<p>In Unix or Linux I would be using the find command to gather up a list of files to delete, while on windows the cmd shelll has the &#8220;forfiles&#8221; command.<\/p>\n<p>Just like the Unix find command, you can gather up the files by regular expression and\/or age and then run a command for each file. This command will basically loop over all files in the given directory that matches the file specification or date.<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\n@echo off\r\nrem deal with old log files\r\nset RUNLOGDIR=c:\\logdirectory\\runlogdir\r\n \r\nset DELCMD=del\r\nset LOGFILES=*.log\r\n \r\ncd %RUNLOGDIR%\r\nforfiles \/p %RUNLOGDIR% \/m %LOGFILES% \/c &quot;cmd \/c %DELCMD% @path&quot; \/d -90\r\n<\/pre>\n<p>During testing you can replace the del command on line 5 with echo to see what would be deleted. \u00a0This was fairly convenient during testing.<\/p>\n<p>I still am not a fan of Windows but it is obvious that they do have a lot of the same type of functionality as their Unix brethren.<\/p>\n<p>&nbsp;<\/p>\n<h2>Powershell<\/h2>\n<p>Windows also has the powershell interpreter which can also be used to create batch tasks. This might be a better choice than the old DOS cmd shell mainly because it is a true programming language that has a lot of functionality to allow easy access to the operating system functions.<\/p>\n<p>It is just as easy to create very reusable function to delete files older than a given number of days.<\/p>\n<pre># delete old files \r\nFunction DeleteFiles([string]$DelPath,[string]$regexpr,[int]days) \r\n{\r\n        # files older than xx days\r\n        $cutoffdate = (Get-Date).AddDays(-days)\r\n\r\n        $filesToDelete = Get-ChildItem -Path \"$DelPath\" -Filter $regexpr | Where-Object {$_.LastWriteTime -lt $checkdate -and !$_.psiscontainer}\r\n        ForEach($file in $filesToDelete) \r\n        {\r\n                Remove-Item  $DelPath$file -force\r\n        }\r\n}<\/pre>\n<p>This is a very nice solution that will be essentially self documenting when you add that function call to the rest of your powershell script.<\/p>\n<blockquote><p><em>ie<\/em><\/p>\n<p><em>DeleteFiles c:\\temp\\logfiles *.log 15<\/em><\/p><\/blockquote>\n<p>There is a lot more information about powershell programming on the internet as well as are couple of my own blog entries<\/p>\n<blockquote><p>Windows scripting sucks or does it?<\/p>\n<p>Windows powershell scripting<\/p><\/blockquote>\n<h2>Reference information<\/h2>\n<h3>Forfiles syntax<\/h3>\n<pre>FORFILES [\/P pathname] [\/M searchmask] [\/S]\r\n         [\/C command] [\/D [+ | -] {dd\/MM\/yyyy | dd}]\r\n\r\nDescription:\r\n    Selects a file (or set of files) and executes a\r\n    command on that file. This is helpful for batch jobs.\r\n\r\nParameter List:\r\n    \/P    pathname      Indicates the path to start searching.\r\n                        The default folder is the current working\r\n                        directory (.).\r\n\r\n    \/M    searchmask    Searches files according to a searchmask.\r\n                        The default searchmask is '*' .\r\n\r\n    \/S                  Instructs forfiles to recurse into\r\n                        subdirectories. Like \"DIR \/S\".\r\n\r\n    \/C    command       Indicates the command to execute for each file.\r\n                        Command strings should be wrapped in double\r\n                        quotes.\r\n\r\n                        The default command is \"cmd \/c echo @file\".\r\n\r\n                        The following variables can be used in the\r\n                        command string:\r\n                        @file    - returns the name of the file.\r\n                        @fname   - returns the file name without\r\n                                   extension.\r\n                        @ext     - returns only the extension of the\r\n                                   file.\r\n                        @path    - returns the full path of the file.\r\n                        @relpath - returns the relative path of the\r\n                                   file.\r\n                        @isdir   - returns \"TRUE\" if a file type is\r\n                                   a directory, and \"FALSE\" for files.\r\n                        @fsize   - returns the size of the file in\r\n                                   bytes.\r\n                        @fdate   - returns the last modified date of the\r\n                                   file.\r\n                        @ftime   - returns the last modified time of the\r\n                                   file.\r\n\r\n                        To include special characters in the command\r\n                        line, use the hexadecimal code for the character\r\n                        in 0xHH format (ex. 0x09 for tab). Internal\r\n                        CMD.exe commands should be preceded with\r\n                        \"cmd \/c\".\r\n\r\n    \/D    date          Selects files with a last modified date greater\r\n                        than or equal to (+), or less than or equal to\r\n                        (-), the specified date using the\r\n                        \"dd\/MM\/yyyy\" format; or selects files with a\r\n                        last modified date greater than or equal to (+)\r\n                        the current date plus \"dd\" days, or less than or\r\n                        equal to (-) the current date minus \"dd\" days. A\r\n                        valid \"dd\" number of days can be any number in\r\n                        the range of 0 - 32768.\r\n                        \"+\" is taken as default sign if not specified.\r\n\r\n    \/?                  Displays this help message.\r\n\r\nExamples:\r\n    FORFILES \/?\r\n    FORFILES\r\n    FORFILES \/P C:\\WINDOWS \/S \/M DNS*.*\r\n    FORFILES \/S \/M *.txt \/C \"cmd \/c type @file | more\"\r\n    FORFILES \/P C:\\ \/S \/M *.bat\r\n    FORFILES \/D -30 \/M *.exe\r\n             \/C \"cmd \/c echo @path 0x09 was changed 30 days ago\"\r\n    FORFILES \/D 01\/01\/2001\r\n             \/C \"cmd \/c echo @fname is new since Jan 1st 2001\"\r\n    FORFILES \/D +14\/2\/2017 \/C \"cmd \/c echo @fname is new today\"\r\n    FORFILES \/M *.exe \/D +1\r\n    FORFILES \/S \/M *.doc \/C \"cmd \/c echo @fsize\"\r\n    FORFILES \/M *.txt \/C \"cmd \/c if @isdir==FALSE notepad.exe @file\"\r\n\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I really didn&#8217;t want to fool around with that little windows task that has been laying on my boss&#8217;s desk. It was planned that Robert would do it after the new year started but then he decided to quit. Now &hellip; <a href=\"https:\/\/blog.paranoidprofessor.com\/index.php\/2017\/11\/01\/windows-batch-processing\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[20],"tags":[39,71],"_links":{"self":[{"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/posts\/2085"}],"collection":[{"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/comments?post=2085"}],"version-history":[{"count":21,"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/posts\/2085\/revisions"}],"predecessor-version":[{"id":2900,"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/posts\/2085\/revisions\/2900"}],"wp:attachment":[{"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/media?parent=2085"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/categories?post=2085"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/tags?post=2085"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}