1

I have a folder with files. I need to filter according to an extension and to ZIP one by one

$files = Get-ChildItem -path "C:\Temp\SharedFolder\SideVIP\*" -Filter *.VIP
foreach ($file in $files) {

Need some help with the rest

Bandit
  • 399
  • 3
  • 10

2 Answers2

2

In PowerShell 4+, you can use Compress-Archive

$destPath = 'X:\NewPath'
foreach($file in $files) {
  Compress-Archive -Path $file -DestinationPath "$destPath\$($file.BaseName).zip" 
}

And if you need to "unzip":

$files = Get-ChildItem C:\Temp\SharedFolder\SideVIP\*.zip
foreach($file in $files) { Expand-Archive -Path $file -DestinationPath . -Force }
PollusB
  • 1,726
  • 2
  • 22
  • 31
  • Nice. Small caveat: While the use of a wildcard-based input path in this case results in the `$file` instances stringifying to their _full path_, that isn't always the case in Windows PowerShell (it now is in PowerShell (Core) 7+), so it is safer to use `$file.FullName` explicitly. – mklement0 Feb 23 '22 at 14:43
  • This script was tested on PowerShell 5.1. If you have 6 or 7 and it doesn't work, I would definitly use $file.FullName. But I would be sad to know that Microsoft's own function could not handle an object. – PollusB Feb 23 '22 at 14:53
  • Yes, as I said it works _in this case_, because you're using a _wildcard_ path directly as the `-Path` argument. But - in Windows PowerShell - the seemingly equivalent `Get-ChildItem C:\Temp\SharedFolder\SideVIP -Filter *.zip` would _not_ work. This headache _has gone away_ in PowerShell _Core_ (v6+). See [this answer](https://stackoverflow.com/a/69966621/45375) for details.. Yes, it is sad that PowerShell's cmdlets stringify `[System.IO.FileInfo]` instances instead of using them as-is, _as objects_. – mklement0 Feb 23 '22 at 14:58
  • Sorry, posted the wrong link: see [this answer](https://stackoverflow.com/a/53400031/45375) for an explanation of the inconsistent stringification of `[System.IO.FileInfo]` and `[System.IO.DirectoryInfo]` instances in Windows PowerShell. – mklement0 Feb 23 '22 at 15:10
  • After all the files were compressed I need to copy them to another folder. There is an easy way? – Bandit Feb 23 '22 at 15:34
  • Copy-Item or Move-Item or you could compress them directly to the new destination using ```-DestinationPath X:\NewDest\Archive.zip``` – PollusB Feb 23 '22 at 15:37
  • You should update your question to reflect that new need. – PollusB Feb 23 '22 at 15:41
  • Try using ```$files = Get-ChildItem -path "C:\Temp\SharedFolder\SideVIP\*.VIP"``` instead – PollusB Feb 23 '22 at 16:47
-1

Creating zip file using Power shell. first need to install 7Zip software

function create-7zip([String] $aDirectory, [String] $aZipfile){
    [string]$pathToZipExe = "$($Env:ProgramFiles)\7-Zip\7z.exe";
    [Array]$arguments = "a", "-tzip", "$aZipfile", "$aDirectory", "-r";
    & $pathToZipExe $arguments;
}

Remove-item C:\BackupDummy\*.zip
Get-ChildItem C:\Satwadhir\ | 
    Group-Object -Property { $_.Name.Substring(0, $_.Name.Length) } |
    % { create-7zip $_.Group.FullName "C:\BackupDummy\$($_.Name).zip" }