![7-Zip:获取存档中每个文件夹中的文件数量(无需提取)](https://linux22.com/image/1612990/7-Zip%EF%BC%9A%E8%8E%B7%E5%8F%96%E5%AD%98%E6%A1%A3%E4%B8%AD%E6%AF%8F%E4%B8%AA%E6%96%87%E4%BB%B6%E5%A4%B9%E4%B8%AD%E7%9A%84%E6%96%87%E4%BB%B6%E6%95%B0%E9%87%8F%EF%BC%88%E6%97%A0%E9%9C%80%E6%8F%90%E5%8F%96%EF%BC%89.png)
我有例如archive.rar
包含文件的文件夹
存档.rar
\folder_a
\folder_b
\folder_c
如何获取每个文件夹中的文件数量(无需提取)并将结果导出到文本文件,例如(类似以下内容)
列表.txt(期望结果)
\folder_a 580
\folder_b 70
\folder_c 0
我已尝试过7z.exe l archive.rar > list.txt
,但它返回所有文件而不显示每个文件夹中文件的数量。
非常感谢。
答案1
新年热身:利用-slt
(显示技术信息)开关:
@ECHO OFF
SETLOCAL EnableExtensions DisableDelayedExpansion
set "_7zArchive=%~1"
set "_7zVerbose=%~2"
if DEFINED _7zVerbose set "_7zVerbose=%_7zVerbose:-=/%"
if NOT EXIST "%_7zArchive%" goto :Usage
REM echo Checking archive "%_7zArchive%"
echo(
set "_7zExe=c:\Program Files\7-Zip\7z.exe"
rem "%_7zExe%" l -slt "%_7zArchive%" | findstr /I "^Path.= ^Folder.= ^Attributes.="
set "_7zFiles=0"
for /F "usebackq skip=1 tokens=1* delims== " %%G in (`
^""%_7zExe%" l -slt "%_7zArchive%" ^| findstr /I "^Path.= ^Folder.="^"
`) do (
if /I "%%~G"=="Folder" (
set "_7zFldr=%%~H"
call :CountFiles
) else (
set "_7zPath=%%~H"
)
)
if /I NOT "%_7zVerbose%"=="/NF" (
echo %_7zFiles% files in "%_7zArchive%":
set __
)
if /I "%_7zVerbose%"=="/NF" goto :FolderStruct
if /I NOT "%_7zVerbose%"=="/V" goto :endlocal
:FolderStruct
echo folder structure in "%_7zArchive%":
set $$
:endlocal
ENDLOCAL
goto :eof
:CountFiles
for %%g in ("H:\%_7zPath%") do (
if "%_7zFldr%"=="-" (
REM echo file "%%~pg" "%%~nxg" "%%~g"
set /A "_7zFiles+=1"
CALL set "_7z_aux=%%__ %%~pg%%"
set /A "_7z_aux+=1"
CALL set "__ %%~pg=%%_7z_aux%%"
set "$$ %%~pg= " # for folder structure
) else (
REM echo folder "%%~pg" "%%~nxg" "%%~g"
set "$$ %%~pnxg\= " # for folder structure
)
)
goto :eof
:Usage
echo(
echo Get number of files in each folder within archive (without extracting^).
echo(
echo "%~0" archive [-V^|/V ^| -NF^|/NF] [^> ^| ^>^> outfile]
echo(
echo archive Specifies the file to be analysed.
echo -V, /V Verbose: lists folder structure within archive as well.
echo -NF,/NF NoFiles: lists folder structure within archive only.
echo(
echo ^> outfile redirects output (outfile overwrite^).
echo ^>^> outfile redirects output (append to outfile^).
echo(
pause
goto :endlocal
针对来自不同来源的一系列档案进行了测试,如下所示(我希望它.rar
也适用于档案):
for /F "delims=" %A in ('dir /B /S D:\Downloads\*.zip') do @D:\bat\SU\1514106.bat "%~A"
for /F "delims=" %A in ('dir /B /S D:\Downloads\*.zip') do @D:\bat\SU\1514106.bat "%~A" /V
for /F "delims=" %A in ('dir /B /S D:\Downloads\*.zip') do @D:\bat\SU\1514106.bat "%~A" -NF
例如(当然,和的丑陋的原始输出set __
可以set $$
被美化):
D:\bat\SU\1514106.bat "D:\Downloads\hex-master.zip" -v
8 files in "D:\Downloads\hex-master.zip": __ \hex-master\=4 __ \hex-master\build\win\=3 __ \hex-master\src\=1 folder structure in "D:\Downloads\hex-master.zip": $$ \hex-master\= $$ \hex-master\build\= $$ \hex-master\build\win\= $$ \hex-master\src\=