Click here to Skip to main content
15,881,588 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi, I am new to powershell scripting. I found this[^] article on creating a zip folder but I don't know how to specifically find files older than 1 week.

What I have tried:

I have tried using a foreach loop and copying the files into a temp directory and then using the link I found earlier but Powershell syntax is not working properly.
Posted
Updated 29-Sep-16 5:55am
Comments
[no name] 29-Sep-16 10:39am    
Perhaps you can reword your question to actually include a question and possibly the code you are having trouble with. As it stands now, your question reads more like you want someone to write your code for you.
rohith naik 29-Sep-16 12:16pm    
Well that is not what I intended at all. Fixed the issue and posted the solution.
Richard MacCutchan 29-Sep-16 10:58am    
but Powershell syntax is not working properly.
And you think we can guess where and why?

If you have a problem then please post all the relevant details.
rohith naik 29-Sep-16 11:56am    
Thank you, powershell syntax goes over my head, however I have posted my solution after I figured it out. Please feel free to tell me what can be improved.
rohith naik 29-Sep-16 12:00pm    
I do have another problem I just figured out. I am pasting the zip file in the same directory as the main file. This would mean that the zip files I have created this week would get zipped again next week. I have used this syntax but this again seems wrong

$rootFolder = "C:\MyFiles\Output"
$tempVariable = $rootFolder + "*.*"
$files = Get-ChildItem -Path $rootFolder -exclude *.zip

Mind telling me whats wrong?

 
Share this answer
 
Comments
rohith naik 29-Sep-16 11:39am    
Thank you. Turns out I was not doing the full file names properly. I have added my solution below
Maciej Los 29-Sep-16 13:19pm    
Interesting... Does my answer was helpful in finding solution? If yes, it would be nice to see my answer accepted as a solution (green button) and it would be nice to see how much it's worth (use voting system)...
C#
#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
 
Share this answer
 
v2

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