windows batch processing

I really didn’t want to fool around with that little windows task that has been laying on my boss’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.

The task is actually pretty simple, migrate that setup from the Hartmut’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’s original filename. The marketing guys will deal with the files after that.

I would rather use cron and bash scripts but that isn’t an option on Windows server 2008, so instead I will get to use the Windows task scheduler and a few cmd scripts.  The Windows scheduler is just as powerful and is standard for that platform.

Task scheduler (taskschd.msc)

Most of the task scheduler is pretty easy to guess.  The main tricks is to use a user that has admin privileges and to select run whether or not the user is logged in – well assuming that is correct for your situation.

Triggers

New trigger

Action

New action

     

I did my testing on windows 7 and the task manager works just fine if you have enough privileges.

 

Cmd Shell

My script doesn’t need to do more than setup a class path and call our Java program.  The output will be written to the current directory and pretty much everyone will be happy.

Well, almost everyone.  Not all that long ago one of our Unix test environments filled up with junk and I received a lot of status emails.  I don’t want something similar to happen on windows, and if it does, I don’t want it to be because of our log files.

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 “forfiles” command.

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.

@echo off
rem deal with old log files
set RUNLOGDIR=c:\logdirectory\runlogdir
 
set DELCMD=del
set LOGFILES=*.log
 
cd %RUNLOGDIR%
forfiles /p %RUNLOGDIR% /m %LOGFILES% /c "cmd /c %DELCMD% @path" /d -90

During testing you can replace the del command on line 5 with echo to see what would be deleted.  This was fairly convenient during testing.

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.

 

Powershell

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.

It is just as easy to create very reusable function to delete files older than a given number of days.

# delete old files 
Function DeleteFiles([string]$DelPath,[string]$regexpr,[int]days) 
{
        # files older than xx days
        $cutoffdate = (Get-Date).AddDays(-days)

        $filesToDelete = Get-ChildItem -Path "$DelPath" -Filter $regexpr | Where-Object {$_.LastWriteTime -lt $checkdate -and !$_.psiscontainer}
        ForEach($file in $filesToDelete) 
        {
                Remove-Item  $DelPath$file -force
        }
}

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.

ie

DeleteFiles c:\temp\logfiles *.log 15

There is a lot more information about powershell programming on the internet as well as are couple of my own blog entries

Windows scripting sucks or does it?

Windows powershell scripting

Reference information

Forfiles syntax

FORFILES [/P pathname] [/M searchmask] [/S]
         [/C command] [/D [+ | -] {dd/MM/yyyy | dd}]

Description:
    Selects a file (or set of files) and executes a
    command on that file. This is helpful for batch jobs.

Parameter List:
    /P    pathname      Indicates the path to start searching.
                        The default folder is the current working
                        directory (.).

    /M    searchmask    Searches files according to a searchmask.
                        The default searchmask is '*' .

    /S                  Instructs forfiles to recurse into
                        subdirectories. Like "DIR /S".

    /C    command       Indicates the command to execute for each file.
                        Command strings should be wrapped in double
                        quotes.

                        The default command is "cmd /c echo @file".

                        The following variables can be used in the
                        command string:
                        @file    - returns the name of the file.
                        @fname   - returns the file name without
                                   extension.
                        @ext     - returns only the extension of the
                                   file.
                        @path    - returns the full path of the file.
                        @relpath - returns the relative path of the
                                   file.
                        @isdir   - returns "TRUE" if a file type is
                                   a directory, and "FALSE" for files.
                        @fsize   - returns the size of the file in
                                   bytes.
                        @fdate   - returns the last modified date of the
                                   file.
                        @ftime   - returns the last modified time of the
                                   file.

                        To include special characters in the command
                        line, use the hexadecimal code for the character
                        in 0xHH format (ex. 0x09 for tab). Internal
                        CMD.exe commands should be preceded with
                        "cmd /c".

    /D    date          Selects files with a last modified date greater
                        than or equal to (+), or less than or equal to
                        (-), the specified date using the
                        "dd/MM/yyyy" format; or selects files with a
                        last modified date greater than or equal to (+)
                        the current date plus "dd" days, or less than or
                        equal to (-) the current date minus "dd" days. A
                        valid "dd" number of days can be any number in
                        the range of 0 - 32768.
                        "+" is taken as default sign if not specified.

    /?                  Displays this help message.

Examples:
    FORFILES /?
    FORFILES
    FORFILES /P C:\WINDOWS /S /M DNS*.*
    FORFILES /S /M *.txt /C "cmd /c type @file | more"
    FORFILES /P C:\ /S /M *.bat
    FORFILES /D -30 /M *.exe
             /C "cmd /c echo @path 0x09 was changed 30 days ago"
    FORFILES /D 01/01/2001
             /C "cmd /c echo @fname is new since Jan 1st 2001"
    FORFILES /D +14/2/2017 /C "cmd /c echo @fname is new today"
    FORFILES /M *.exe /D +1
    FORFILES /S /M *.doc /C "cmd /c echo @fsize"
    FORFILES /M *.txt /C "cmd /c if @isdir==FALSE notepad.exe @file"

This entry was posted in programming and tagged , . Bookmark the permalink.