Batch file 为什么用WinRAR从命令行提取的文件和文件夹名称中有%20而不是空格?

Batch file 为什么用WinRAR从命令行提取的文件和文件夹名称中有%20而不是空格?,batch-file,command-line,rar,winrar,Batch File,Command Line,Rar,Winrar,我正试图使用调用WinRAR的脚本提取ZIP文件,但名称中带有空格的文件夹正在解压缩为My%20文件夹。有什么想法吗 set rar="%home%\Automation\winrar.exe" %rar% x "%nugetfileweb%" *.* "%releasefrombuildserver%\web\" 无论是WinRAR还是创建RAR或ZIP存档或提取RAR/ZIP存档的任何其他压缩工具,都将分别使用 提取后名称为%20而非空格字符的文件和文件夹已添加到文件/文件夹名称为%20的

我正试图使用调用WinRAR的脚本提取ZIP文件,但名称中带有空格的文件夹正在解压缩为
My%20文件夹
。有什么想法吗

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

无论是WinRAR还是创建RAR或ZIP存档或提取RAR/ZIP存档的任何其他压缩工具,都将分别使用

提取后名称为
%20
而非空格字符的文件和文件夹已添加到文件/文件夹名称为
%20
的存档中

因此,在提取过程中,无法用空格替换所有文件/文件夹名称中的每个
%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
      • Text_File.txt
    • 另一个%20File.txt
    • 文件%201.txt
    • File2.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
包含:

  • 我的文件夹
    • 子文件夹
      • 再来一个File.txt文件
      • Text_File.txt
    • 另一个文件.txt
    • 文件1.txt
    • File2.txt

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

zip和rar不会像那样重命名文件。他们没有理由对文件名中的空格进行url编码。最有可能的是,原来的zip是用%20创建的。您好,谢谢。我在看原始的nuget包,没有url编码。我并不是说你错了,但是/这个脚本还有更多的地方可能在做这件事。我会将您的信息传递给我的同事请将
%rar%t archive.rar
的输出添加到您的问题中。我刚刚遇到,这说明了如何使用nuget发布文件,这是首选方法。无论如何谢谢你