Problem converting a single double quote in hybrid batch file

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

Problem converting a single double quote in hybrid batch file

#1 Post by Jer » 14 Mar 2016 00:47

I am working on an epic project developing a batch tool to complement Aacini's colorshow.exe,
and need a little help along the way. One of the options is to be able to display text entered
from the command line.

Special characters included in the text string required special handling and needed conversion
while the entire command line string was being tested for parameters and a file name or bracketed text string.

The original plan was to handle all special characters by converting them in batch to unused ascii characters,
then converting them back just before storing them in the array for display. The command line entry
includes coded parameters as well as text source, either a file or a text string. I found that I could
do this conversion in batch but could only get as far as a quoted string: "show me ^ and | and & and < and > and ! and %".
Then I could use vbscript in the batch file to remove the quotes and save that in a file. There will be instructional
notes that explain that these characters must be entered with a caret and the percent character doubled.

Now I figure that since I need vbscript, why not just skip most of the conversion and pass back from vbscript
the parsed parameters in one temporary file and source of the text, either a string or a file, in another temporary file,
as well as the error level to indicate what type the text source is.

The good new is everything is working except that pesky " symbol.
Passing a single double-quote to vbscript fails, so I convert it before passing the string. Hopefully you can see
from the code below that passing character for code 157 from batch to vbscript somehow will not convert unless I use code 209.
The code is entered into the batch file with KEDIT for Windows editor by pressing Alt-157. Strangely, when I hextype the line
in my editor, it shows the code to be 165, and an ascii chart shows 165 to be a capital N with a squiggle on top, the same
thing I see when it is echoed.

Thanks for your help.

Code: Select all

<!-- : Begin batch script

@Echo Off
setlocal
Set "str=show me a single double quote: ¥%" & rem ascii code 157

Echo calling script with string that contains character for ascii code 157...
CScript //nologo "%~f0?.wsf" "%str%"

endlocal & exit /b


----- Begin wsf script --->

<job><script language="VBScript">

Option Explicit
Dim text_string

text_string = WScript.Arguments(0)

