Change literal % to something else...then change it back to %

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Jer
Posts: 177
Joined: 23 Nov 2014 17:13
Location: California USA

Change literal % to something else...then change it back to %

#1 Post by Jer » 23 Feb 2016 17:58

My current project examines a command line entry for several options, each with a leading
forward slash, and an optional text string following an open bracket.

I am attempting to work through all the special characters by changing them to something else,
then changing back to the original character, used as a literal in the text string, after it has been
determined that the entry is in fact a text string (because of the opening bracket). Entry
exceptions: a caret is required with the ampersand, and %% is required to display a percent symbol.

I am trying to get the %%, entered for a literal % character, converted to an unused character while the command
line string is being tested, then after it has been determined that a text string has been entered, convert it
back. Is this possible? The code below does not work but it shows the conversion logic.

Code: Select all

@Echo Off
setlocal enabledelayedexpansion

set "str=50%% off"
set "str=%str:%%=ò%"& rem change percent to character for ascii code 149
echo str after 1st convert: %str%

echo do some stuff with str...

set "str=%str:ò%=%%"& rem change character for ascii code 149 to percent
echo str after 2nd convert: %str%

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Change literal % to something else...then change it back to %

#2 Post by foxidrive » 23 Feb 2016 18:47

My comment, on noticing that percent signs are the poison characters being manipulated, is to ask what the actual task is. One of the readers may have a better way to do what it is you have to do.

jeb
Expert
Posts: 1055
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

Re: Change literal % to something else...then change it back to %

#3 Post by jeb » 23 Feb 2016 19:05

Hi Jer,

as foxidrive said, it would be useful to explain more of the task you want to solve and why do you think that you need to change the special characters at all.
For me, it sounds like you want to build a bullet proof batch file, but perhaps I'm wrong.

You want to examine command line parameters, then the first problem is to retrieve the parameters without destroying them and not beeing influenced by special characters.
This can be handled with the so called "REM technic".

The next problem is to work with variables without getting trouble with special characters.

The second task can be solved with delayed expansion, as delayed expansion can handle any content without problems.
But you will get other problems with delayed expansion, like to transfer variables out of the current scope (endlocal barrier).

For all this stuff you can find solutions here at dostips or at stackoverflow.

jeb

Jer
Posts: 177
Joined: 23 Nov 2014 17:13
Location: California USA

Re: Change literal % to something else...then change it back to %

#4 Post by Jer » 23 Feb 2016 21:37

That's right, trying to bullet-proof the code. I try to convince myself that just one more fix
will make the code bullet-proof.

The batch file is based on Aacini's ColorShow.exe and includes his method of reading
a text file and assigning lines to an array for display with ColorShow. The current code
includes adding a border character of your choice, from ascii codes 33-254, and this is working.
There are two ways to bring in your text, by naming the text file, or by entering text for display.
As of now, everything seems to be working except the %% I mention below.

Here is a typical command line to display text from a file:
batch_file_name left /b08 /t1e /i4 177 myownfile.txt

where text is left justified, border colors are gray on black, text area colors are light yellow on blue,
indent the frame 4 columns, use ascii code 177 for the border character, display all text from
myownfile.txt

To display text typed on the console:
batch_file_name /be1 /t0f /w+4 { Your Message Here }

these options include adding 4 columns to the width, insert a line above the centered text and a line below.

