且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Powershell system.io.compression压缩文件和/或文件夹

更新时间:2023-11-19 19:07:46

@DavidBrabant是正确的,将通配符放在路径中是正常的,但是我认为你可以在这种情况下逃脱它,因为你通过foreach声明来管理结果。

@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.

我认为问题是你的第一个参数 CreateFromDirectory 应该是一个目录名,但你已经传递了一个文件名。使用变量名称($ Source和$ source)确实没有帮助。

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).

当你打电话给 CreateFromDirectory 它将包含第一个zip文件的全名,因为以下行:

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

$Source = $_.fullName

我猜你有一个过滤器'* .txt'你要添加单个文件而不是整个文件夹到一个zip文件然后它更多涉及。请参阅此处以获取示例: http:// msdn。 microsoft.com/en-us/library/hh485720(v=vs.110).aspx

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)