Quantcast
Channel: Windows PowerShell forum
Viewing all articles
Browse latest Browse all 21975

Compressing Log Files from Multiple Sources?

$
0
0

I need some help with a problem I'm facing at work. I've been tasked with developing a script to take video files older than a day stored from multiple directories and placing them in zipped folders named after their parent directory.

For example, I would need to search for all files in C:\DirA and move them to C:\ Archive\DirA(date-1).zip I would then need the script to go to C:\DirB and move them to C:\ArchiveDirB(date-1).zip and so on. 

I'm really lost and I don't have much of an idea on how to tackle this one. I have a partial script from another discussion thread that is running into errors with the actual zip process. Any help would be greatly appreciated.

Here is the actual code I'm trying to use. This code lacks the needed parts for moving the individual parts from the subfolders and doesn't quite work...

#usage: creat new zip function
function New-Zip
{
	param([string]$zipfilename)
	set-content $zipfilename ("PK" + [char]5 + [char]6 + ("$([char]0)" * 18))
	(dir $zipfilename).IsReadOnly = $false
}

#usage: -Recurse | add-Zip
function Add-Zip
{
	param([string]$zipfilename)

	if(-not (test-path($zipfilename)))
	{
		set-content $zipfilename ("PK" + [char]5 + [char]6 + ("$([char]0)" * 18))
		(dir $zipfilename).IsReadOnly = $false	
	}
	$shellApplication = new-object -com shell.application
	$zipPackage = $shellApplication.NameSpace($zipfilename)
	foreach($file in $input) 
	{ 
            $zipPackage.CopyHere($file.FullName)
            Start-sleep -milliseconds 500
	}
}

#Change these variables to meet needed reqs

$targetFolder = 'D:\sample'
$destinationFolder = 'C:\sample'
$now = Get-Date
$days = 1
$lastWrite = $now.AddDays(-$days)

#usage: destination folder

Get-ChildItem $targetFolder -Recurse | Where-Object { $_ -is [System.IO.FileInfo] } | ForEach-Object {
	If ($_.LastWriteTime -lt $lastWrite)
	{
		$_ | New-Zip $($destinationFolder + $_.BaseName + $lastWrite + ".zip") 
		$_ | Add-Zip $($destinationFolder + $_.BaseName + $lastWrite + ".zip")
	}
}


Viewing all articles
Browse latest Browse all 21975

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>