If I enter {"save 50%% ^& more } there's an error, and I'd rather not have to deal with the %%
if I can easily replace them with an unused character, since I am replacing all other poison characters
temporarily.

Unless there is some rule or reason not to, I would like to share the batch file with the forum
for review, correction, hints of better ways to do a particular operation, suggestions on improving
program flow, etc. This would come after a period of testing the file and studying it for code
line reduction.
Jerry

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Change literal % to something else...then change it back to %

#5 Post by foxidrive » 23 Feb 2016 23:01

Jer wrote:If I enter {"save 50%% ^& more } there's an error, and I'd rather not have to deal with the %%
if I can easily replace them with an unused character, since I am replacing all other poison characters
temporarily.

Unless there is some rule or reason not to, I would like to share the batch file with the forum
for review, correction, hints of better ways to do a particular operation, suggestions on improving
program flow, etc.
Jerry


Post your code as many times as you like. No restrictions there.

There are some interesting and useful options you have outlined - good luck with fighting poison characters though :) In my tools I just accept some characters as not available - and use a HLL like .vbs etc if I need to process all characters. Perhaps a secondary .vbs script would suit your code?

Jer
Posts: 177
Joined: 23 Nov 2014 17:13
Location: California USA

Re: Change literal % to something else...then change it back to %

#6 Post by Jer » 24 Feb 2016 12:56

I think this batch file answers my question, discovered through trial and error.
It changes the literal % in a string into an extended ascii character, then the string
can be tested without interference from the percent symbol, and finally
the % is restored for display as a literal character.

Code: Select all

@Echo Off
setlocal EnableDelayedExpansion
set "str=all store items 50%% off"
echo before conversion: %str%
Set "str=!str:%%=û!"& rem replace % with character from ascii code 150
echo after conversion: %str%
set "str=!str:û=%%!"
echo after restore: %str%
endlocal & exit /b


Can I get a two-fer out of this thread?
If I use enabledelayedexpansion and exit without an endlocal,
am I still in the original state in the DOS window, or have I changed
the DOS environment by not doing the endlocal?

I observed that when testing and an error causes the code to exit
before completing, after correcting the code, I often need to close the
DOS window and start again, because the corrected code does not run
correctly until I run it in a new DOS window.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Change literal % to something else...then change it back to %

#7 Post by foxidrive » 25 Feb 2016 17:16

Jer wrote:If I use enabledelayedexpansion and exit without an endlocal,
am I still in the original state in the DOS window, or have I changed
the DOS environment by not doing the endlocal?

There is an implied endlocal when the batch finishes

You can evaluate it with code like this - run it and then type the last line at the cmd prompt.

Code: Select all

@Echo Off
setlocal EnableDelayedExpansion
set test=123
echo %test%


I observed that when testing and an error causes the code to exit
before completing, after correcting the code, I often need to close the
DOS window and start again, because the corrected code does not run
correctly until I run it in a new DOS window.

Are you talking about the same code? The variable is reset in line 3 so it should work fine at all times.

Jer
Posts: 177
Joined: 23 Nov 2014 17:13
Location: California USA

Re: Change literal % to something else...then change it back to %

#8 Post by Jer » 26 Feb 2016 02:34

The error condition where I had to start a new DOS session
occurred a few times with my 'big project', and I did not document
what the lines were doing that caused the error. When I try to make
code that breaks before completing, I can't duplicate the issue. :oops:

Thanks for that answer about the implied endlocal when a batch file ends.

mirrormirror
Posts: 129
Joined: 08 Feb 2016 20:25

Re: Change literal % to something else...then change it back to %

#9 Post by mirrormirror » 28 Feb 2016 21:35

There is an implied endlocal when the batch finishes

Do you know if ALL "setlocals" are ended upon batch file termination? - i.e. if I run
SETLOCAL
SETLOCAL
SETLOCAL

-end of batch

Will all 3 be terminated?

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Change literal % to something else...then change it back to %

#10 Post by foxidrive » 29 Feb 2016 03:11

mirrormirror wrote:Do you know if ALL "setlocals" are ended upon batch file termination? - i.e. if I run
SETLOCAL
SETLOCAL
SETLOCAL

-end of batch

Will all 3 be terminated?


Open a cmd prompt, run this script and then type the echo %testA% %testB% %testC% line at the cmd prompt.

Code: Select all

@Echo Off
setlocal
set testA=123

setlocal
set testB=456

setlocal
set testC=789

echo %testA% %testB% %testC%
pause

Post Reply