You're on the right track.
The first case doesn't work because, command prompt reads from % to % to determine variables:
IF /I
%VAR:~%num
%,1%=="A" [do commands]
With delayed expansion, it reads as:
IF /I
!VAR:~%num%,1!=="A" [do commands]
This should work, except the problem is there is a syntax error. You cannot expose a
comma to command prompt in the comparison. So either enclose both sides of the comparison in quotation marks (disables special character handling) or escape the problematic character using a caret ^.
IF /I "!VAR:~%num%,1!"=="A" [do commands]
or
IF /I !VAR:~%num%^,1!==A [do commands]
Remember that the quotation marks are
included in the comparison, so they need to be present on both sides appropriately.
By the way, you mis-typed
SETLOCAL as SETL
COAL.
Lastly, it's only needed for commas or special characters like &|<>, so quotation marks aren't needed in this example:
Code: Select all
setlocal enabledelayedexpansion
set var=ABCDE
if !var:~1!==BCDE echo The characters following the first one in var are BCDE.
if !var:~-1!==E echo Last character of var is E.