Removing files from ZIP folder according to ModifyDate

This topic contains 0 replies, has 1 voice, and was last updated by  Forums Archives 5 years, 11 months ago.

  • Author
    Posts
  • #6015

    by sgarciacode at 2013-02-06 19:56:30

    Hello Everyone,

    This is my first post on the forum. I hope to learn a lot from all you talented Powershell users on this site, and share what I know as well.

    The first snippet of code is a simple function that I created to delete files in a folder that are older than a given date. This worked great so I thought that I would use similar logic to access files within a ZIP folder and delete the files using the same criteria. Unfortunately, the object created when accessing the files within the zip cannot be deleted using the Remove-Item cmdlet. I have searched for solutions online but have not yet found anything that really address what I am trying to do. The second snippet of code was my attempt to solve the problem, if there is anyone who can help me with this that would be great.

    Thanks

    [code2=powershell]function delete-files{

    ($TargetFolder = $(Read-host "Please Enter The Directory Path"))

    ($oldDate = $(Read-Host "Please Enter the date from which you wish to delete back"))

    if (Test-Path $TargetFolder){
    $Files = get-childitem $TargetFolder -recurse | Where {$_.LastWriteTime -lt (get-date $oldDate)} | remove-item
    }
    else{
    {Write-Host "The Folder $TargetFolder Does Not Exist!"}
    }
    }[/code2]

    _______________________________________________________________________________________________________________________
    [code2=powershell]function delete-fromZip{

    ($TargetFolder = $(Read-host "Please Enter The Directory Path"))

    ($oldDate = $(Read-Host "Please Enter the date from which you wish to delete back"))

    $shellApp = new-object -com shell.application

    if (Test-Path $TargetFolder){
    $zipFile = $shellApp.NameSpace("$TargetFolder")

    $Files = $zipFile.Items() | Where {$_.LastWriteTime -lt (get-date $oldDate)}

    foreach ($File in $Files){
    write-host "Deleting File: $File" -foregroundcolor "Red"
    Remove-Item $File
    }
    }
    else{
    {Write-Host "The Folder $TargetFolder Does Not Exist!"}
    }
    }[/code2]

    by poshoholic at 2013-02-06 21:55:08

    As you discovered, Remove-Item won't work. That's for non-compressed files/folders.

    Instead of:
    Remove-Item $File
    I think you want something like this:
    $File.InvokeVerb('Delete')

    by sgarciacode at 2013-02-07 11:24:29

    Thanks for the response,

    This worked great. Now the only problem I am having is selecting the files I want. When I run the function it deletes all the files within the zip. It seems that the "Where" conditions are not being taken into account. When I took a look at the individual files, in the command line and I got the following information regarding the ModifyDate: 12/5/2011 5:08:32 PM. But if I look at this same file on my computer folder it states the Date is 7/22/2011 1:10 PM. So I am wondering if I need to use a different variable other than .LastWriteTime.

    Thanks

    by poshoholic at 2013-02-08 08:00:58

    I just looked into the properties you get back from the files in the archive/zip folder. ModifyDate is the name of the property in those files, not LastWriteTime (which is the name of the property you would get from non-archived files if you use PowerShell or .NET to retrieve them). With that in mind, you should change your Where-Object clause for archive/ZIP folder contents to look like this:
    Where {$_.ModifyDate -lt (get-date $oldDate)}

You must be logged in to reply to this topic.