Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Escape percent in bat file

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.

Here is the code

@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 

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?

like image 506
user_v Avatar asked Nov 25 '12 14:11

user_v


People also ask

What does @echo off do in Bat?

Example# @echo off prevents the prompt and contents of the batch file from being displayed, so that only the output is visible. The @ makes the output of the echo off command hidden as well.

What does %% bat mean?

Use double percent signs ( %% ) to carry out the for command within a batch file. Variables are case sensitive, and they must be represented with an alphabetical value such as %a, %b, or %c. ( <set> ) Required. Specifies one or more files, directories, or text strings, or a range of values on which to run the command.

What is Exit B in batch file?

EXIT /B at the end of the batch file will stop execution of a batch file. use EXIT /B < exitcodes > at the end of the batch file to return custom return codes. Environment variable %ERRORLEVEL% contains the latest errorlevel in the batch file, which is the latest error codes from the last command executed.

What does %1 mean in a batch file?

When used in a command line, script, or batch file, %1 is used to represent a variable or matched string. For example, in a Microsoft batch file, %1 can print what is entered after the batch file name.


1 Answers

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

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

Use this:

rasdial sl employee1 K%%%%Pxev3=^)g:{#Swc9 
like image 79
ElektroStudios Avatar answered Sep 22 '22 15:09

ElektroStudios