4

Im producing some automated tasks at work where i need to zip certain files and/or folders. What im trying to do is getting zip the text files in folder 1 which contains 4 txt files.

Executing this command gives an error but still zips the txt files :

Exception calling "CreateFromDirectory" with "4" argument(s): "The directory name is invalid.
"
At line:15 char:13
+             [System.IO.Compression.ZipFile]::CreateFromDirectory($Source, "$Sour ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : NotSpecified: (:) [], MethodInvocationException

What i got now is:

[Reflection.Assembly]::LoadWithPartialName( "System.IO.Compression.FileSystem" )
$includeBaseDirectory = $false
$compressionLevel= [System.IO.Compression.CompressionLevel]::Optimal

$source = "C:\folder1\*"
Get-ChildItem $source -include *.txt  |
Foreach {
        $Source = $_.fullName
        [System.IO.Compression.ZipFile]::CreateFromDirectory    ($Source, "$Source.zip",$compressionLevel, $includebasedirectory)
        }

Also if i want to zip the folders inside folder1 i use -directory switch instead of include. that doesnt produce any error messages. any suggestions?

vya
  • 57
  • 1
  • 2
  • 7

1 Answers1

7

@DavidBrabant is right that it is not normal to put the wildcard in the path, but I think you can get away with it in this instance as you are piping the results through a foreach statement.

I believe the problem is that your first parameter of CreateFromDirectory should be a directory name, but you have passed it a filename. This isn't really helped by the use of variable names ($Source and $source).

When you call CreateFromDirectory it will contain the full name to the first zip file because of the following line:

$Source = $_.fullName

I'm guessing that as you have a filter '*.txt' you want to add individual files rather than the whole folder to a zip file then it is a little more involved. See here for an example: http://msdn.microsoft.com/en-us/library/hh485720(v=vs.110).aspx

But if you simply want to zip the folder then use:

$sourceFolder = "C:\folder1"
$destinationZip = "c:\zipped.zip" 
[Reflection.Assembly]::LoadWithPartialName( "System.IO.Compression.FileSystem" )
[System.IO.Compression.ZipFile]::CreateFromDirectory($sourceFolder, $destinationZip)
David Martin
  • 11,764
  • 1
  • 61
  • 74
  • Only thing is that i want it zipped with its own original name. Also i only want some files zipped and no folders. – vya Nov 19 '13 at 11:47
  • This one works : Get-ChildItem "C:\temp\test" -Directory <#norecurse#> | foreach { $Folder = $_.fullName [System.IO.Compression.ZipFile]::CreateFromDirectory($Folder, "$Folder.zip",$compressionLevel,$ExcludeBaseDirectory) – vya Nov 19 '13 at 11:52
  • How do I increase the compression to make file smaller ? – Nigel Fds Jul 26 '17 at 00:29
  • 1
    @NigelFds - Have a read of https://msdn.microsoft.com/en-us/library/hh485721(v=vs.110).aspx and https://msdn.microsoft.com/en-us/library/system.io.compression.compressionlevel(v=vs.110).aspx – David Martin Jul 26 '17 at 08:46
  • @DavidMartin thanks thats exactly what I was looking for, cheers – Nigel Fds Jul 27 '17 at 00:10