Assign a string with poison characters with some limitations

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
carlsomo
Posts: 91
Joined: 02 Oct 2012 17:21

Assign a string with poison characters with some limitations

#1 Post by carlsomo » 14 Apr 2013 01:57

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

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

Re: Assign a string with poison characters with some limitat

#2 Post by foxidrive » 14 Apr 2013 02:16

you don't explain getargs.txt or otherwise how to use it.
People looking at it in the future will wonder.

carlsomo
Posts: 91
Joined: 02 Oct 2012 17:21

Re: Assign a string with poison characters with some limitat

#3 Post by carlsomo » 14 Apr 2013 02:29

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

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

Re: Assign a string with poison characters with some limitat

#4 Post by foxidrive » 14 Apr 2013 02:41

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>

carlsomo
Posts: 91
Joined: 02 Oct 2012 17:21

Re: Assign a string with poison characters with some limitat

#5 Post by carlsomo » 14 Apr 2013 02:48

Here is the original thread I used for inspiration:

viewtopic.php?f=3&t=2836

After 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.

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

Re: Assign a string with poison characters with some limitat

#6 Post by foxidrive » 14 Apr 2013 02:53

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=>"

carlsomo
Posts: 91
Joined: 02 Oct 2012 17:21

Re: Assign a string with poison characters with some limitat

#7 Post by carlsomo » 14 Apr 2013 03:06

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

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

Re: Assign a string with poison characters with some limitat

#8 Post by foxidrive » 14 Apr 2013 03:21

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.

Liviu
Expert
Posts: 470
Joined: 13 Jan 2012 21:24

Re: Assign a string with poison characters with some limitat

#9 Post by Liviu » 14 Apr 2013 10:27

carlsomo wrote:Here is the original thread I used for inspiration: viewtopic.php?f=3&t=2836

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

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

Re: Assign a string with poison characters with some limitat

#10 Post by Aacini » 14 Apr 2013 14:06

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

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

Re: Assign a string with poison characters with some limitat

#11 Post by jeb » 14 Apr 2013 23:13

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

Post Reply