0

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

Dom
  • 51
  • 1
  • 7
  • 1
    You must at least precise how you expect to get these substring. Pattern matching, position on the original string ? – sandwood Apr 25 '18 at 09:20
  • What is DOS Bat? – Gerhard Apr 25 '18 at 09:32
  • will it always be `AB` and `QS` as beginning and end? – Gerhard Apr 25 '18 at 10:17
  • 1
    The following seems to do as you've asked, `Set "String=AB134SSPQS"`   `Set "subString=%String:~2,-2%"` `Echo %subString%`. Note: I haven't counted the string length, just removed the first two and last two characters, the substring could be any length, _(within reason)_. – Compo Apr 25 '18 at 11:02
  • 1
    Sorry, based on your change in question and your comment in my answer, but it seems you are unclear on what you want and quite frankly do not know what you want. You just changed your question completely and reposted the same question. Voting to close. – Gerhard Apr 25 '18 at 12:27

4 Answers4

2

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
Compo
  • 36,585
  • 5
  • 27
  • 39
  • Thanks,how to do it without a file.txt ? – Dom Apr 25 '18 at 12:26
  • @Dom, I have added an Edit section to my answer. – Compo Apr 25 '18 at 12:33
  • Thanks again, I try to use a variable set "VAR_BEGIN=*AB" then Set "Up2Sub=%String:%VAR_BEGIN%=%" why this variable can not take affect? – Dom Apr 25 '18 at 12:58
  • @Dom, that's a slightly different scenario, and your latest question, so I'll leave it as such and allow for relevant answers to that question. – Compo Apr 25 '18 at 13:12
  • @Dom, I've edited my answer, **Edit2**, to provide an answer based on your now changed question parameters. _(it is the same as I've posted in your other question)_. – Compo Apr 25 '18 at 13:48
  • Thank you very much, it is fine – Dom Apr 25 '18 at 14:00
1

do it with replacement:

@echo off
set "string=AB134SSPQS"
set "substr=%string:AB=%"
set "substr=%substr:QS=%"
echo %substr%
Gerhard
  • 22,678
  • 7
  • 27
  • 43
1

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
0

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 character2when character0is the first in a string-variable nameda` - variable-case is irrelevant)

See set /? from the prompt for documentation (or any of thousands of entries on SO)

Magoo
  • 77,302
  • 8
  • 62
  • 84