If InStr(text_string, Chr(157)) > 0 Then text_string = Replace(text_string, Chr(157), """")
WScript.Echo "text_string after attempt to convert chr(157) to a quote: " & text_string

If InStr(text_string, Chr(209)) > 0 Then text_string = Replace(text_string, Chr(209), """")
WScript.Echo "text_string after attempt to convert chr(209) to a quote: " & text_string

WScript.Quit

</script></job>


calling script with string that contains character for ascii code 157...
text_string after attempt to convert chr(157) to a quote: show me a single double quote: Ñ
text_string after attempt to convert chr(209) to a quote: show me a single double quote: "

Aacini
Expert
Posts: 1914
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Problem converting a single double quote in hybrid batch file

#2 Post by Aacini » 14 Mar 2016 01:31

I am afraid I don't understand what exactly your method is supposed to do and what is your problem... When you read a line of text via SET /P command, the special characters in the input line does NOT need to be escaped. If you expand the variable that contain such special characters, the expansion will correctly works as long as you use !delayed! expansion. Anyway, ColorShow program may work over Batch variable names, so the expansion is not even required. For example:

Code: Select all

@echo off
setlocal EnableDelayedExpansion

set /P "var=Enter a string: "
ColorShow /1F "Using delayed expansion: !var!"  13 10
ColorShow /1F "Using variable name:     " var  13 10

Output example:

Code: Select all

C:\> test.bat
Enter a string: show me ^ and | and & and < and > and %
Using delayed expansion: show me ^ and | and & and < and > and %
Using variable name:     show me ^ and | and & and < and > and %

In previous example, the text shown by ColorShow appears in light white letters on blue background.

A ColorShow program feature that was missing from the documentation (that I will fix very soon) is that you may show a quote in a string literal if you insert the quote twice:

Code: Select all

C:\> ColorShow /1F "He said: ""Hello, World!"" and gone" 13 10
He said: "Hello, World!" and gone

However, this is not necessary if the string was read via SET /P command and the variable name is used (as usual):

Code: Select all

C:\> set /P "var=A string: "
A string: He said: "Hello, World!" and gone

C:\> ColorShow /1F var 13 10
He said: "Hello, World!" and gone


Antonio

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

Re: Problem converting a single double quote in hybrid batch file

#3 Post by foxidrive » 14 Mar 2016 01:35

A fairly robust workaround is to get the input and put it in a file as follows, then read the file with a VBS script.

Code: Select all

@echo off
set /p var=
setlocal enabledelayedexpansion
>file.txt echo !var!


You might consider using two set /p statements - one for the program parameters and the other for the input
or take program parameters on the command line and get the text via set /p

If you want to make your script programmable, then perhaps have the user supply the text in a file, rather than on the command line.

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

Re: Problem converting a single double quote in hybrid batch file

#4 Post by Jer » 14 Mar 2016 14:56

Parameters driving the color tool will come from the command line or another batch file.
Those parameters could include the name of the text file, or a bracketed one-liner text string.

To overcome the issue of capturing and passing double quotes from command line args
to vbscript, I will change each double quote to an improbable string, pass that to vbscript, then
change them back to double quotes in the script.
Here's a preview of the tool.
You type:
<the_name_of_your_batch_file_color_tool> /a3f /be4 178 /i2 /w+4 _info.txt

where text area colors are 3f, border colors e4, border character is ascii char. code 178
indent the display 2 columns, adjust the width to be 4 additional columns.

You get a batch file written for you, in this case the default name splash.bat:
edited the following batch file 3/15/2016 to handle & < > % and |

Code: Select all

@Echo Off
setlocal DisableDelayedExpansion & rem revised from enabledelayedexpansion
Set "line[1]= "
Set "line[2]=Color Utility Description"
Set "line[3]=!   >|< %% >|<   !"
Set "line[4]=Option #1 reads text from your prepared file and presents it on a"
Set "line[5]=colored background that has a width determined by the longest line"
Set "line[6]=of text. A border can be added using the /b option.  Options give"
Set "line[7]=you control of text justification, overall width, indentation,"
Set "line[8]=setting of left margin, colorization of border and text area,"
Set "line[9]=border character, filling text area with a character, adding blank lines"
Set "line[10]=below text, pausing after display,and naming a new batch file to display"
Set "line[11]=your output (displays your panel without this tool)."
Set "line[12]= "
Set "line[13]=Lines can be all Centered, or Left or Right justified."
Set "line[14]= "
Set "line[15]=Enter parameters in the following order as in this example:"
Set "line[16]= "
Set "line[17]=batch_file_name left /a1e /b08 177 /i4 /w80 myownfile.txt"
Set "line[18]= "
Set "line[19]=result: displays contents of myownfile.txt, text is left justified"
Set "line[20]=text area colors are light yellow on blue"
Set "line[21]=border colors are gray on black"
Set "line[22]=border character is ASCII character for code 177"
Set "line[23]=output is indented 4 columns"
Set "line[24]=override calculated text width; set width to 80"
Set "line[25]= "
Set "line[26]=Formatting parameters should be entered first followed by the name of your"
Set "line[27]=text file.  All lines of the text file will be displayed, up to 50 lines."
Set "line[28]= "
Set "line[29]=panel 1 of 5"
Set "line[30]= "
Set "numLines=30"
setlocal EnableDelayedExpansion & rem added
Set "parms=/07 32*2 /e4 178 178*80 178 13 10"
For /L %%i In (1,1,%numLines%) Do (
   Set "parms= !parms! /07 32*2 /e4 178*2 /3f line[%%i]:+78 /e4 178*2 13 10"
)
Set "parms= %parms% /07 32*2 /e4 178 178*80 178 13 10"
ColorShow %parms%
endlocal & endlocal



Here is the workaround method for double quotes entered on the command line.

Code: Select all

<!-- : Begin batch script

@Echo Off
rem to demonstrate passing double quote from command line args to vbscript, include
rem one or more double quotes in the argument(s)

setlocal
set "str=%*"

CScript //nologo "%~f0?.wsf" "%str:"=d_~_q%"

endlocal & exit /b


----- Begin wsf script --->

<job><script language="VBScript">

Option Explicit
Dim text_string

text_string = WScript.Arguments(0)

WScript.Echo "string before conversion: " & text_string
If InStr(text_string, "d_~_q") > 0 Then text_string = Replace(text_string, "d_~_q", """")
WScript.Echo "string after conversion: " & text_string

WScript.Quit

</script></job>


Post Reply