Page 1 of 1
Assign a string with poison characters with some limitations
Posted: 14 Apr 2013 01:57
by carlsomo
I have used what I have learned from this site to create a batch file to assign a string containg poison characters to a variable with the following batch file. It has its limitations that are described in the help section. If anyone can improve on this I would be grateful. Here is my attempt at Assign.bat:
Code: Select all
@echo off&goto :start
:Assign.bat "EnVar=String with quotes & Poison chars to assign to variable EnVar"
echo(
echo(USAGE: %~nx0 "Envar=String which may contain 'Poison' characters"
echo(
echo(Format: Must place quotes at the very beginning and end of argument string
echo( as in the USAGE example to assign difficult strings to a variable.
echo(
echo(To display the variable use delayed expansion, ie. Echo ^^!Envar^^!
echo( or use Set/p with quotes, ie. ^<nul Set/p="%%EnVar%%"
echo(
echo(If double quotes are used inside the string they must be 'balanced' with a
echo(second double quote inside the string and unwanted results may occur
echo(if poison characters are quoted inside the string.
goto :eof
:start
setlocal enableDelayedExpansion
if !"%~1" equ ""! goto :Assign.bat
if !"%~1" equ "/?"! goto :Assign.bat
>"%temp%\getArg.txt" <"%temp%\getArg.txt" (
setlocal disableExtensions
set prompt=#
echo on
for %%a in (%%a) do rem . %*.
echo off
endlocal
set /p "args="
set /p "args="
set "args=!args:~7,-2!"
)
endlocal& set %args%&exit /b
Re: Assign a string with poison characters with some limitat
Posted: 14 Apr 2013 02:16
by foxidrive
you don't explain getargs.txt or otherwise how to use it.
People looking at it in the future will wonder.
Re: Assign a string with poison characters with some limitat
Posted: 14 Apr 2013 02:29
by carlsomo
The Rem statement writes to getargs.txt and then the batch reads from the file created to assign the variable 'args'. This has been posted previously as a method to deal with poison strings. I stand on the shoulders of giants who could explain it better, I am sure. I suggest you copy and paste the batch file and test it with difficult strings to see what happens. Then perhaps you could improve it? That is my purpose in tossing out my attempt to deal with this difficult subject.
Carl
Re: Assign a string with poison characters with some limitat
Posted: 14 Apr 2013 02:41
by foxidrive
It didn't do anything. How about you explain how it is supposed to function.
Code: Select all
USAGE: A.BAT "Envar=String which may contain 'Poison' characters"
Format: Must place quotes at the very beginning and end of argument string
as in the USAGE example to assign difficult strings to a variable.
To display the variable use delayed expansion, ie. Echo !Envar!
or use Set/p with quotes, ie. <nul Set/p="%EnVar%"
If double quotes are used inside the string they must be 'balanced' with a
second double quote inside the string and unwanted results may occur
if poison characters are quoted inside the string.
d:\abc>a "envar=>"
d:\abc>
Re: Assign a string with poison characters with some limitat
Posted: 14 Apr 2013 02:48
by carlsomo
Here is the original thread I used for inspiration:
viewtopic.php?f=3&t=2836After you run the script from the command line type this:
<nul set/p="%envar%"
to see what 'envar' contains.
I get: '>' as a result when I do your example on my win7 machine.
Re: Assign a string with poison characters with some limitat
Posted: 14 Apr 2013 02:53
by foxidrive
I guess I don't understand why you are using code to do something like that.
This is all that's needed. What is the purpose of the batch file? I just don't see the point.
set "envar=>"
Re: Assign a string with poison characters with some limitat
Posted: 14 Apr 2013 03:06
by carlsomo
I am struggling with trying to assign difficult strings such as:
set "envar=This^"&that|th!is"|tha"t%"
without an error message and getting the string assigned and displaying it
Re: Assign a string with poison characters with some limitat
Posted: 14 Apr 2013 03:21
by foxidrive
carlsomo wrote:I am struggling with trying to assign difficult strings such as:
set "envar=This^"&that|th!is"|tha"t%"
without an error message and getting the string assigned and displaying it
That's not a real life string, but IMO you are using the wrong language if you have to deal with *any* string.
Re: Assign a string with poison characters with some limitat
Posted: 14 Apr 2013 10:27
by Liviu
Page 2 of that thread has a fully worked out example
http://www.dostips.com/forum/viewtopic.php?p=13056#p13056. That code does not have the limitations about quoting and escaping that you mention here, in fact the only limitations are:
- can't handle characters outside the current codepage (because of the output redirection);
- won't work under "cmd /u" (because the temp file would ge generated as UTF-16LE in that case, which cmd cannot read back).
FWIW both those limitations apply to your code as well.
The difference in (and issue with) your code seems to be that it's attempting to use early expansion on the "endlocal& set %args%&exit /b" line. That's known to fail when the variable contains certain poison characters. If you really want to "tunnel" the variable past endlocal you'll need to use one of the techniques for returning arbitrary values from a local block, discussed elsewhere on dostips.
Liviu
Re: Assign a string with poison characters with some limitat
Posted: 14 Apr 2013 14:06
by Aacini
carlsomo wrote:I am struggling with trying to assign difficult strings such as:
set "envar=This^"&that|th!is"|tha"t%"
without an error message and getting the string assigned and displaying it
You may assign
any character string to a variable directly in a SET command; the trick consist in insert the "poison" characters via Delayed Expansion:
Code: Select all
@echo off
setlocal DisableDelayedExpansion
set quote="
set caret=^^
set exclam=!
setlocal EnableDelayedExpansion
echo Quote: !quote!, Caret: !caret!, Exclam: !exclam!
set "envar=This!caret!!quote!&that|th!exclam!is!quote!|tha!quote!t%%"
echo !envar!
Antonio
Re: Assign a string with poison characters with some limitat
Posted: 14 Apr 2013 23:13
by jeb
As Aacini shows even the strongest string can be created, but in your case it's only necessary to escape every special character.
Code: Select all
set ^"envar=This^"^&that^|th^!is^"^|tha^"^t%%^"
jeb