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

编程入门 行业动态 更新时间:2024-10-20 08:50:08
本文介绍了Powershell system.iopression压缩文件和/或文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在工作中生成一些自动化任务,我需要压缩某些文件和/或文件夹。 我想要做的是获取zip文件夹1中包含4个txt文件的文本文件。

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.

执行此命令会出错但仍会拉链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

我现在得到的是:

[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) }

此外,如果我想压缩folder1中的文件夹,我使用-directory switch而不是include。 不会产生任何错误消息。 有什么建议吗?

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?

推荐答案

@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/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: msdn.microsoft/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)

更多推荐

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

本文发布于:2023-11-09 20:43:49,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1573391.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:压缩文件   文件夹   system   Powershell   compression

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!