使用PS将文件夹中的文件压缩为zip文件 [英] Compress files in folder to zip file by using PS

查看:250
本文介绍了使用PS将文件夹中的文件压缩为zip文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下脚本将文件夹(文件夹中的所有文件)压缩为 zip 文件:

I have the following scripts to compress a folder (all files in the folder) to a zip file:

 set-content $zipFileName ("PK" + [char]5 + [char]6 + ("$([char]0)" * 18)) 
 $ZipFile = (new-object -com shell.application).NameSpace($zipFileName) 
 Get-ChildItem $folder | foreach {$zipFile.CopyHere($_.fullname)} 

以 $folder = "C:\Test" 和 $zipFileName = "C:\data\test.zip" 为例

where $folder = "C:\Test", and $zipFileName = "C:\data\test.zip" as example

如果 "C:\Test" 不包含空子文件夹,它工作正常,并且它似乎递归地压缩子文件夹中的所有文件.我真的很喜欢上面简单的行脚本.例如:

It works fine if "C:\Test" contains no empty sub-folders, and it seems works recursively to compress all files within sub-folders. I really like above simple line script. For example:

C:\Test
   file1.dat
   file2.dat
   Test-Sub
      File21.bat
      ....

但是,我在一种情况下出错.我发现如果有任何空文件夹,例如C:\Test\EmptySub",

However, I got error in one case. I find that if there is any empty folder such as "C:\Test\EmptySub",

C:\Test
   file1.dat
   file2.dat
   Test-Sub
      File21.bat
      ....
   EmptySub
   AnotherSub
      file31.sp1
      ...

脚本会产生错误.我尝试了以下脚本:

the script will generate an error. I tried the following scripts:

Get-ChildItem $files -Recurse | foreach { if (!$_.PSIsContainer) 
    {$zipFile.CopyHere($_.fullname)}} 

这不能按预期工作.它只是跳过所有子文件夹.不确定是否有过滤器或子句可用于跳过所有空子文件夹?

This does not work as expected. It just skips all the sub-folders. Not sure if there are filter or clause available to skip all the empty sub-folders?

更新:根据建议,我试了一下.我的问题还没有解决.这是我的问题的更新.首先,我更新了上面的脚本以显示 $zipFile 对象是如何创建的.其次,我有建议的代码:

Updated: Based on suggests, I gave it a try. My problem has not be resolved. Here is the update of my question. First, I updated the scripts above to show how $zipFile object is created. Secondly I have the suggested codes:

Get-ChildItem $files | ? {-not ($_.PSIsContainer -eq $True -and 
  $_.GetFiles().Count -eq 0) } | % {$zipfile.CopyHere($_.fullname)}

我在我的 WindowsXP 上尝试了上述更新,它在空子文件夹下工作正常.但是,相同的代码在 Windows 2003 Server 中不起作用.以下是错误信息:

I tried above updates on my WindowsXP, it works fine with empty sub-folders. However, the same codes do not workin in Windows 2003 Server. The following is the error message:

[窗口标题]压缩(zipped)文件夹错误

[Window Title] Compressed (zipped) Folders Error

[内容]未找到文件或没有读取权限.

[Content] File not found or no read permission.

[确定]

不确定此类型的PK对象是否在Windows 2003 server中有效,或者该对象是否有其他设置.

Not sure if this type PK object works in Windows 2003 server, or if there is other settings for the object.

推荐答案

您可以通过针对 get-childitem 的空返回进行测试来检测空目录.例如,这将返回空目录列表

You can detect empty directories by testing against an empty return from get-childitem. For example, this will return the list of empty directories

dir | where{$_.PSIsContainer -and -not (gci $_)}

虽然在你的情况下,你想要相反的:

Though in your case, you want the inverse:

Get-ChildItem $files | where{-not ($_.PSIsContainer -and -not (gci $_))} | foreach {$zipfile.CopyHere($_.fullname)}} 

这篇关于使用PS将文件夹中的文件压缩为zip文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