Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Need a script to archive log files

Posted on 2011-09-14
6
Medium Priority
?
546 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
6 Comments
 
LVL 35

Expert Comment

by:YZlat
ID: 36538761
0
 
LVL 35

Accepted Solution

by:
YZlat earned 2000 total points
ID: 36538852
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 57

Expert Comment

by:Bill Prew
ID: 36539101
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 57

Expert Comment

by:Bill Prew
ID: 37124914
@AnthonyUK

Any feedback?

~bp
0

Featured Post

Looking for the Wi-Fi vendor that's right for you?

We know how difficult it can be to evaluate Wi-Fi vendors, so we created this helpful Wi-Fi Buyer's Guide to help you find the Wi-Fi vendor that's right for your business! Download the guide and get started on our checklist today!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Windows 10 came with  a lot of built in applications, Some organisations leave them there, some will control them using GPO's. This Article is useful for those who do not want to have any applications in their image (example:me).
In the absence of a fully-fledged GPO Management product like AGPM, the script in this article will provide you with a simple way to watch the domain (or a select OU) for GPOs changes and automatically take backups when policies are added, removed o…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

721 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