remove files from zipped folders

Tagged: ,

This topic contains 6 replies, has 3 voices, and was last updated by Profile photo of Ethen 5155 Ethen 5155 5 months, 2 weeks ago.

  • Author
    Posts
  • #63748
    Profile photo of Ethen 5155
    Ethen 5155
    Participant

    Hi all,

    well i have suffered for many days to get this code for my case but unfortunately i failed:(

    i want to go on target path folder which contains about 500 zipped files then open each on of them and delete one folder and another single file

    as shown on below link

    http://imgur.com/a/k9c2E

    open Zip filder —> go on (xl) folder —> delete (queryTables) and (connections.xml)

    any help or hint please

    Thanks in advance

  • #63755
    Profile photo of Jonathan Warnken
    Jonathan Warnken
    Participant

    Do you have code that works up to a point?

    If you are trying to work with zip files powershell 5 does have some commands to help. see https://blogs.technet.microsoft.com/heyscriptingguy/2015/08/14/working-with-compressed-files-in-powershell-5/

  • #63763
    Profile photo of random commandline
    random commandline
    Participant

    This will delete the folder and file from your zips.

    $zip = Get-ChildItem \\path\to\zips -Filter *.zip
    add-type -AssemblyName 'System.IO.Compression.filesystem'
    
    # Change zip file
    foreach ($z in $zip){
        Write-Verbose "Updating $($z.name)" -Verbose
        $tempz = [io.compression.zipfile]::Open($z.FullName,'Update')
        $entry = $tempz.Entries | Where-Object {
        $_.fullname -match 'queryTables|connections.xml'}
        foreach ($e in $entry){$e.Delete()}
        $tempz.Dispose()
    }
    
    • #63803
      Profile photo of Ethen 5155
      Ethen 5155
      Participant

      Hello,

      well at the first thanks for your reply and code

      but when i used it i found that error msg below

      any idea why??

      and i already have "System.IO.Compression.FileSystem.dll" on my ".NETFramework\v4.5" folder
      Thanks

  • #63850
    Profile photo of random commandline
    random commandline
    Participant

    Run

    $PSVersionTable

    Check PSVersion and CLRVersion (.NET Version)
    What versions of PowerShell and .NET are installed? I tested it with PS:3.0 / CLR:4.0.30319.42000.
    Update .NET and at least to PS 3.0

    • #63889
      Profile photo of Ethen 5155
      Ethen 5155
      Participant

      oh works perfect now, really i don't find enough words to thank you

      one more request please and anyway don't push your self to answer if you are busy

      i'm looking now to add (.zip) to all files at target path folder to be zipped manually and individually

      then i want to reverse this process with another code, delete (.zip) to all individually too

      for ex,

      Test.xls —> Test.xls.zip —> Test.xls

      and to be applied on many files at target path folder

      can it be

      Thanks in advance

      cheers

  • #63892
    Profile photo of Ethen 5155
    Ethen 5155
    Participant

    well i think i got it finally 😀 😀

    Dir *.xls | rename-item -newname { [io.path]::ChangeExtension($_.name, "xls.zip") }

    Thanks a lot for your care

You must be logged in to reply to this topic.