Help Process and assign special characters from cmd line

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

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

Help Process and assign special characters from cmd line

#1 Post by carlsomo » 10 Feb 2013 16:03

I have a batch that I wish to assign the entire command line to a string
ie. set "string=%*"
However input from the command line may have speical characters such as:
^ & < > |, etc
for instance: test.bat (This is a ~!@#)$^^^^%*^&_-^|=+ string)
to get this result in a variable: string=(This is a ~!@#)$^%*&_-|=+ string)
I tried this using extended ascii 247 '÷' as a delim that is not likely to appear in the cmd line:

Code: Select all

setlocal disabledelayedexpansion
rem assign %* to string
for /f "tokens=2* delims=÷" %%a in ('"for %%b in (1) do rem ÷%*÷"') do set "string=%%a"
setlocal enabledelayedexpansion
<nul set/p="string=!string!"

result:
string=(This is a ~!@#)$^%*&_-|=+ string)
string gets assinged the whole of '%*' as long as ^ escapes are used but I cannot supress
this message:
'rem)' is not recognized as an internal or external command,
operable program or batch file.

There must be a simpler solution?

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Help Process and assign special characters from cmd line

#2 Post by dbenham » 10 Feb 2013 17:35

Derived from info in this thread: foolproof counting of arguments

Code: Select all

@echo off
setlocal enableDelayedExpansion
>"%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!"
)
del "%temp%\getArg.txt"
echo args=!args!
It's not a simple solution, but it is very reliable. There is no need to worry about escaping any characters. It loads whatever was provided on the command line.


Dave Benham

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

Re: Help Process and assign special characters from cmd line

#3 Post by carlsomo » 10 Feb 2013 18:29

Thanks Dave,
It is certainly an advantage to not have to worry about placing escape chars. I just wish there was a way to get the all command line args with poison chars but without using a temp file.

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

Re: Help Process and assign special characters from cmd line

#4 Post by Liviu » 11 Feb 2013 00:45

dbenham wrote:Derived from info in this thread

I remember that thread. Don't know that the following would make much sense to me on a first reading otherwise ;-)
- "setlocal disableExtensions"
- "for %%a in (%%a)"
- the space after the "." in "do rem . %*."
- the magical numbers in "args=!args:~7,-2!"

carlsomo wrote:I just wish there was a way to get the all command line args with poison chars but without using a temp file.

I wish, too, there were a way, especially since redirections (including the temp file) lose characters outside the current codepage. Unfortunately, I am not aware of any solution in the general case that doesn't require a temp file. I notice that your example quotes most of the 7-bit "poison" characters, but not the " quote itself. If you know (or have control over) the quoting syntax for the params, specifically whether they are always/never present/balanced etc, then there might be shortcuts possible.

Liviu

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

Re: Help Process and assign special characters from cmd line

#5 Post by jeb » 11 Feb 2013 02:34

Liviu wrote:I remember that thread. Don't know that the following would make much sense to me on a first reading otherwise ;-)
- "setlocal disableExtensions"
- "for %%a in (%%a)"
- the space after the "." in "do rem . %*."
- the magical numbers in "args=!args:~7,-2!"


My original solution has one drawback with %%a.
If the parameter itself contains %%a this is expanded, but also %%~fa or %%~da, someone (suppose it was Aacini) has the clever idea to disable the extensions,
so only %%a is expanded, the %%~ variants are unchanged.
Therefore the %%a is expanded to %%a by "for %%a in (%%a)".

The space after "." is necessary as REM has a special parser, it parses the frist token after REM to recognize if there is /?.
So the space move the %* after the first token, the trailing character is necessary, to avoid multiline problems with an ending caret character in the parameter.

The magical numbers in "args=!args:~7,-2!" is simply the result of the prompt output

Code: Select all

#REM[space].[space]<%*paramter>.[space]


carlsomo wrote:It is certainly an advantage to not have to worry about placing escape chars. I just wish there was a way to get the all command line args with poison chars but without using a temp file.

Currently I can't see any possible way for this, because:
The only ways to read %* or any other thing you need a SET or a FOR-loop.
SET can't work, as you can build paramters, which always fails, like "&"&

If you place it in the FOR command part, you can break it with the same poisen string, and you can't avoid this.

Therefore you need a variant where the expanded %* will be ignored completely by the parser, so the REM %* seems to be appropiated.

But there are still two problems left.
It's not possible to catch any <CR> characters embedded in the parameter, as just after the percent expansion all CR's are removed.
With embedded linefeeds in the parameter you can break the complete code(code injection).

Like

Code: Select all

test.bat this is my parameter ^

echo hello ^> CON ^

rem -



jeb

Post Reply