batch-filepowershellwindows-server-2008windows-server-2008-r2archive

Writing a batch utility to archive files on Windows Server 2008 R2


I need to write a command line (PowerShell or DOS) utility that will archive all files created in between specified dates present in folder X and store them as a .zip package in the same folder.

This utility will be scheduled into windows scheduler where arguments like to and from dates, store location will be provided to it and it will run at specified duration for e.g. at 12:00 noon daily.

Is it possible to write it as a batch .bat file? Are there any built-in functionalities inside windows to zip files or I would need to use a third party program like 7-Zip etc for it.

I am not looking for any direction, perhaps a tutorial or something. Both a DOS based and a PowerShell based solution would work for me.


Solution

  • I would go for PowerShell.

    With PowerShell you can easyly build and compare dates with the Get-Date commandlet and comparison operators. You could try something like:

    $folderPath = 'C:\Temp'
    
    $date1 = Get-Date '2014-05-01'
    $date2 = Get-Date '2014-05-31'
    
    foreach( $item in (Get-ChildItem $folderPath) )
    {
        if( $item.LastWriteTime -ge $date1 -and $item.LastWriteTime -le $date2 )
        {
            # Compression step
        }
    }
    

    As for compression you have several options like stated in this question.

    And for the script parameters you can check this blog article.