Powershell system.io.compression zipping files and/or Folders

旧时模样 提交于 2019-12-05 19:09:07

@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)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!