String Manipulation: Escaping Special Characters

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Phylum
Posts: 4
Joined: 07 Nov 2011 06:29

String Manipulation: Escaping Special Characters

#1 Post by Phylum » 09 Nov 2011 06:13

I have loads of fun with batch scripting. I tend to rely on batches for quick [& dirty] operations vs VBScript or PowerShell, mainly due to a lack of the same level of in-depth familiarity with those languages.

I'm in need of assistance figuring this out a way to do some basic user input normalization. The batch prompts for a path and expects something like
    software\adobe
    image\WinXP
    etc

However I'm trying to build some basic 'intelligence' to remove strings that start with a backslash and double quotes. Otherwise paths like this will fail:
    \software\adobe
    "image\Windows 7"
    "\software\Nero Burning"
    etc

Detecting & clearing the backslash was no problem..

Code: Select all

if "!arg:~0,1!" EQU "\" (
      echo. & echo.     WARNING: Path starts with a slash and it should not
      echo.         User Input: !arg!
      set arg=!arg:~1!
)


But I can't figure out how to detect whether or not the string starts with a double quote.

Code: Select all

if "!arg:~0,1!" EQU """ (
   echo bad double quotes
)
if "!arg:~0,1!" EQU "\"" (
   echo bad double quotes
)
if "^!arg:~0,1!" EQU "\"" (
   echo bad double quotes
)
if "^!arg:~0,1!" EQU "^"" (
   echo bad double quotes
)
And several other variants with/without brackets and carrots etc.


Presumably once I figure that out I can use this technique (thanks to DosTips for that) to remove the first & last characters which should be quotes.

Code: Select all

set arg=%arg:~1,-1%


Its not urgent, its not serious. I'm the only one that uses this script, that I know of, but I know at some point once word gets out others will want to use this as well.

Many thanks

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: String Manipulation: Escaping Special Characters

#2 Post by Ed Dyreen » 09 Nov 2011 08:33

'

Code: Select all

@echo off &setlocal enabledelayedexpansion
set $var="
set $var
if /i ["!$var!"] == [""^"] echo.DQuote detected

set $var=
set $var
if /i ["!$var!"] == [""^"] echo.DQuote detected

pause
exit
:twisted:

Code: Select all

set $var="This Works"
%forX_% ( '$var' ) %_Dequote%
viewtopic.php?f=3&t=1893

It looks like you are looking for a path verifier, this can be done easily in DOS using:

Code: Select all

set "$VAR=%%~dpr"
I would use my own eDOS library fcourse:

Code: Select all

%forX_% ( '$var, "C:\This Works\", 1' ) %_Path.declare%
%forX_% ( '$var, "C:\This Works", 1' ) %_Path.declare%
%forX_% ( '$var, "C:\This Works\This\Works\This Works.TMP", 1' ) %_File.declare%
output, input, SetsError

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

Re: String Manipulation: Escaping Special Characters

#3 Post by jeb » 10 Nov 2011 07:22

Hi Phylum,

Ed's answer is possible correct but it seems a bit too complex.

Your question of how to compare a character against a quote.
It could be solved :)

Code: Select all

set input="hello"
echo ^%input:~0,1%
if [^%input:~0,1%]==[^"] (
  echo it is a quote
) else (
  echo it is any character
)


But this doesn't look very good.

With delayed expansion it's a bit nicer

Code: Select all

setlocal EnableDelayedExpansion
set input="hello"
set "quote=""
if "!input:~0,1!"=="!quote!" (
  echo it is a quote
) else (
  echo it is any character
)


jeb

Phylum
Posts: 4
Joined: 07 Nov 2011 06:29

Re: String Manipulation: Escaping Special Characters

#4 Post by Phylum » 11 Nov 2011 05:50

Thank you both for taking the time read this silly post.

Both of your suggestions work fine - its excellent stuff! I tried [] but I don't think I did it with ^.

Ed: Really clever & brilliant stuff you've linked to!

Post Reply