bat文件中的转义百分比

bat文件中的转义百分比

本文介绍了bat文件中的转义百分比的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用批处理文件编写 VPN 拨号器/断开器,但卡住了.经过一番调查,我发现密码中%的存在效果不佳.

I was trying to write a VPN dialer/disconnector using a batch file, but I got stuck. After some investigation I found that the presence of % in the password is not playing well.

这是代码

@echo OFF
SET SWITCHPARSE1=%1
SET SWITCHPARSE2=%2
REM echo %SWITCHPARSE1%
SHIFT
SHIFT
IF "SWITCHPARSE1" == "" goto Usage
IF "SWITCHPARSE1" == "/?" goto Usage
IF "SWITCHPARSE2" == "" goto Usage
IF "SWITCHPARSE2" == "/?" goto Usage


IF "%SWITCHPARSE1%" == "sl" (
    IF "%SWITCHPARSE2%" == "conn" (
        echo "inside sl conn"
        rasdial sl employee1 K%%Pxev3=)g:{#Swc9
        goto end
    ) ELSE IF "%SWITCHPARSE2%" == "disconn" (
        rasdial sl /disconnect
        goto end
    ) ELSE (
        goto Usage
    )
) ELSE IF "%SWITCHPARSE1%" == "off" (
        IF "%SWITCHPARSE2%" == "conn" (
        rasdial Office employee1 office123
        goto end
    ) ELSE IF "%SWITCHPARSE2%" == "disconn" (
        rasdial Office /disconnect
        goto end
    ) ELSE (
        goto Usage
    )
) ELSE (
    goto Usage
)

:Usage
echo "Usage is vpnconn.bat /[sl|off] /[conn|disconn]"

:end

在上面的脚本中,我试图使用 %(即 %%,参考来自 此处),但 bat 脚本给出了 g:{#Swc9 此时出乎意料..

In the above script I am trying to escape % using % (i.e. %%, reference from here), but the bat script gives g:{#Swc9 was unexpected at this time..

为了进一步解决根本原因,我尝试在不同的批处理文件中将 %%(转义 %)加倍,并且成功了:

To root cause further, I tried to double %% (escape %) in a different batch file, and it worked:

@echo OFF
rasdial sl employee1 K%%Pxev3=)g:{#Swc9

为什么相同的脚本在集成到不同的连接时不起作用?

Why does the same script when integrated to work with different connections not work?

推荐答案

两个%%等于一个%.没错(但只在脚本中,不能直接在cmd中),不需要使用转义符^.

Two %% equals to one %. That's right (but only in a script, not directly in the cmd), no need to use the escape operator ^.

但是您缺少聚合运算符 ).那就是问题所在;IF 在您的命令中找到 ) 时关闭.

But you are missing the agrupation operator ). That's the problem; the IF closes when it finds the ) at your command.

使用这个:

rasdial sl employee1 K%%%%Pxev3=^)g:{#Swc9

这篇关于bat文件中的转义百分比的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 12:36