批处理文件中的网址解码

Urldecode in batch file

如何在批处理文件中 'urldecode' 以下字符串?我需要更改以下内容

http://www.example.com/some-page/some/link.html

至此

http://www.example.com/some-page/some/link.html

可以在 Javascript 中使用 decodeURIComponent() 完成,但我不知道如何在 MSDOS 等中的批处理文件中完成。

 @if (@x)==(@y) @end /***** jscript comment ******
     @echo off
    set "url=http://www.example.com/some-page/some/link.html"
     cscript //E:JScript //nologo "%~f0" "%url%" 
     exit /b 0

 @if (@x)==(@y) @end ******  end comment *********/

 var args=WScript.Arguments;
 //WScript.Echo(args.Item(0));
 WScript.Echo(decodeURIComponent(args.Item(0)));

虽然这绝对不是 uri 编码url...

It can be done in Javascript with decodeURIComponent() but I've no idea how to do it in a batch file just within MSDOS etc.

没有 decodeURIComponent(),没有。尽管如此,解决方案仍然在于调用 JavaScript。您可以通过 JScript 调用 IE7 JavaScript 引擎。 See this page 有关批处理 + JScript 混合脚本的更多示例。

没有内置的 JavaScript 函数可以将 HTML 个实体(ÿ 个字符)转换回纯文本,但您自己动手并不难。

@if (@CodeSection == @Batch) @then

:: batch portion

@echo off
setlocal

set "url=http://www.example.com/some-page/some/link.html"

cscript /nologo /e:JScript "%~f0" "%url%"

goto :EOF

@end
// JScript portion

function decodeEntities(what) {
    return what.replace(/&#x([0-9a-f]{2});/ig, function(m, ) {
        return String.fromCharCode(parseInt(, 16));
    });
}

WSH.Echo(decodeEntities(WSH.Arguments(0)));

作为学术练习,这里提供了一种将 HTML 字符实体转换为 ASCII 的纯批处理方法。它比 JScript 混合体稍慢,并且可能会错误处理包含感叹号的字符串。

@echo off
setlocal enabledelayedexpansion

set "url=http://www.example.com/some-page/some/link.html"

set "url=%url:&#x=;0x%"

:begin
for %%I in (%url%) do (
    set "chunk=%%~I"
    if "!chunk:~0,2!"=="0x" if "!chunk:~4,1!"=="" (
            for /f "delims=" %%x in (
                'forfiles /m "%~nx0" /c "cmd /c echo(%%~I"'
            ) do set "url=!url:;%%I;=%%~x!"
            goto begin
        )
    )
)

echo !url!

这可以在纯批处理中实现...

@echo off
setlocal EnableDelayedExpansion

set "input=http://www.example.com/some-page/some/link.html"

rem Define the equivalences
for %%a in ("#x3a=:" "#x2f=/") do (
   for /F "tokens=1,2 delims==" %%b in (%%a) do set "replace[%%b]=%%c"
)

echo Input = "%input%"
set "input=%input:&=\%"
set "output="
for %%a in (%input%) do (
   for /F "tokens=1,2 delims=\" %%b in ("%%a")  do (
      if "%%c" neq "" (
         set "output=!output!%%b!replace[%%c]!"
      ) else (
         set "term=%%a"
         if "!term:~0,1!" equ "\" (
            set "output=!output!!replace[%%b]!"
         ) else (
            set "output=!output!%%b"
         )
      )
   )
)
echo Output = "%output%"