0

Okay, so I am very new - learning Python and PowerShell currently, but I have a problem and am trying to solve it with code.

I need a script that will

  1. Look at a folder to find .zip files
  2. Unzip located file (to whatever destination, I can define that)
  3. Delete the completed zip.
  4. Repeat steps 1-3 until there are no more zip files in the destination

I tried to write a simple loop in PowerShell and I couldn’t figure out how to get Expand-Archive to cooperate. When I used the *.zip wildcard for the path or literal path, it would get mad that I am pointing it at multiple files.

There are thousands of files at the destination, and they are not small so unzipping by hand would be extremely time consuming, and naming the path exactly what the zip file is called five thousand times would only be slightly less time consuming.

npeez
  • 1
  • 1
  • 1
    Show the code you currently have – nordmanden May 17 '22 at 17:43
  • 1
    Please give us an example folder structure, example input data, and desire output data. Please also include enough of your code that we can both understand and recreate the issue. – another victim of the mouse May 17 '22 at 17:45
  • [1] use `Get-ChildItem` to get the list of source files ///// [2] iterate thru the resulting collection ///// [3] make the destination dir ///// [4] extract that one zip into the dest dir ///// if i understand your intent, that otta work. [*grin*] – Lee_Dailey May 17 '22 at 17:59

1 Answers1

0

Get-ChildItem will not work. This is because it has to build the list before it can start running any actions. By building the list, it has to put all of the items in memory, then it can apply filters or perform additional actions.

If you are expecting to run it against a large amount of items, you need to use a .net call: [System.IO.Directory]::GetFiles() Get-ChildItem vs. direct use of the .NET Framework's [System.IO.Directory]::GetFiles() method with UNC paths

These calls, when piped "|" with other commands will stream the list of items to the commands and allow you to run a bunch of filters on files much more quickly. There are still a few things you will need to figure out.

I used 7zip commands to do something similar as to what you are describing now: https://jasonduffett.net/post/5103879646/using-7zip-from-powershell#:~:text=One%20solution%20is%20to%20use%20the%20excellent%207-zip,-Path%20%28Split-Path%20-parent%20%24MyInvocation.MyCommand.Definition%29%20-ChildPath%20%227z.exe%22%20if%20%28%21

Technoob1984
  • 172
  • 9