0

I am sure this issue is something silly. But I am not sure what I am doing wrong. I want the variables, start and end to be used to set the mid string method I am using to set subStr. I have already read set /? with no success. Any help would be greatly appreciated.

echo off
rem // First I will set all my varialbles...
set pleaStr=this is just a test. Please help me figure this out. I appreciate your help
set start=21
set end=-23
set subStr=%pleaStr:~start,end%
rem // The mid string method works normally. See!!
set subStrEasy=%pleaStr:~21,-23%
rem // Return extracted URL:
echo %pleaStr%
echo %subStr%
echo %subStrEasy%

The code above, does not work if I substitute the Environment Variables(ENV) start and end for their digit counterparts, 21 and -23. when I echo %subStr% I get pleaStr:~start,end where as echo %subStrEasy% gives me Please help me figure this out.

Perhaps environment variable cannot be used in this manner?

  • You may read a detailed explanation of this problem, and the way to solve it, at [this answer](https://stackoverflow.com/questions/10166386/arrays-linked-lists-and-other-data-structures-in-cmd-exe-batch-script/10167990#10167990) – Aacini May 15 '21 at 01:06

1 Answers1

2

And here is the solution using delayed expansion.

@echo off
setlocal enabledelayedexpansion
set "pleaStr=this is just a test. Please help me figure this out. I appreciate your help"
set "start=21"
set "end=-23"
set "subStr=!pleaStr:~%start%,%end%!"
echo %subStr%
Squashman
  • 11,987
  • 5
  • 23
  • 33