Solved How To Loop By Month?

November 10, 2014 at 07:02:00
Specs: Windows 7
Hi. I have a huge folder to zip (40gigs) and the output has to be compressed files BY MONTH. How do you *loop by month* with batch files and apply the proper command every time, going back several years (say, Jan 2004)?

Thanks a million.

See More: How To Loop By Month?

Report •

November 10, 2014 at 19:38:22
No enough detail, but I guess you mean the script needs to find files of a month, ie JAN.

If so, the next question is:

Do the files have the month in their names?
Or does the bat need to work out file date?

Either way, we will need to know the naming convention.

M2 Golden-Triangle

Report •

November 11, 2014 at 07:36:45
Actually, regardless of the naming convention, I need to loop one of two ways: By a certain time frame, i.e., a month or two weeks or whatever, or by zip file size. The files can't be more than 100MB; that said, I don't just want to split one big file using 7-zip or so; this is so because the naming of the files needs to be in particular way, and not .001, .002, or what not. I just need to know how to increment time in a loop, or how to loop through a file structure, add to a zip file, and check the zip file size before adding to it or, if the file is too big, creating another zip file.

Report •

November 21, 2014 at 14:04:34
✔ Best Answer
Consider the following PowerShell script. Note I've tried to limit myself to v2, since your system is listed as Win7, and most people haven't upgraded to PS v3 or v4. This was done on v3, though, so it still might need a little tweaking.
$sourceDir = "."

$files = @{}
dir $sourceDir | 
 foreach { $files[$_.LastWriteTime.ToString('yyyy.MM')] += , $_ }

function checkFile {
  try {
    Get-Content -TotalCount 1 -Encoding Byte -Path $filePath |
  } catch {
    return $false
  return $true
$shell = New-Object -ComObject Shell.Application

foreach ($f in $files.GetEnumerator()) {
  $zipName = Join-Path (Resolve-Path $sourceDir) ($f.Name + ".zip")
  Set-Content $zipName -Encoding Byte -Value ([byte[]](80, 75, 5, 6 + ( , 0 * 18)))
  $zip = $shell.NameSpace($zipName.ToString())
  if ($zip -eq $null) { continue }
  foreach ($file in $f.Value) {
      Start-Sleep -Seconds 1
    while (!(checkFile $zipName)) {
      Start-Sleep -Milliseconds 100
  [Runtime.InteropServices.Marshal]::ReleaseComObject($zip) | Out-Null
[Runtime.InteropServices.Marshal]::ReleaseComObject($shell) | Out-Null
del Function:checkFile

How To Ask Questions The Smart Way

message edited by Razor2.3

Report •
Related Solutions

Ask Question