2016-03-21 7 views
4

私は交換したい! %21(url encode)をバッチで使用すると、私のコードはうまく機能しますが、エレガントではありません。交換方法!バッチで%21にエレガントに?

set pwd=!xxxxxxx 
set pwd=%pwd:[email protected]@% 
setlocal enableDelayedExpansion 
echo !pwd:@[email protected]=%%21! 
setlocal disabledelayedexpansion 
+1

「^!」や「^^!」のように置き換えて、 '!'をエスケープすると、遅延拡張を切り替える必要はないかもしれません。 – aschipfl

+1

@aschipfl ,!遅延拡張をトグルする必要はありませんが、%%が必要です... – jils2013

+0

'%%'はバッチで2倍にする必要があります(または 'cmd'で直接' ^% 'のようにエスケープする必要があります)... – aschipfl

答えて

2
@echo off 
set pwd=!xxxxxxx 
set pwd=%pwd:[email protected]@% 
setlocal enableDelayedExpansion 
echo !pwd:@[email protected]=%%^21! 
setlocal disabledelayedexpansion 

それはあなたがそれを避けるために%%^2を使用できるようにスクリプトの2番目の引数で%2を交換しようとします。

+0

tks、より厳密な... 1つのセットの文章で置き換えを完了するための任意のアイデア? – jils2013

+0

@ jils2013 - シングルセットのステートメントでは可能ではないと思います... – npocmaka

0

私は将来のために(重要)オリジナルを保つ異なる変数に異なる値を格納することを好む:

@ECHO OFF 
SETLOCAL EnableExtensions DisableDelayedExpansion 
set "pwd=!xxx" 
set "pwX=%pwd:[email protected]@%" 
set pw 
setlocal enableDelayedExpansion 
echo EDE !pwX:@[email protected]=%%21! 
set "pwdAux=!pwX:@[email protected]=%%21!" 
endlocal&set "pwX=%pwdAux%" 
set pw 
echo pwd %pwd% 
echo pwX %pwX% 

は出力

==> D:\bat\SO\36125004.bat 
pwd=!xxx 
[email protected]@xxx 
EDE %21xxx 
pwd=!xxx 
pwX=%21xxx 
pwd !xxx 
pwX %21xxx 

==> 
2

あなたはPowerShellを経由して、.NET System.Uri.EscapeUriString()メソッドを呼び出すことができ、一揃いのライナーのためにfor /fでキャプチャしました。

for /f "usebackq delims=" %%I in (`powershell "[uri]::EscapeUriString('%pwd%')"`) do set "pwd=%%~I" 

私はそれはそれはエレガントとして適格かどうかはあなた次第だと思います。 * shrug * [uri]::EscapeUriString()に加えて、あなたは[uri]::EscapeDataString()で遊んで、どの方法で結果が得られるかを知ることができます。

少し速く実行すると、JavaScriptのencodeURI()またはencodeURIComponent()をデータに実行するためにJScriptハイブリッドコードを使用できます。

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

@echo off & setlocal 

set "pwd=http://www.google.com/search?q=Hello world!&gws_rd=ssl" 

for /f "delims=" %%I in ('cscript /nologo /e:JScript "%~f0" "%pwd%"') do set "pwd=%%I" 

