0
我試圖使用調用WinRAR的腳本來提取ZIP文件,但名稱中具有空格的文件夾正在解壓縮爲My%20Folder
。有任何想法嗎?爲什麼從命令行用WinRAR提取的文件和文件夾有%20而不是名稱空間?
set rar="%home%\Automation\winrar.exe"
%rar% x "%nugetfileweb%" *.* "%releasefrombuildserver%\web\"
我試圖使用調用WinRAR的腳本來提取ZIP文件,但名稱中具有空格的文件夾正在解壓縮爲My%20Folder
。有任何想法嗎?爲什麼從命令行用WinRAR提取的文件和文件夾有%20而不是名稱空間?
set rar="%home%\Automation\winrar.exe"
%rar% x "%nugetfileweb%" *.* "%releasefrombuildserver%\web\"
無論WinRAR的或任何其他壓縮工具創建RAR或ZIP檔案或提取RAR/ZIP檔案使用URL Encoding分別percent-encoding。
%20
在文件/文件夾名稱中已添加%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
包含下列文件夾和文件:
以上批處理文件輸出以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
批量執行後包含:
批處理文件可以從另一個批處理文件被調用。它有4個退出代碼(錯誤級別),批處理文件成功退出,其值爲。有關返回值的詳細信息,請參閱批處理文件代碼底部的代碼行。
zip和rar不會重命名這樣的文件。他們沒有理由對文件名中的空格進行網址編碼。最有可能的原始郵編是在%20已經創建的地方創建的。 –
嗨,謝謝。我看着原始的nuget包,沒有url編碼。我不是說你錯了,/這個腳本還有很多可以做的事情。我會將你的信息傳遞給我的同事 – DavidB
請將'%rar%t archive.rar'的輸出添加到你的問題中。 –