我有例如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\=