I am trying to check if a string is quoted by checking the first and last characters of the string. But my script fails when checking for the quote see output: AND was unexpected at this time. below.
Code
@echo off
set mystring=Non quoted string
set myquotedstring=^"My quoted string^"
echo mystring: %mystring%
echo myquotedstring: %myquotedstring%
set result=%mystring:~0,1%
echo first character of non quoted string is: %result%
set result=%mystring:~-1%
echo last character of non quoted string is: %result%
if %mystring:~0,1%u==^" AND %mystring:~-1%==^" (
echo this string is NOT quoted
set newstring=^"Non quoted string^"
echo newstring: %newstring%
)
set result=%myquotedstring:~0,1%
echo first character of quoted string is: %result%
set result=%myquotedstring:~-1%
echo last character of quoted string is: %result%
if %myquotedstring:~0,1%u==^" AND %myquotedstring:~-1%==^" (
echo this string is quoted
)
This is the output I am getting
mystring: Non quoted string
myquotedstring: "My quoted string"
first character of non quoted string is: N
last character of non quoted string is: g
this string is NOT quoted
newstring: "Non quoted string"
first character of quoted string is: "
last character of quoted string is: "
AND was unexpected at this time.
UPDATE
I realise now I cannot use AND. But even if I remove I have a problem.
eg
if %mystring:~0,1%u==^" if %myquotedstring:~-1%==^" (
echo this string is NOT quoted
set newstring=^"Non quoted string^"
echo newstring: %newstring%
)
I get
The syntax of the command is incorrect.
@echo off
setlocal EnableDelayedExpansion
set mystring=Non quoted string
echo %mystring%
if !mystring:~0^,1!!mystring:~-1! equ "" (
echo -^> String is quoted
) else (
echo -^> String not quoted
set newstring="%mystring%"
echo New string: !newstring!
)
echo/
set mystring="My quoted string"
echo %mystring%
if !mystring:~0^,1!!mystring:~-1! equ "" (
echo -^> String is quoted
) else (
echo -^> String not quoted
set newstring="%mystring%"
echo New string: !newstring!
)