控制台版本Rar.exe有命令l根据WinRAR程序文件夹中的文本文件Rar.txt作为手册列出存档文件内容对于控制台版本。可以在批处理文件中处理使用命令l(或L)运行Rar.exe 时的列表输出,以确定RAR 存档文件是否在顶层仅包含单个目录而没有其他内容。但是Rar.exe 支持像免费的UnRAR.exe 只是RAR 档案。
要支持 ZIP 压缩文件,必须使用 GUI 版本 WinRAR.exe,它支持提取 RAR 和 ZIP 压缩文件以及其他一些压缩文件类型。
WinRAR.exe 的手册是 WinRAR 的帮助,可以通过单击菜单项 帮助主题 上的菜单项 帮助 来打开运行WinRAR。在帮助选项卡 Contents 上有一个列表项 Command line mode,在参考的帮助页面中包含所有必要的信息,用于从命令行运行 WinRAR.exe。
查看命令列表可以看出WinRAR.exe 不支持命令l 将存档文件内容输出到控制台窗口,因为它是一个图形用户界面应用程序。
因此,在使用WinRAR.exe 时,如果归档文件在顶层仅包含一个目录,则实际上无法从命令行或批处理文件中确定。
但是,这并不重要,因为首先解析存档文件以获取文件和目录名称,然后使用适当的命令提取存档文件,而无需或在命令行上指定额外的提取文件夹,效率会很低。
首先使用一个 WinRAR 调用和开关 -ad 将每个存档文件提取到子目录中,首先提取所有 *.rar(然后是所有 *.zip)文件更有效使用存档文件的名称,然后消除每个不需要的提取目录,因为相应的存档文件仅包含一个顶级目录。
这种聪明的方法用在下面的批处理文件中,它包含以下附加功能,希望对许多 WinRAR 用户有用:
可以将工作目录指定为调用批处理文件的第一个参数,甚至可以是 UNC 路径。
批处理文件会自动找出 WinRAR.exe 的安装位置,也适用于未安装在默认程序文件目录中的 32 位或 64 位 WinRAR 的用例(如在我所有的电脑上)。
注意:下面发布的注释批处理文件不检查当前或指定目录中是否已经提取了现有存档文件。因此,不建议在一个目录上多次运行批处理文件,一旦处理过的存档文件不会从该目录中删除。
@echo off
rem Change working directory if batch file was started with an argument.
if not "%~1" == "" (
pushd "%~1" 2>nul
if errorlevel 1 (
echo Specified directory "%~1" does not exist.
echo/
pause
goto :EOF
)
)
setlocal EnableExtensions DisableDelayedExpansion
rem Does WinRAR exist in default program files folder?
set "WinRAR=%ProgramFiles%\WinRAR\WinRAR.exe"
if exist "%WinRAR%" goto StartExtraction
rem Does WinRAR exist in default program files folder for x86 applications?
set "WinRAR=%ProgramFiles(x86%\WinRAR\WinRAR.exe"
if exist "%WinRAR%" goto StartExtraction
rem Try to determine installation location of WinRAR.exe from registry.
set "TypeToken=2"
goto GetPathFromRegistry
rem On Windows Vista and later REG.EXE outputs without version info:
rem HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\App Paths\WinRAR.exe
rem (Default) REG_SZ Full path to WinRAR\WinRAR.exe
rem There are only spaces used to separate value name, value type and value string.
rem But REG.EXE version 3.0 outputs on Windows XP with version info:
rem ! REG.EXE VERSION 3.0
rem
rem HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\App Paths\WinRAR.exe
rem <NO NAME> REG_SZ Full path to WinRAR\WinRAR.exe
rem NOTE: There are 4 indent spaces and 2 separating tabs in REG 3.0 output line.
rem So either token 2 or token 3 contains value type REG_SZ
rem used to identify the line with the wanted information.
:GetPathFromRegistry
for /F "skip=1 tokens=%TypeToken%*" %%A in ('%SystemRoot%\System32\reg.exe QUERY "HKLM\Software\Microsoft\Windows\CurrentVersion\App Paths\WinRAR.exe" /ve 2^>nul') do (
if "%%A" == "REG_SZ" (
if exist "%%~fB" (
set "WinRAR=%%~fB"
goto StartExtraction
)
) else if "%%A" == "NAME>" (
set "TypeToken=3"
goto GetPathFromRegistry
)
)
endlocal
if not "%~1" == "" popd
echo Could not determine directory containing WinRAR.exe.
echo/
echo Please configure it manually in file: %~f0
echo/
pause
goto :EOF
rem WinRAR supports multiple archive types on extraction.
rem Specify here the archive file extensions for extraction.
:StartExtraction
for %%I in (rar zip) do call :ExtractArchives %%I
rem Restore previous command environment, restore previous current directory
rem and exit this batch file without fall through to the subroutines below.
endlocal
if not "%~1" == "" popd
goto :EOF
rem The subroutine ExtractArchives processes all archive files in current
rem directory with the file extension passed to subroutine as first argument.
rem WinRAR is called once to extract all files with specified file extension
rem for extraction into a subdirectory with name of the archive file.
rem Then one more subroutine is called for each archive file to determine
rem if it is safe to move the extracted archive file contents up one level.
:ExtractArchives
if not exist "*.%~1" goto :EOF
"%WinRAR%" x -ad -cfg- -ibck -y -- "*.%~1"
for %%A in ("*.%~1") do call :MoveUpExtracted "%%~nA"
goto :EOF
rem The subroutine MoveUpExtracted first checks if for the archive file
rem passed to the subroutine as first argument a subdirectory exists at
rem all, i.e. the extraction before was successful for that archive.
rem Next it counts the subdirectories in the archive extraction directory.
rem Nothing is moved up if there is more than 1 subdirectory in archive
rem extraction directory.
rem Also nothing is moved up if archive extraction directory contains
rem 1 or more files.
rem After verification of archive extraction directory really containing
rem only a single subdirectory and nothing else, the name of the archive
rem extraction directory is compared case-insensitive with the name of
rem the single subdirectory in archive extraction directory. On equal
rem directory names the archive extraction directory is renamed by
rem appending _tmp to make it possible to move the subdirectory with same
rem name up one level in directory hierarchy. There is hopefully by chance
rem never a directory present in current directory with name of an archive
rem file and _tmp appended.
rem Next it is checked if in current directory there is not already existing
rem a directory with name of the subdirectory from extracted archive in which
rem case it is also not possible to move the directory up one level. In this
rem special use case the archive extraction directory is kept containing just
rem a single subdirectory with restoring original directory name.
rem Then the single subdirectory in archive extraction directory is moved up
rem one level which is very fast as just the file allocation table is updated
rem and no data is really moved.
rem The directory movement could fail if the extracted directory has hidden
rem attribute set. In this case temporarily remove the hidden attribute,
rem move the directory up one level in directory hierarchy and set the
rem hidden attribute again on the directory.
rem On a succesful moving up of the extracted directory the (renamed)
rem extraction directory being now empty is deleted as not further needed.
:MoveUpExtracted
if not exist "%~1\" (
echo Error: No folder for archive %~1
goto :EOF
)
echo Processing archive folder "%~1"
set FolderCount=0
set "FolderName="
for /F "delims=" %%D in ('dir "%~1\*" /AD /B 2^>nul') do (
if defined FolderName goto :EOF
set /A FolderCount+=1
set "FolderName=%%D"
)
if not %FolderCount% == 1 goto :EOF
for /F "delims=" %%F in ('dir "%~1\*" /A-D /B 2^>nul') do goto :EOF
set "ParentRenamed=0"
set "ParentFolder=%~1"
if /I "%~1" == "%FolderName%" (
ren "%~1" "%~1_tmp" 2>nul
if errorlevel 1 (
echo Failed to rename "%~1" to "%~1_tmp".
goto :EOF
)
set "ParentFolder=%~1_tmp"
set "ParentRenamed=1"
)
if exist "%FolderName%" (
if %ParentRenamed% == 1 ren "%~1_tmp" "%~1"
echo Error: Folder exists "%FolderName%"
goto :EOF
)
move "%ParentFolder%\%FolderName%" "%FolderName%" >nul 2>nul
if not errorlevel 1 (
rd "%ParentFolder%"
goto :EOF
)
%SystemRoot%\System32\attrib.exe -h "%ParentFolder%\%FolderName%" >nul
move "%ParentFolder%\%FolderName%" "%FolderName%" >nul
if errorlevel 1 (
if %ParentRenamed% == 1 (
ren "%ParentFolder%" "%~1"
goto :EOF
)
)
%SystemRoot%\System32\attrib.exe +h "%FolderName%"
rd "%ParentFolder%"
goto :EOF
我从 Windows 95 开始使用 32 位 Windows,但我从未遇到过 MAX_PATH 限制,即绝对文件/文件夹名称长度超过 259 个字符。
因此,当存档文件名很长时(例如,文件名 + 文件扩展名正好为 256 个字符),重写批处理文件以使其正常工作是一个非常有趣且非常耗时的挑战。
在开发下面的批处理文件期间,我发现了以下内容:
DIR、FOR、RD 和 REN 等一些命令支持路径中的 8.3 短名称AND 文件/文件夹名称,而 ATTRIB 和 MOVE 等其他命令仅在路径中支持它们,而不在文件/文件夹名称中支持它们(至少在 Windows XP 上)。
因此,无法使用 8.3 的短名称移动文件夹或更改其属性。
当具有完整路径的文件夹名称超过 259 个字符时,所有命令都无法仅使用具有相对文件夹路径的相对文件夹名称。这意味着 Windows 命令解释器在执行任何命令之前首先确定具有完整路径的文件夹名称。因此,当前目录应该有一个短路径来处理名称很长的档案或包含一个名称很长的目录。
我无法弄清楚如何使用 %~fs1 获取文件夹的短名称或其路径,正如 call /? 或 %%~fsI(在批处理文件中)所解释的那样,当只有一个相对文件夹路径由 Windows 命令解释器解析,即只是没有路径的文件夹的长名称。
在运行带有选项/X 的命令DIR 以获取目录的短名称时,第三列包含短名称,第四列包含长名称。但是在非常短的文件夹名称中,第三列中的短名称可能会丢失。
dir /AD /X 在英语 Windows 7 SP1 x64 上的输出,在 NTFS 分区上执行,德国在 Windows 区域和语言设置中设置:
Volume in drive C is System
Volume Serial Number is 7582-4210
Directory of C:\Temp\Test
29.04.2017 22:39 <DIR> .
29.04.2017 22:39 <DIR> ..
29.04.2017 22:39 <DIR> ARCHIV~1 archive_with_a_very_very_very_..._long_name_1
29.04.2017 22:39 <DIR> Batch
29.04.2017 22:39 <DIR> xyz
在 FAT32 分区上的德语 Windows XP SP3 x86 上执行相同的命令 dir /AD /X,同时在 Windows 区域和语言设置中设置了德国:
Datenträger in Laufwerk F: ist TEMP
Volumeseriennummer: CAA5-41AA
Verzeichnis von F:\Temp
29.04.2017 22:39 <DIR> .
29.04.2017 22:39 <DIR> ..
29.04.2017 22:39 <DIR> BATCH Batch
29.04.2017 22:39 <DIR> xxx
29.04.2017 22:39 <DIR> ARCHIV~1 archive_with_a_very_very_very_..._long_name_1
注意:很长的目录名在此处被我截断,名称为...。
为什么目录 Batch 在 Windows XP 计算机上具有短名称 BATCH 但在 Windows 7 上没有短名称,这对我来说并不能真正解释。
这里的批处理脚本也支持长存档名称和存档中的长目录名称,只要当前目录的路径很短。
@echo off
rem Change working directory if batch file was started with an argument.
if not "%~1" == "" (
pushd "%~1" 2>nul
if errorlevel 1 (
echo Specified directory "%~1" does not exist.
echo/
pause
goto :EOF
)
)
setlocal EnableExtensions DisableDelayedExpansion
rem Does WinRAR exist in default program files folder?
set "WinRAR=%ProgramFiles%\WinRAR\WinRAR.exe"
if exist "%WinRAR%" goto StartExtraction
rem Does WinRAR exist in default program files folder for x86 applications?
set "WinRAR=%ProgramFiles(x86%\WinRAR\WinRAR.exe"
if exist "%WinRAR%" goto StartExtraction
rem Try to determine installation location of WinRAR.exe from registry.
set "TypeToken=2"
goto GetPathFromRegistry
rem On Windows Vista and later REG.EXE outputs without version info:
rem HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\App Paths\WinRAR.exe
rem (Default) REG_SZ Full path to WinRAR\WinRAR.exe
rem There are only spaces used to separate value name, value type and value string.
rem But REG.EXE version 3.0 outputs on Windows XP with version info:
rem ! REG.EXE VERSION 3.0
rem
rem HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\App Paths\WinRAR.exe
rem <NO NAME> REG_SZ Full path to WinRAR\WinRAR.exe
rem NOTE: There are 4 indent spaces and 2 separating tabs in REG 3.0 output line.
rem So either token 2 or token 3 contains value type REG_SZ
rem used to identify the line with the wanted information.
:GetPathFromRegistry
for /F "skip=1 tokens=%TypeToken%*" %%A in ('%SystemRoot%\System32\reg.exe QUERY "HKLM\Software\Microsoft\Windows\CurrentVersion\App Paths\WinRAR.exe" /ve 2^>nul') do (
if "%%A" == "REG_SZ" (
if exist "%%~fB" (
set "WinRAR=%%~fB"
goto StartExtraction
)
) else if "%%A" == "NAME>" (
set "TypeToken=3"
goto GetPathFromRegistry
)
)
endlocal
if not "%~1" == "" popd
echo Could not determine directory containing WinRAR.exe.
echo/
echo Please configure it manually in file: %~f0
echo/
pause
goto :EOF
rem WinRAR supports multiple archive types on extraction.
rem Specify here the archive file extensions for extraction.
rem But first delete temporary folder from a previous breaked execution.
:StartExtraction
rd /Q /S # 2>nul
for %%I in (rar zip) do call :ExtractArchives %%I
rem Restore previous command environment, restore previous current directory
rem and exit this batch file without fall through to the subroutines below.
endlocal
if not "%~1" == "" popd
goto :EOF
rem The subroutine ExtractArchives processes all archive files in current
rem directory with the file extension passed to subroutine as first argument.
rem WinRAR is called once to extract all files with specified file extension
rem for extraction into a subdirectory with name of the archive file.
rem Then one more subroutine is called for each archive file to determine
rem if it is safe to move the extracted archive file contents up one level.
:ExtractArchives
if not exist "*.%~1" goto :EOF
"%WinRAR%" x -ad -cfg- -ibck -y -- "*.%~1"
for %%A in ("*.%~1") do call :MoveUpExtracted "%%~nA" %1
goto :EOF
rem The subroutine MoveUpExtracted first checks if for the archive file
rem passed to the subroutine as first argument a subdirectory exists at
rem all, i.e. the extraction before was successful for that archive, and
rem determines short 8.3 name of this directory.
rem Next it counts the subdirectories in the archive extraction directory
rem using short directory name. Nothing is moved up if there is more than
rem 1 subdirectory in archive extraction directory.
rem Also nothing is moved up if archive extraction directory contains
rem 1 or more files.
rem After verification of archive extraction directory really containing
rem only a single subdirectory and nothing else, the current archive folder
rem is renamed to # (single character folder name) using short folder name.
rem This folder rename should work in general. The current archive folder
rem is kept in case of this folder rename fails unexpected because it is
rem not yet known if the current directory does not already contain the
rem single directory extracted from current archive or rename failed
rem because of a permission or a directory sharing access restriction.
rem Next it is checked if in current directory there is not already existing
rem a directory with name of the subdirectory from extracted archive in which
rem case it is also not possible to move the directory up one level. In this
rem special use case the archive extraction directory is kept containing just
rem a single subdirectory with restoring original directory name. In case of
rem restoring archive directory fails unexpected, the directory with name #
rem is deleted and the archive is extracted once again into a directory with
rem name of archive file.
rem It is clear on this point that the single directory in archive extraction
rem directory can be moved up to current directory from directory wit having
rem now the temporary name #.
rem Moving a directory with command MOVE is not possible if hidden attribute
rem is set on directory. For that reason it is checked next if the directory
rem to move up has hidden attribute set using its short directory name.
rem In case of directory has hidden attribute is indeed set, it is removed
rem which is also verified. The verification can't be done with errorlevel
rem evaluation as external command ATTRIB does not set errorlevel on failed
rem attribute change. So the attribute check is done once again after the
rem hidden attribute is removed with ATTRIB.
rem ATTRIB also fails to change the attribute if absolute folder path is
rem longer than 259 characters. In this case the current extraction folder
rem with temporary name # is deleted completely and the current archive is
rem extracted once again to current directory without creation of an
rem additional directory with name of archive file.
rem Then the single subdirectory in archive extraction directory having
rem now name # is also renamed to # using short directory name to avoid
rem a problem on next command MOVE with an absolute folder path longer
rem than 259 characters as much as possible.
rem The directory extracted from archive with name # in directory # is
rem moved up to current directory with suppressing all errors which could
rem occur for example if path of current directory plus name of directory
rem as extracted from archive file is too long.
rem The directory # in current directory with its subdirectory # is deleted
rem on a moving error and the current archive file is extracted once again
rem into current directory without creation of an additional directory with
rem name of archive file.
rem But on successful movement of the folder with correct name to current
rem directory the hidden attribute is set on folder if the extracted folder
rem has it also set before moving the folder and the finally empty folder #
rem is deleted before exiting subroutine.
:MoveUpExtracted
set "FolderToCheck=%~f1"
set "FolderToCheck=%FolderToCheck:~0,258%"
for /F "skip=5 tokens=4*" %%X in ('dir "%FolderToCheck%*" /AD /X 2^>nul') do (
if "%%Y" == "%~1" set "ArchiveFolder=%%X" & goto Subfolders
if "%%Y" == "" if /I "%%X" == "%~1" set "ArchiveFolder=%%X" & goto Subfolders
)
echo Error: No folder for archive %~1
goto :EOF
:Subfolders
@echo off
echo Processing archive folder "%~1"
set FolderCount=0
set "FolderName="
for /F "delims=" %%D in ('dir "%ArchiveFolder%\*" /AD /B 2^>nul') do (
if defined FolderName goto :EOF
set /A FolderCount+=1
set "FolderName=%%D"
)
if not %FolderCount% == 1 goto :EOF
for /F "delims=" %%F in ('dir "%ArchiveFolder%\*" /A-D /B 2^>nul') do goto :EOF
ren "%ArchiveFolder%" # 2>nul
if errorlevel 1 (
echo Error: Failed to rename "%~1"
goto :EOF
)
set "FolderToCheck=%~dp1%FolderName%"
set "FolderToCheck=%FolderToCheck:~0,258%"
for /F "skip=5 tokens=4*" %%X in ('dir "%FolderToCheck%*" /AD /X 2^>nul') do (
if "%%Y" == "%FolderName%" goto FolderExist
if "%%Y" == "" if /I "%%X" == "%FolderName%" goto FolderExist
)
set "HiddenFolder=0"
set "FolderToCheck=%~dp1#\%FolderName%"
set "FolderToCheck=%FolderToCheck:~0,258%"
for /F "skip=5 tokens=4*" %%X in ('dir "%FolderToCheck%*" /AD /X 2^>nul') do (
if "%%Y" == "%FolderName%" set "FolderToMove=%%X" & goto CheckHidden
if "%%Y" == "" if /I "%%X" == "%FolderName%" set "FolderToMove=%%X" & goto CheckHidden
)
:CheckHidden
for %%X in ("#\%FolderToMove%") do (
for /F "tokens=2 delims=h" %%H in ("%%~aX") do (
if %HiddenFolder% == 1 goto ArchiveExtract
set "HiddenFolder=1"
%SystemRoot%\System32\attrib.exe -h "#\%FolderName%"
goto CheckHidden
)
)
ren "#\%FolderToMove%" # 2>nul
move #\# "%FolderName%" >nul 2>nul
if errorlevel 1 goto ArchiveExtract
if %HiddenFolder% == 1 %SystemRoot%\System32\attrib.exe +h "%FolderName%"
rd #
goto :EOF
:ArchiveExtract
rd /Q /S #
"%WinRAR%" x -cfg- -ibck -y -- "%~1.%~2"
goto :EOF
:FolderExist
echo Error: Folder exists "%FolderName%"
ren # "%~1" 2>nul
if not errorlevel 1 goto :EOF
rd /Q /S #
"%WinRAR%" x -ad -cfg- -ibck -y -- "%~1.%~2"
goto :EOF
用 C 或 C++ 或 C# 编写一个控制台应用程序肯定会更好,因为在上面的批处理脚本中替换子例程 MoveUpExtracted。
在 Windows 10 版本 1607(周年更新)或更高版本的 Windows 上,可以通过组策略或添加注册表值来禁用 MAX_PATH 260 个字符(259 个字符加上终止空字节)的限制,请参阅
要了解所使用的命令及其工作原理,请打开命令提示符窗口,在其中执行以下命令,并仔细阅读每个命令显示的所有帮助页面。
attrib /?
call /?
dir /?
echo /?
endlocal /?
for /?
goto /?
if /?
move /?
pause /?
popd /?
pushd /?
rd /?
reg /?
reg query /?
rem /?
ren /?
set /?
setlocal /?
另请阅读 Microsoft 文章: