How to mask %....% in string "....."?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
pstein
Posts: 125
Joined: 09 Nov 2011 01:42

How to mask %....% in string "....."?

#1 Post by pstein » 26 Nov 2011 07:16

From a DOS batch script I want to pass a command/string to a program (here:mysql.exe)
This string parameter contains a %....% token which should NOT be replaced by a DOS batch variable but passed exactly as written to mysql.exe
From cmdline it works fine.

How can I prevent replacement? The percentage sign MUST be passed to mysql.

The following does NOT work:

mysql.exe -u myuser -pmypasswd -e "show variables like '%version%';"

mysql.exe -u myuser -pmypasswd -e "show variables like '\%version\%';"

So how could that work?

Thank you
Peter

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: How to mask %....% in string "....."?

#2 Post by Ed Dyreen » 26 Nov 2011 07:19

'
Hi pstein,

unfortunately CMD does not support regular expressions, try:

Code: Select all

mysql.exe -u myuser -pmypasswd -e "show variables like '%%version%%';"

orange_batch
Expert
Posts: 442
Joined: 01 Aug 2010 17:13
Location: Canadian Pacific
Contact:

Re: How to mask %....% in string "....."?

#3 Post by orange_batch » 29 Nov 2011 03:27

There are a few characters that need special handling in batch.

The caret character ^ is batch's escape character, so you would write & like ^&.

It's preferable to write characters within quotation marks (special character processing is disabled within quotation marks, so no problematic carets are needed).

The only two exceptions to that are ! (when delayedexpansion is enabled, needs any number of preceding carets) and %, like your case.

% is easy though. Simply double the character up. %% = %. This is why for loops require %% for its tokens within batch scripts, and not when directly on the command line.

Post Reply