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?

3
  • 1
    Compress-Archive requires PowerShell Version 5+ Commented May 17, 2017 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. Commented May 17, 2017 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. Commented May 17, 2017 at 17:42

4 Answers 4

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"
}
Sign up to request clarification or add additional context in comments.

2 Comments

$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?
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
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()

1 Comment

v5.1.16299.251 complains TypeNotFound ``` Major Minor Build Revision ----- ----- ----- -------- 5 1 16299 251 ```
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.

1 Comment

The real question then becomes, why are you still using v2? Update it.
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.