-2

I asked this question yesterday, but it was marked as duplicate and I was given a link to a question that discusses something similar, but NOT variable substitution and substrings specifically, so I'm posting it again.

I am trying to do variable substitution with delayed expansion variables

Normally, I'd have something like this:

REM Sub 123 for bcd
SET Myvar=abcdefg
SET MyNewVar=%MyVar:bcd=123%
echo %MyNewVar%

REM Just get all but the last 2 chars of the string
SET MyShortVar=%Myvar:~0,-2%
echo %MyShortVar%

Based on this article (Variables Are Not Behaving As Expected), I tried this:

SET MyShortVar=%%Myvar:~0,-1%%
ECHO MyShortVar=[%MyShortVar%] or [!MyShortVar!]

and got this output:

MyShortVar=[%Myvar:~0,-1%] or [%Myvar:~0,-1%]

But how do I do this when using variables where I have to use the bang symbol because of Delayed Expansion?

aschipfl
  • 33,626
  • 12
  • 54
  • 99
cashonly
  • 119
  • 9
  • As we said in your previous question the simplest solution is to enable delayed expansion with the `SETLOCAL` command and then variables are referenced using exclamation points instead of percent symbols. The only time you double the percent symbol is if you are trying to escape percent symbols or you are trying to do delayed expansion with the `CALL` command. – Squashman Jul 22 '21 at 14:03
  • Regardless you do not have a [mcve] of actually **NEEDING** delayed expansion in your code. – Squashman Jul 22 '21 at 14:04
  • 2
    If you do not post an actual piece of code we can all copy and sufficient additional information for use to reproduce the issue you are having, then we will be unable to provide anything other than another duplicate link to the same question, again. – Compo Jul 22 '21 at 14:26
  • 1
    Do not repost your question again, but edit your original one to make your actual issue clear, then it will become reopened. By the way, have you ever tried `setlocal EnableDelayedExpansion`, then `set MyVar=abcdefg`, and then `echo !MyVar:bcd=123!` or `echo !MyVar:~0,-2!`? – aschipfl Jul 22 '21 at 14:27
  • 2
    The article you linked, describes exactly how to use `%%var%%` (namely with the `call` command) and even explains why. Please read again and *follow* the instructions. But as already commented by others, there is no need for delayed expansion in your code. – Stephan Jul 22 '21 at 14:37

1 Answers1

-2

Found my answer here: Why can I not get a substring of a delayed expansion variable in an if statement?

BY using the ! instead of % for string substitution AND enclosing that in double quotes, it worked. As in this example from an answer to that question:

setLocal enableDelayedExpansion
set test=testString
if "!test:~0,4!" == "test" echo Success
cashonly
  • 119
  • 9
  • 1
    Again, your example code does not require delayed expansion. Quotes are always a best practice for doing string literal comparisons. – Squashman Jul 22 '21 at 14:29
  • 2
    From what you've posted above, you appear only to be performing an `if` command with variable substring expansion. The real and obvious question is, why did you enable delayed expansion, when it is not required. – Compo Jul 22 '21 at 14:30