setlocal enabledelayedexpansion 
echo(!pwd! 
endlocal 

goto :EOF 

@end // end Batch/begin JScript 

WSH.Echo(encodeURI(WSH.Arguments(0))) 
4

コードを単純化できないだけでなく、コードが複雑ではないと私は主張します。次のいずれかの初期pwd値が既に@[email protected]

  • 初期pwd値が既に%21が含まれている含まれてい

    • を発生した場合

      あなたのコードは失敗します。おそらくあなたは、おそらく値も含めることができるので、簡単に引用符を追加すると、毒の文字の問題を解決することはできません>

    %

  • %25として初期値が &|<のような任意の毒の文字が含まれてエンコードしたいと思います引用符。したがって、あなたがそれをどのように引用しても、 "this & that" & the other thingのような値は失敗します。

    ここでは、null、キャリッジリターン、および改行を除く任意の文字セットを処理できる堅牢なコードがあります。 !%21と符号化し、%%25と符号化する。他のエンコーディングは行われません。あなたはENDLOCAL後に変数を保持したい時には

    @echo off 
    setlocal disableDelayedExpansion 
    
    :: Set and display the initial value 
    set pwd=;"This & that" ^& the other thing! %%25 %%21 @A @Q @P @E 
    echo Before encoding: 
    set pwd 
    echo(
    
    
    :: Encoding begins here 
        setlocal enableDelayedExpansion 
    
        :: Protect @ as @A 
        set "pwd=!pwd:@[email protected]!" 
    
        :: Protect " as @Q 
        set "pwd=!pwd:"[email protected]!" 
    
        :: Protect % as @P 
        set "pwd=!pwd:%%[email protected]!" 
    
        :: Encode ! as %21 in two steps 
        set "pwd=%pwd:[email protected]%" 
        set "pwd=!pwd:@E=%%21!" 
    
        :: Encode protected % as %25 
        set "pwd=!pwd:@P=%%25!" 
    
        :: Restore protected Q 
        set "pwd=!pwd:@Q="!" 
    
        :: Restore protected @ 
        set "pwd=!pwd:@[email protected]!" 
    
        :: Display the result 
        echo After encoding: 
        echo !pwd! 
    
    endlocal 
    

    --OUTPUT--

    Before encoding: 
    pwd=;"This & that" & the other thing! %25 %21 @A @Q @P @E 
    
    After encoding: 
    ;"This & that" & the other thing%21 %2525 %2521 @A @Q @P @E 
    

    。これはFOR/Fを介して簡単に実行できます。EOLとDELIMSの両方のオプションを無効にするには、特殊な構文が必要です。

    必要なコードの量をより正確に表示するために、すべてのコメントを削除しました。

    @echo off 
    setlocal disableDelayedExpansion 
    
    set pwd=;"This & that" ^& the other thing! %%25 %%21 @A @Q @P @E 
    echo Before encoding: 
    set pwd 
    echo(
    
    setlocal enableDelayedExpansion 
    set "pwd=!pwd:@[email protected]!" 
    set "pwd=!pwd:"[email protected]!" 
    set "pwd=!pwd:%%[email protected]!" 
    set "pwd=%pwd:[email protected]%" 
    set "pwd=!pwd:@E=%%21!" 
    set "pwd=!pwd:@P=%%25!" 
    set "pwd=!pwd:@Q="!" 
    for /f delims^=^ eol^= %%A in ("!pwd:@[email protected]!") do (
        endlocal 
        set "pwd=%%A" 
    ) 
    
    echo After encoding: 
    set pwd 
    

    - OUTPUT - あなたは無効に遅延拡張と(および終了)を起動したときに

    Before encoding: 
    pwd=;"This & that" & the other thing! %25 %21 @A @Q @P @E 
    
    After encoding: 
    pwd=;"This & that" & the other thing%21 %2525 %2521 @A @Q @P @E 
    
    通常

    の上に表示/ F技術のためのシンプルでのみ動作します。 %%Aが拡張され、遅延拡張が有効になっている場合、!を保護するために追加コードが必要です。しかし、すでに!
    %21とエンコードしているため、ENDLOCALの後に遅延拡張が有効になっていても問題ありません。

    +0

    新しい質問は、 "="を置き換えますか? – jils2013

    +0

    @ jils2013 - これは、純粋なバッチを使用して効率を上げることはほとんど不可能です。詳細はhttp://www.dostips.com/forum/viewtopic.php?t=1485を参照してください。 – dbenham

    +0

    @ jils2013、[this](https://stackoverflow.com/a/37823807)をチェック... – aschipfl

    関連する問題