使用PowerShell对zip文件中的文件和文件夹进行计数 [英] Count files and folders in a zip file using PowerShell

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

问题描述

如何计算zip文件中的文件和文件夹?
我正在运行备份验证,需要将zip文件的文件夹结构与Windows文件夹的文件夹结构进行比较。
的最终目标是具有一个布尔值,如果源计数等于zip内容计数。
到目前为止,这是我所拥有的:

How do you count the files and folders in a zip file? I am running a verification of backups and need to compare the folder structure of the zip file against the folder structure of the windows folder is was made from. The final goal is to have a boolean value of if the source count is equal to the zip content count. This is as far as I have, so far:

 [Reflection.Assembly]::LoadWithPartialName('System.IO.Compression.FileSystem') |out-null
 $ZipContents=[IO.Compression.ZipFile]::OpenRead($FilePath).Entries
 $ZipContents.Count

这得到了所有内容的原始计数,但是希望有不同数量的文件,文件夹数量或某种比较结构的方法。使用7-zip也可以。这些zip文件可能会变得非常大,因此,首先提取文件将是非常耗时的选择。

This gets a raw count of everything, but hoping for a distinct count of files and a count of folders or some way to compare structures. Using 7-zip would be fine, also. These zip files can get very large, so extracting first would be too time consuming to be an option.

推荐答案

如果可以使用 7z.exe ,您也许可以执行以下操作:

If you can use 7z.exe, you may be able to do something like this:

& 'C:\Program Files\7-Zip\7z.exe' l <zipfilename> |
  Select-Object -Last 1 |
  Select-String '([0-9]+) files, ([0-9]+) folders' |
  ForEach-Object {
    $fileCount = [Int] $_.Matches[0].Groups[1].Value
    $dirCount = [Int] $_.Matches[0].Groups[2].Value
  }

这篇关于使用PowerShell对zip文件中的文件和文件夹进行计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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