如何通过WinRAR命令行读取RAR文件的注释内容
提取多个RAR文件注释到文本文件的解决方案
我明白你的痛点——WinRAR -z命令确实只能用来查看或设置注释,没法直接通过管道输出内容到文件。不过有几种可靠的方法可以实现批量提取RAR注释的需求,下面分两种场景介绍:
方法1:使用WinRAR命令行结合批处理脚本(无需额外依赖)
WinRAR的i命令会输出压缩文件的详细信息,其中包含注释内容。我们可以通过过滤输出的方式提取注释:
单个RAR文件提取
执行以下命令,会把example.rar的注释提取到example_comment.txt:
@echo off WinRAR i -c- "example.rar" > temp_info.txt :: 找到注释起始的分隔线,提取之后的内容 for /f "skip=2 delims=" %%a in ('findstr /n "^" temp_info.txt ^| findstr /b "----"') do set "skip_line=%%a" for /f "tokens=1 delims=:" %%a in ("%skip_line%") do set /a "start_line=%%a+1" more +%start_line% temp_info.txt > "example_comment.txt" del temp_info.txt
批量处理当前目录下所有RAR文件
把下面的代码保存为extract_rar_comments.bat,双击运行即可为每个RAR文件生成对应的注释文件(比如test.rar生成test_comment.txt):
@echo off setlocal enabledelayedexpansion for %%f in (*.rar) do ( echo Processing %%f... WinRAR i -c- "%%f" > temp_info.txt :: 定位注释起始的分隔线 set "start_line=0" for /f "tokens=1 delims=:" %%a in ('findstr /n "^----" temp_info.txt') do set /a "start_line=%%a+1" if !start_line! gtr 0 ( more +!start_line! temp_info.txt > "%%~nf_comment.txt" ) else ( echo No comment found in %%f > "%%~nf_comment.txt" ) ) del temp_info.txt echo All comments extracted successfully! pause
方法2:使用PowerShell脚本(更稳定,适合复杂场景)
通过WinRAR的COM对象可以直接获取注释内容,不需要过滤命令行输出,可靠性更高:
单个RAR文件提取
# 替换为你的RAR文件路径 $rarPath = "C:\path\to\your\file.rar" $outputPath = "C:\path\to\output\comment.txt" $rarCom = New-Object -ComObject WinRAR.RAR $archive = $rarCom.OpenArchive($rarPath) $comment = $archive.Comment # 输出到文件 $comment | Out-File -FilePath $outputPath -Encoding UTF8 # 清理COM对象 $archive.Close() [System.Runtime.Interopservices.Marshal]::ReleaseComObject($rarCom) | Out-Null Write-Host "Comment extracted to $outputPath"
批量处理指定目录下的所有RAR文件
# 替换为要处理的目录路径 $targetDir = "C:\path\to\your\rar\files" $outputDir = "C:\path\to\save\comments" # 创建输出目录(如果不存在) if (-not (Test-Path $outputDir)) { New-Item -ItemType Directory -Path $outputDir | Out-Null } $rarCom = New-Object -ComObject WinRAR.RAR Get-ChildItem -Path $targetDir -Filter *.rar | ForEach-Object { $archivePath = $_.FullName $outputPath = Join-Path $outputDir "$($_.BaseName)_comment.txt" try { $archive = $rarCom.OpenArchive($archivePath) $comment = $archive.Comment if ([string]::IsNullOrEmpty($comment)) { "No comment found in $($_.Name)" | Out-File -FilePath $outputPath -Encoding UTF8 } else { $comment | Out-File -FilePath $outputPath -Encoding UTF8 } $archive.Close() Write-Host "Processed $($_.Name)" } catch { Write-Error "Failed to process $($_.Name): $_" } } # 清理COM对象 [System.Runtime.Interopservices.Marshal]::ReleaseComObject($rarCom) | Out-Null Write-Host "Batch processing completed!"
注意:使用COM对象的方法需要确保WinRAR已经正确注册(通常安装WinRAR时会自动完成),如果遇到COM对象创建失败的问题,可以尝试重新安装WinRAR。
内容的提问来源于stack exchange,提问作者Itay Braha
相关产品推荐
相关产品推荐

