'PS Script to handle thousands of zip files…
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
- Look at a folder to find .zip files
- Unzip located file (to whatever destination, I can define that)
- Delete the completed zip.
- 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.
Solution 1:[1]
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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Technoob1984 |
