0

我正在尝试使用调用 WinRAR 的脚本提取 ZIP 文件,但名称中带有空格的文件夹被解压缩为My%20Folder. 有任何想法吗?

set rar="%home%\Automation\winrar.exe"
%rar% x "%nugetfileweb%" *.* "%releasefrombuildserver%\web\"
4

1 回答 1

0

无论WinRAR还是任何其他创建 RAR 或 ZIP 档案或提取 RAR / ZIP 档案的压缩工具都使用URL Encodingpercent-encoding

提取后具有%20in 名称而不是空格字符的文件和文件夹已添加到具有%20in 文件/文件夹名称的存档中。

因此,在提取过程中无法将%20所有文件/文件夹名称中的每个替换为空格。

这是此任务的批处理文件:

@echo off
setlocal EnableDelayedExpansion
rem The start folder can be specified as argument of the batch file.
rem The current working directory is used as start folder if batch
rem file is executed without any argument.
set "StartFolder=%~1"
if "%StartFolder%"=="" set "StartFolder=%CD%" else (
  rem Remove backslash from end of passed start folder path if present.
  rem That would not be really necessary but makes output looking better.
  if "%StartFolder:~-1%"=="\" set "StartFolder=%StartFolder:~0,-1%"
)
echo.
echo Searching for files with %%20 in name to replace by space in all folders
echo starting from "%StartFolder%" ...
echo.

set ExitCode=0
set FoundCounter=0
set RenameCounter=0
for /f "usebackq delims=" %%F in (`dir "%StartFolder%\*%%20*" /A-D /B /S 2^> nul`) do (
    set "EncodedName=%%~nxF"
    set "DecodedName=!EncodedName:%%20= !"
    set /A FoundCounter+=1
    ren "%%F" "!DecodedName!" 2>nul
    if errorlevel 1 (
       echo Error: Failed to rename file "%%F"
    ) else (
       echo File "!EncodedName!" renamed to "!DecodedName!"
       set /A RenameCounter+=1
    )
)
if not "!FoundCounter!"=="!RenameCounter!" set ExitCode=1

echo.
if "!FoundCounter!"=="0" (
   echo No file found with %%20 in name.
) else if "!RenameCounter!"=="1" (
   if "!FoundCounter!"=="1" (
      echo Renamed 1 file successfully.
   ) else (
      echo Renamed 1 of !FoundCounter! files successfully.
   )
) else (
   echo Renamed !RenameCounter! of !FoundCounter! files successfully.
)
echo.
echo Searching for folders with %%20 in name to replace by space in all folders
echo starting from "%StartFolder%" ...
echo.

set LoopRun=0
set FoundCounter=0
set RenameCounter=0

rem It is necessary to run the following loop more than once if
rem a folder with %20 in name contains in folder tree a subfolder
rem also with %20 in name. The loop is executed 20 times at most.
:NextRun
set RenameError=no
for /f "usebackq delims=" %%D in (`dir "%StartFolder%\*%%20*" /AD /B /S 2^> nul`) do (
    set "EncodedName=%%~nxD"
    set "DecodedName=!EncodedName:%%20= !"
    ren "%%D" "!DecodedName!" 2>nul
    if errorlevel 1 (
       set RenameError=yes
       if "!LoopRun!"=="20" (
          echo Error: Failed to rename folder "%%D"
          set /A FoundCounter+=1
       )
    ) else (
       echo Folder "!EncodedName!" renamed to "!DecodedName!"
       set /A RenameCounter+=1
       set /A FoundCounter+=1
    )
)

if "!RenameError!"=="yes" (
   set /A LoopRun+=1
   if not "!LoopRun!"=="21" goto NextRun
)
if not "!FoundCounter!"=="!RenameCounter!" set /A ExitCode+=2

echo.
if "!FoundCounter!"=="0" (
   echo No folder found with %%20 in name.
) else if "!RenameCounter!"=="1" (
   if "!FoundCounter!"=="1" (
      echo Renamed 1 folder successfully.
   ) else (
      echo Renamed 1 of !FoundCounter! folders successfully.
   )
) else (
   echo Renamed !RenameCounter! of !FoundCounter! folders successfully.
)
echo.
pause

if "!ExitCode!"=="1" (
   endlocal
   rem At least 1 file could not be renamed.
   exit /B 1
) else if "!ExitCode!"=="2" (
   endlocal
   rem At least 1 folder could not be renamed.
   exit /B 2
) else if "!ExitCode!"=="3" (
   endlocal
   rem At least 1 file and 1 folder could not be renamed.
   exit /B 3
) else (
   endlocal
   rem Success as no file / folder to rename found or all renamed.
   exit /B 0
)

例如文件夹C:\Temp包含以下文件夹和文件:

  • 我的%20文件夹
    • 子%20文件夹
      • 一个%20More%20File.txt
      • 文本文件.txt
    • 另一个%20File.txt
    • 文件%201.txt
    • 文件2.txt

C:\Temp上面的批处理文件输出作为参数运行它:

Searching for files with %20 in name to replace by space in all folders
starting from "C:\Temp" ...

File "Another%20File.txt" renamed to "Another File.txt"
File "File%201.txt" renamed to "File 1.txt"
File "One%20More%20File.txt" renamed to "One More File.txt"

Renamed 3 of 3 files successfully.

Searching for folders with %20 in name to replace by space in all folders
starting from "C:\Temp" ...

Folder "My%20Folder" renamed to "My Folder"
Folder "Sub%20Folder" renamed to "Sub Folder"

Renamed 2 of 2 folders successfully.

Press any key to continue . . .

C:\Temp批量执行后文件夹包含:

  • 我的文件夹
    • 子文件夹
      • 再来一份文件.txt
      • 文本文件.txt
    • 另一个文件.txt
    • 文件 1.txt
    • 文件2.txt

批处理文件可以从另一个批处理文件中调用。它有 4 个退出代码(错误级别),其中批处理文件在成功时以值0退出。有关返回值的详细信息,请参阅批处理文件代码底部的代码行。

于 2014-07-25T20:27:05.110 回答