Click here to Skip to main content
15,881,781 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have found a script in this group about compressing a file according to the file modification date. But what I am looking for is to compress log files which are in .jrn format and the file name is like this ABCD20200710.jrn. And the compression must be done after the 90 days of the log generation. I mean 90 days after the file is generated according to the filename date. Also, I am just trying the scripting thing not a well good user. Please guide me.

What I have tried:

#get the list of files in the original folder
$rootFolder = "C:\MyFiles\Output"
$tempVariable = $rootFolder
$files = Get-ChildItem -Path $rootFolder 

#create a temporary folder using today's date
$tempFolderRoot = "C:\Temp_"
$date = Get-Date
$date = $date.ToString("yyyy-MM-dd")
$tempFinalFolder = "$tempFolderRoot$date"
New-Item -ItemType directory -Path $tempFinalFolder -Force

#decide how long back to go
$timespan = new-timespan -days 7

#move the files to a temporary location
foreach($file in $files)
{
	$fileLastModifieddate = $file.LastWriteTime
	if(((Get-Date) - $fileLastModifiedDate) -gt $timespan)
	{
		Move-Item "$rootFolder\$file" -destination $tempFinalFolder
	}
}

$CompressionToUse = [System.IO.Compression.CompressionLevel]::Optimal
$IncludeBaseFolder = $false
$zipTo = "{0}\Archive_{1}.zip" -f $rootFolder,$date

#add the files in the temporary location to a zip folder
[Reflection.Assembly]::LoadWithPartialName( "System.IO.Compression.FileSystem" )
[System.IO.Compression.ZipFile]::CreateFromDirectory($tempFinalFolder, $ZipTo, $CompressionToUse, $IncludeBaseFolder)

#Remove-Item $tempFinalFolder -RECURSE
Posted
Updated 13-Jul-20 23:47pm
Comments
Garth J Lancaster 14-Jul-20 2:32am    
the format of you journal file - "ABCD20200710.jrn" - is there ALWAYS only 4 characters (for example, the 'ABCD') before the date ?
AVT007 14-Jul-20 3:55am    
Yes. Its just 4 characters. Its a log file of an ATM machine.

1 solution

ok, look at the bold bits - I've left in the original code, commented out with '#'.. NB, all untested, just off the top of my head
#decide how long back to go
#$timespan = new-timespan -days 7
$timespan = new-timespan -days 90

#move the files to a temporary location
foreach($file in $files)
{
	#$fileLastModifieddate = $file.LastWriteTime
    $filenameFromPath = [System.IO.Path]::GetFileName($file)
    $dateFromFilename = $filenameFromPath.Substring(4,8)
    $fileCreateDate = [datetime]::parseexact($dateFromFilename, 'yyyyMMdd', $null)
	#if(((Get-Date) - $fileLastModifiedDate) -gt $timespan)
    if(((Get-Date) - $fileCreateDate) -gt $timespan)
	{


The '#' code etc can be removed when it's working, and those sequences of mine can be combined to some extent, but it makes it easier to show the why && what if I express them long-form first
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900