列出ZIP文件的内容

时间:2013-04-24 09:18:00

标签: batch-file zip 7zip

按照技术配方的例子,我设法列出了ZIP文件的内容(使用7-Zip:

FOR /F "tokens=* delims=" %%A in ('dir /b /s *.zip') do (7z.exe l -r "%%A" >> listing.txt)

但是,这只是将ZIP文件的整个目录结构转储到一个文本文件(名为listing.txt)中。

我只想列出最高级目录的目录名,例如

Example.Zip

7-Zip 9.20  Copyright (c) 1999-2010 Igor Pavlov  2010-11-18

Listing archive: C:\Users\Test\Desktop\7zip\Demo.zip

--
Path = C:\Users\Test\Desktop\7zip\Demo.zip
Type = zip
Physical Size = 1252

   Date      Time    Attr         Size   Compressed  Name
------------------- ----- ------------ ------------  ------------------------
2013-04-24 13:12:26 D....            0            0  Directory Three\Sub Folder One
2013-04-24 13:13:00 D....            0            0  Directory Three\Sub Folder Three
2013-04-24 13:12:54 D....            0            0  Directory Three\Sub Folder Two
2013-04-24 13:12:26 D....            0            0  Directory Two\Sub Folder One
2013-04-24 13:13:00 D....            0            0  Directory Two\Sub Folder Three
2013-04-24 13:12:54 D....            0            0  Directory Two\Sub Folder Two
2013-04-24 13:12:26 D....            0            0  Directory One\Sub Folder One
2013-04-24 13:13:00 D....            0            0  Directory One\Sub Folder Three
2013-04-24 13:12:54 D....            0            0  Directory One\Sub Folder Two
------------------- ----- ------------ ------------  ------------------------
                                     0            0  0 files, 9 folders

我只希望文本文件包含:

  • 目录一
  • 目录二
  • 目录三

有人可以建议我如何实现这个目标吗?

2 个答案:

答案 0 :(得分:2)

7z似乎没有内置密钥,但你可以做一些批处理脚本(这个在文件名中搜索斜杠,如果找不到斜杠则显示行):

7z.exe l -r archive.zip > lines.txt

@echo off

setlocal ENABLEDELAYEDEXPANSION

for /f "tokens=*" %%a in (lines.txt) do (
  set line=%%a
  set srch=!line:\=!
  if "!line!" == "!srch!" (
     echo !line!
  )
)

答案 1 :(得分:0)

7z.exe l -r "File.7z" | FINDSTR "[0-9].D....\>" | FIND /V "\"

使用第一个Findstr,我们过滤输出,得到一个只有dirs(“D”属性)的列表。

使用第二个find我们从输出中删除包含反斜杠的路径(Folder \ Subfolder)。

如何使用拉链:

@echo off

(For /R %%# in (*.zip) do (

    Echo File: "%%#" | MORE

    For /F "Tokens=5,*" %%A in (
        '7z.exe l -r "%%#" ^| FINDSTR "[0-9].D....\>" ^| FIND /V "\"'
    ) do (
        Set /A "Num+=1"
        Call Echo %%NUM%%.0: %%B
    )

    Set /A "Num=0"
    Echo+

))>"listing.txt"

Pause&Exit

经测试的结构:

   Date      Time    Attr         Size   Compressed  Name
------------------- ----- ------------ ------------  ------------------------
2013-04-24 13:02:29 ....A          274          167  ..bat
2013-04-24 13:02:29 ....A          274               1\2\2.bat
2013-04-24 13:02:29 ....A          274               a\b\c\c.bat
2013-04-24 13:12:45 D....            0            0  a\b\c
2013-04-24 13:12:37 D....            0            0  a\b
2013-04-24 13:12:33 D....            0            0  a
2013-04-24 13:12:50 D....            0            0  1\2
2013-04-24 13:12:24 D....            0            0  1
------------------- ----- ------------ ------------  ------------------------
                                   822          167  3 files, 5 folders

收到的结果:

File: "C:\Users\Administrador\Desktop\Nueva carpeta\x.7z" 

1.0: a
2.0: 1