问题
Following an example of tech-recipes I have managed to list the contents of a ZIP file (using 7-Zip:
FOR /F "tokens=* delims=" %%A in ('dir /b /s *.zip') do (7z.exe l -r "%%A" >> listing.txt)
However, this just dumps out the entire directory structure of the ZIP file into a text file (called listing.txt).
I only want to list the directory names of the highest level directories e.g.
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
I would only want the text file to contain:
- Directory One
- Directory Two
- Directory Three
Can anyone suggest how I could achieve this?
回答1:
7z doesn't seem to have builtin key for this, however you can do some batch scripting (this one searches for slash in file name and displays line if slash not found) :
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!
)
)
回答2:
7z.exe l -r "File.7z" | FINDSTR "[0-9].D....\>" | FIND /V "\"
With the first Findstr we filter the output getting a list of only dirs ("D" attribute).
With the second find we delete from the output the paths containing backslashes (Folder\Subfolder).
How to use it with a amount of zips:
@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
Tested structure:
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
Output received:
File: "C:\Users\Administrador\Desktop\Nueva carpeta\x.7z"
1.0: a
2.0: 1
来源:https://stackoverflow.com/questions/16188135/listing-the-contents-of-a-zip-file