0

I have these below files at a location C:\Desktop\Mobile.

    Apple_iphone6.dat
    Apple_iphone7.dat
    Samsung_edge7.dat
    Samsung_galaxy.dat
    Sony_experia.dat
    Sony_M2.dat

I need to create a script that writes the similar files into a single zip. So files Apple_iphone6.dat and Apple_iphone7.dat must be into single zip. So the final zip files created would be:

Apple_Files_Timestamp.zip
Samsung_Files_Timestamp.zip
Sony_Files_Timestamp.zip

I tried this

Get-ChildItem C:\Desktop\Mobile -Recurse -File -Include *.dat | Where-Object { $_.LastWriteTime -lt $date } | Compress-Archive -DestinationPath C:\Desktop\Mobile

But it gives me error 'Compress-Archive' is not recognized as the name of a cmdlet.

How can I get this code work?

wonea
  • 4,783
  • 17
  • 86
  • 139
Novice_Techie
  • 426
  • 3
  • 10
  • 21
  • 1
    [Compress-Archive](https://ss64.com/ps/compress-archive.html) requires PowerShell Version 5+ –  May 17 '17 at 17:10
  • 1
    I'd first group by the make name `gci *.dat|group {($_.Name).split('_')[0]}` and then iterate the groups to zip with a 3rd party tool like 7zip. –  May 17 '17 at 17:25
  • I just realized i am using lower version of Power Shell. also my code will zip everything in one single zip. But i need to group it. Some code samples will really help me. – Novice_Techie May 17 '17 at 17:42

4 Answers4

2

Pre Powershell v5 you can use this. No additional downloads needed.

$FullName = "Path\FileName"
$Name = CompressedFileName
$ZipFile = "Path\ZipFileName"
$Zip = [System.IO.Compression.ZipFile]::Open($ZipFile,'Update')
[System.IO.Compression.ZipFileExtensions]::CreateEntryFromFile($Zip,$FullName,$Name,"optimal")
$Zip.Dispose()
Jessie
  • 284
  • 1
  • 3
  • 12
  • v5.1.16299.251 complains TypeNotFound ``` Major Minor Build Revision ----- ----- ----- -------- 5 1 16299 251 ``` – aaron Apr 04 '18 at 06:08
2

You have two problems, I will try to summarize both of them.

1. Compress files

In order to use Compress-Archive command you need to have PowerShell 5 as already commented by @LotPings. You can:

  • run your script on Windows 10 machine, or Server 2016 which are coming with v5
  • download and install PoSh 5, see details on MSDN

If you cannot do either of those, you can

  • install some module from PowerShell gallery that provides similar functionality via 7-zip tool. Search resultes are here. Download and check those modules before use!
  • use .NET 4.5 class, check answer here on Stack Overflow

2. Group files

Once you group files, you can easily pipe them to compressing command, similar as you already tried. Proper grouping would be achieved with something like this:

$Files = Get-ChildItem 'C:\Desktop\Mobile'
$Groups = $Files | ForEach-Object {($_.Name).split('_')[0]} | Select-Object -Unique

foreach ($Group in $Groups) {
    $Files | where Name -Match "^$Group" | Compress-Archive "C:\Desktop\Mobile\$Group.7z"
}
Community
  • 1
  • 1
Igor
  • 1,349
  • 12
  • 25
  • $destination= 'C:\Temp' $Files = Get-ChildItem 'C:\Desktop\Mobile.,*.dat' $Groups = $Files | ForEach-Object {($_.Name).split('_')[0]} | Select-Object -Unique foreach ($Group in $Groups) { $Files | where Name -Match "^$Group" | Add-Type -assembly "system.io.compression.filesystem" [io.compression.zipfile]::CreateFromDirectory($Files,$destination) } This isn't creating any zip files. What is missing here? – Novice_Techie May 30 '17 at 01:03
  • specify -DestinationPath with xxx.zip like this: `| Compress-Archive -DestinationPath xxx.zip` will suppress the 7z not supported error and the prompt to ask for destination path – aaron Apr 04 '18 at 05:58
0

With Powershell 2.0 you can't use Compress-Archive, you need download the original terminal executables to zip and unzip files from here.

You can use:

zip <path> <zip_name> -i <pattern_files>

In your example:

zip "C:\Desktop\Mobile" Apple_Files_Timestamp.zip -i Apple*.dat
zip "C:\Desktop\Mobile" Samsung_Files_Timestamp.zip -i Samsung*.dat
zip "C:\Desktop\Mobile" Sony_Files_Timestamp.zip -i Sony*.dat

If you need use adittional zip options, visit zip manual.

randiel
  • 290
  • 1
  • 16
0
  • The following script does the grouping,
  • the zipping command depends on your chosen zipper.

$TimeStamp = Get-Date -Format "yyyyMMddhhmmss"
Get-ChildItem *.dat|
  Group-Object {($_.Name).split('_')[0]}|
    ForEach-Object {
      $Make = $_.Name
      Foreach($File in $_.Group){
        "{0,20} --> {1}_Files_{2}.zip" -f $File.Name,$Make,$TimeStamp
      }
}

Sample output:

> .\SO_44030884.ps1
   Samsung_edge7.dat --> Samsung_Files_20170517081753.zip
  Samsung_galaxy.dat --> Samsung_Files_20170517081753.zip
   Apple_iphone6.dat --> Apple_Files_20170517081753.zip
   Apple_iphone7.dat --> Apple_Files_20170517081753.zip
         Sony_M2.dat --> Sony_Files_20170517081753.zip
    Sony_experia.dat --> Sony_Files_20170517081753.zip

This link might help Module to Synchronously Zip and Unzip using PowerShell 2.0