1

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.

halfer
  • 19,824
  • 17
  • 99
  • 186
Maven
  • 14,587
  • 42
  • 113
  • 174

3 Answers3

2

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.

Community
  • 1
  • 1
Chris
  • 935
  • 3
  • 10
  • 21
  • If you want it to be portable across regions you can get the local timezone offset with this code `$tzOffset = Get-Date -UFormat '%Z'` and then initialize $date1 and $date2 this way `$date1 = Get-Date "2014-05-01 10:00:00 $tzOffset"` – Chris Jun 01 '14 at 12:59
  • Assuming you timezone offset is +02, just do it this way `$date2 = Get-Date '2014-05-31 +02'`, it will use the `00:00:00` time which is the beginning of the day. – Chris Jun 01 '14 at 13:14
  • The problem is that it is not between the dates. It is from 00:00:00 of the first date to 00:00:00 of the start of the last day. The last day is not included. It should either be between the dates as asked, or to make them `both` inclusive. – foxidrive Jun 01 '14 at 13:39
  • Then use the `-lt` operator instead `-le` – Chris Jun 01 '14 at 13:58
  • That doesn't make it inclusive or exclusive of the two dates, which is the reasonable way to approach this task. – foxidrive Jun 01 '14 at 14:01
  • Don't you see? If you use `-lt` it will include the first day of the range and exclude the last day of the range. In case you missed it in the flurry of exchanges, the `$date1 and $date2` are already in local time. No need to use UTC conversion. The current issue is that `$date2` needs `23:59:59` added to it, to make the range inclusive. Or add `23:59:59` to `$date1` to make the range exclusive of the two supplied dates. – foxidrive Jun 02 '14 at 07:29
  • I think I see what you mean. If I wanted `$date2` to be inclusive I would use the following test expression `$item.LastWriteTime -lt $date2.AddDays(1)`. – Chris Jun 03 '14 at 15:37
  • Yes, and thank you for the syntax - it works well. One final issue is that it includes folder names. Can they be excluded? – foxidrive Jun 04 '14 at 14:03
  • You're welcome. You can do this by replacing `Get-ChildItem` with `Get-ChildItem -File` or with PowerShell 2.0 you can replace it with `Get-ChildItem | Where-Object {$_.PSIsContainer -eq $false}` – Chris Jun 04 '14 at 14:16
  • Thank you - it works well. I'll post a batch script wrapped around your work. – foxidrive Jun 04 '14 at 14:38
0

Here is a batch file that builds on Cristophe C's powershell script

It takes a folder and two dates on the command line and writes the last-modified filenames from date1 to date2 into a text file called daterange.txt

Some code can be added to zip up the files if further details are added to the question.

@echo off
if "%~3"=="" (
echo "%~0" "c:\folder" yyyy-mm-dd1 yyyy-mm-dd2
echo(
echo( This returns last-modified files in the folder from the two dates inclusive
echo( and puts them in a file called "daterange.txt"
echo(
echo( Requires Powershell V3+ (Powershell scripting also needs to be enabled^)
echo(
pause
goto :EOF
) 

set "file=%temp%\psdaterange.ps1"

(
echo( $folderPath = '%~1\'
echo( 
echo( $date1 = Get-Date '%~2'
echo( $date2 = Get-Date '%~3'
echo( 
echo( foreach( $item in (Get-ChildItem -file $folderPath^) ^)
echo( {
echo( if( $item.LastWriteTime -ge $date1 -and $item.LastWriteTime -lt $date2.AddDays(1^) ^)
echo(     {
echo(         Write-Host $folderPath$item
echo(     }
echo( }
) >"%file%"

powershell "%file%" > "daterange.txt"
del "%file%"
foxidrive
  • 40,353
  • 10
  • 53
  • 68
0

THere is Compress-Archive utility available in powershell 5.0.

# Create a zip file with the contents of C:\Stuff\
Compress-Archive -Path C:\Stuff -DestinationPath archive.zip

# Add more files to the zip file
# (Existing files in the zip file with the same name are replaced)
Compress-Archive -Path C:\OtherStuff\*.txt -Update -DestinationPath archive.zip

# Extract the zip file to C:\Destination\
Expand-Archive -Path archive.zip -DestinationPath C:\Destination

See this answer for more details: How to create a zip archive with PowerShell?

Community
  • 1
  • 1
Sabyasachi
  • 411
  • 4
  • 9
  • 21