Reputation: 51
Here are my code segment:
set CMS_SQL="cms91_to_913.json cms913_to_9132.json cms9132_to_9133.json cms9133_to_10100.json"
set CMS_FROM_VERSION="cms913_"
set CMS_TO_VERSION="to_9133.json"
Both CMS_FROM_VERSION and CMS_TO_VERSION are variables
I want to extract substring "cms913_to_9132.json cms9132_to_9133.json"
from %CMS_SQL%
Just want to get the substring between %CMS_FROM_VERSION% and %CMS_TO_VERSION%
containing these two variable values
Do not want to use file to do this and also do not want to hard code number to get the substring, since the two strings are variables.
How extract substring between these two strings? Thanks
Upvotes: 0
Views: 8610
Reputation: 38589
To re-iterate my comment, the following does not know or count the substring length, it simply removes the first two and last two characters from the string:
Set "String=AB134SSPQS"
Set "subString=%String:~2,-2%"
Echo %subString%
If AB
and QS
precede and succeed the required substring but do not necessarily begin and start the string, then perhaps this will work for you:
Rem Looking for a file.txt substring preceded by AB and succeeded by QS
@Echo Off
For /F "Delims=" %%A In ('FindStr /I "AB.*QS" "file.txt"') Do Call :Sub "%%A"
Pause
GoTo :EOF
:Sub
Set "Line=%~1"
Set "Up2Sub=%Line:*AB=%"
Set "SubStr=%Up2Sub:QS="&:"%"
Echo %SubStr%
Exit /B
I've had to make up a scenario, (see Rem
), because you've not provided sufficient information on which to base an answer.
Edit
Set "String=AB134SSPQS"
Set "Up2Sub=%String:*AB=%"
Set "SubStr=%Up2Sub:QS="&:"%"
Echo %SubStr%
Edit2
As given in your other question, and added here based on your now changed question, here's one method:
@Echo Off
Set "CMS_SQL=cms91_to_913.json cms913_to_9132.json cms9132_to_9133.json cms9133_to_10100.json"
Set "CMS_FROM_VERSION=cms913_"
Set "CMS_TO_VERSION=to_9133.json"
For /F "Delims=" %%A In ('Echo %%CMS_SQL:*%CMS_FROM_VERSION%^=%%'
) Do Set "SUBSTRING=%CMS_FROM_VERSION%%%A"
For /F "Delims=" %%A In ('Echo %%SUBSTRING:%CMS_TO_VERSION%^=^&:%%'
) Do Set "SUBSTRING=%%A%CMS_TO_VERSION%"
Set SUBSTRING
Pause
Upvotes: 2
Reputation:
With a bit of variable shuffling:
:: SO50021845.cmd
Echo off
set "CMS_SQL=cms91_to_913.json cms913_to_9132.json cms9132_to_9133.json cms9133_to_10100.json"
set "FROM=cms913_"
set "TO=to_9133.json"
:: Check if CMS_SQL contains FROM and TO
Echo:%CMS_SQL%|Findstr /i "%FROM%.*%TO:.=\.%" >NUL 2>&1 || (
Echo string doesn't contain FROM and TO & Pause & Exit /b
)
:: Remove part before FROM
Call set "Result=%%CMS_SQL:*%FROM%=%FROM%%%"
:: extract part to remove behind TO
Call set "Remove=%%Result:*%TO%=%%"
:: remove part behind TO
Call set "Result=%%Result:%Remove%=%%"
Echo %Result%
Sample output:
cms913_to_9132.json cms9132_to_9133.json
Upvotes: 1
Reputation:
do it with replacement:
@echo off
set "string=AB134SSPQS"
set "substr=%string:AB=%"
set "substr=%substr:QS=%"
echo %substr%
Upvotes: 1
Reputation: 79983
In windows cmd
, this is simple:
set "result=%A:~2,6%"
echo %result%
but perhaps you mean real DOS?
(assuming you want the 6
characters, starting at character
2when character
0is the first in a string-variable named
a` - variable-case is irrelevant)
See set /?
from the prompt for documentation (or any of thousands of entries on SO)
Upvotes: 0