0

I am not too familiar with Windows PowerShell (and Windows console commands in general), but I wish to write a batch file which can create sperated .zip archives from specific files in a folder, then delete all but the .zip files. So, I have a folder as "C:\myfolder\" and some files in it like: myfile0001.a myfile0001.b myfile0001.c myfile0002.a myfile0002.b myfile0002.c myfile0003.a myfile0003.b myfile0003.c ... and so on.

I want a create a .zip for every myfileXXXX.a + myfileXXXX.b + myfileXXXX.c package and name the .zips as the file names (for example myfile0001.zip would contain myfile0001.a + myfile0001.b + myfile0001.c).

I know that I can use this code to create each .zip archive one-by-one:

powershell -Command "& {Compress-Archive -Path C:\myfolder\myfile0001.*  -CompressionLevel Optimal -DestinationPath C:\myfolder\myfile0001.zip}"

And this code working fine to delete all but the .zip archives

powershell -Command "& {Remove-Item C:\myfolder\*.* -Exclude *.zip}"

What I cannot solve is to create a for cycle which can loop through all myfileXXXX.* and create a myfileXXXX.zip using the XXXX as the increasing value.

eleks1835
  • 3
  • 2

3 Answers3

0

I cannot still test this as I am not on a windows host now, but something like this. It needs to be saved as a .cmd or .bat file:

@echo off
 for %%i in (C:\myfolder\*) do (
    powershell -Command "& {Compress-Archive -Path C:\myfolder\%%~ni.*  -CompressionLevel Optimal -DestinationPath C:\myfolder\%%~ni.zip}"
)
Gerhard
  • 22,678
  • 7
  • 27
  • 43
  • Hi! During code run it generated error messeges like: [Compress-Archive : The archive file "C:\myfolder\myfile0001.zip already exists. Use the -Update par ameter to update the existing archive file or use the -Force parameter to overwrite the existing archive file.] , but generally worked. Thank you! – eleks1835 May 22 '19 at 18:10
  • I could not test it, so post the errors so I can see what they are. – Gerhard May 22 '19 at 18:10
0

In PowerShell, you could do:

Get-ChildItem -Path 'C:\myfolder' -File | Where-Object { $_.BaseName -match 'myfile\d{4}' } | Group-Object BaseName | ForEach-Object {
    $target = Join-Path -Path ($_.Group[0].DirectoryName) -ChildPath ('{0}.zip' -f $_.Group[0].BaseName)
    $_.Group | Compress-Archive -CompressionLevel Optimal -DestinationPath $target
    $_.Group | Remove-Item -Force
}

Writing it as commandline gets a little ugly, but here you go:

powershell -Command "& {Get-ChildItem -Path 'C:\myfolder' -File | Where-Object { $_.BaseName -match 'myfile\d{4}' } | Group-Object BaseName | ForEach-Object { $target = Join-Path -Path ($_.Group[0].DirectoryName) -ChildPath ('{0}.zip' -f $_.Group[0].BaseName); $_.Group | Compress-Archive -CompressionLevel Optimal -DestinationPath $target; $_.Group | Remove-Item -Force}}"
Theo
  • 57,719
  • 8
  • 24
  • 41
0

How's this? (Just use powershell) Zip files based on the basename. .zip automatically gets added to the archive filename.

powershell "ls | foreach { compress-archive $_ $_.basename -update -verbose }"


VERBOSE: The archive file path 'C:\users\js\foo\file1' supplied to the DestinationPath patameter does not include .zip extension. Hence .zip is
 appended to the supplied DestinationPath path and the archive file would be created at 'C:\users\js\foo\file1.zip'.
VERBOSE: Preparing to compress...
VERBOSE: Performing the operation "Compress-Archive" on target "C:\users\js\foo\file1.1".
VERBOSE: Adding 'C:\users\js\foo\file1.1'.
VERBOSE: The archive file path 'C:\users\js\foo\file1' supplied to the DestinationPath patameter does not include .zip extension. Hence .zip is
 appended to the supplied DestinationPath path and the archive file would be created at 'C:\users\js\foo\file1.zip'.
VERBOSE: Preparing to compress...
VERBOSE: Performing the operation "Compress-Archive" on target "C:\users\js\foo\file1.2".
VERBOSE: Adding 'C:\users\js\foo\file1.2'.
VERBOSE: The archive file path 'C:\users\js\foo\file1' supplied to the DestinationPath patameter does not include .zip extension. Hence .zip is
 appended to the supplied DestinationPath path and the archive file would be created at 'C:\users\js\foo\file1.zip'.
VERBOSE: Preparing to compress...
VERBOSE: Performing the operation "Compress-Archive" on target "C:\users\js\foo\file1.3".
VERBOSE: Adding 'C:\users\js\foo\file1.3'.
js2010
  • 23,033
  • 6
  • 64
  • 66