Solved

Need a script to archive log files

Posted on 2011-09-14
6
497 Views
Last Modified: 2013-08-02
Hello everyone,

I hope someone can help. I have had a few stabs at this but it doesn't seem to be working.

I need help writing a script that will zip up the previous month's worth of logfiles, move the zip to a network folder on a mapped drive and then delete the original logs.

The compression format isn't too important, as long as it's fast and gets reasonably good compresion ratios with text/html files, which should be all. I've been favouring 7zip.

The script language isn't too important either as long as it can be run completely in the background as a Windows 7 scheduled task and not impact performance. This rules out linux shell scripting.

An example of the format for todays logs would be:

Webs - Daily - 14 - 09 - 11.txt
OR
Webs - Daily - 14 - 09 - 11.html

Also, there is usually only one file per day, html or text.

Etc

If anyone can help I'd be very grateful.

Regards,
Anthony
0
Comment
Question by:AnthonyUK
  • 2
  • 2
6 Comments
 
LVL 35

Expert Comment

by:YZlat
Comment Utility
0
 
LVL 35

Accepted Solution

by:
YZlat earned 500 total points
Comment Utility
try something like that:


$lastMonth = ($Now.AddMonths(-1)).month
$SourcePath = "X:\sourceDirectory"
$DestPath = "\\Server\TargetPath"
$FileName="MyZip.zip"
$ZipPath=$SourcePath + $FileName

$files = dir $SourcePath

# empty arrays to hold file lists
$movedFiles = @()
$skippedFiles = @()
#create zip file
$ZipFile = (new-object -com shell.application).NameSpace($path) 

foreach ($file in $files){
	if (($file.lastwritetime).month -eq $lastMonth){
		$newname = $DestPath + "\" + $file.name + "-" + $file.lastwritetime.tostring("yyyy-MMM-dd")		
		$ZipFile.CopyHere($file.fullname)} 

		
		
	}# end if
}# end for

#move zip file
move-item -ev a -path $ZipFile.fullname -destination $DestPath + "\" + $FileName

#loop through old files, deleting them
foreach ($file in $files){
	if (($file.lastwritetime).month -eq $lastMonth){
		Remove-Item $file | out-null
	}

}

Open in new window

0
 
LVL 51

Expert Comment

by:Bill Prew
Comment Utility
Could be done using 7zip from a BAT file.

Would you run the scheduled job on say the last day of the month, after the backup file has been created?  This would take the date logic out of the mix, and the script could just grab all files in the folder and place them in the monthly archive on the server.

~bp
0
 
LVL 51

Expert Comment

by:Bill Prew
Comment Utility
@AnthonyUK

Any feedback?

~bp
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Utilizing an array to gracefully append to a list of EmailAddresses
Set OWA language and time zone in Exchange for individuals, all users or per database.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now