Page 1 of 1

Capture trailing characters of a string based on a delimiter

Posted: 22 Nov 2011 05:44
by sherlock99
Hello all,

does anyone know if a FOR or SET command can be used to capture the right hand characters based on a final delimiter?

Here are some basic examples which includes commas, spaces, MULTIPLE backslashes or none of the aforementioned:

set script=windows
set script=c:\dos
set script=fail,back,Apex,Production department,QWERTY\KEYBOARD\VMPAPP06
set script=fail back,Apex Production department,SUPER\QWERTY\KEYBOARDe\VM

Using the backslash as a delimiter, in the above examples the command would capture:
windows
dos
VMPAPP06
VM

You will note that I have included an example with NO backslashes in it as it must also work under this circumstance. You will also notice that both the number of backslashes and the length of the string to capture varies.

Can anyone help please.

Many thanks,

Sherlock99.

Re: Capture trailing characters of a string based on a delim

Posted: 22 Nov 2011 07:00
by dbenham
Here is an interesting solution using search and replace command injection (a sort of dynamic macro):

Code: Select all

@echo off
setlocal

set script=windows
call :lastToken
set script=c:\dos
call :lastToken
set script=fail,back,Apex,Production department,QWERTY\KEYBOARD\VMPAPP06
call :lastToken
set script=fail back,Apex Production department,SUPER\QWERTY\KEYBOARDe\VM
call :lastToken
set script=fail back,Apex Production department,SUPER\QWERTY\KEYBOARDe\VM\
call :lastToken
exit /b

:lastToken
set "lastToken=%script:"=%"
set "lastToken=%lastToken:\="&set "lastToken=%"
set script
set lastToken
echo(
exit /b

Note the last example I added that ends with the token delimiter. The result is an empty string, so lastToken is undefined. Not sure if this is a problem for you.

Some known limitations
- quotes are not preserved (this can probably be overcome easily with substitution before and after)
- script must not contain % (this can probably be overcome easily with substitution before and after)
- token delimiter cannot be =

There are probably additional limitations

If you are having trouble seeing how it works, try running the example after disabling the @echo off.


Dave Benham

Re: Capture trailing characters of a string based on a delim

Posted: 22 Nov 2011 07:41
by sherlock99
Excellent dbenham,

that's just what I needed. There will always be a value so no risk of it ending with a delimiter and it will never be an = sign.

Thanks for the prompt reply.

Re: Capture trailing characters of a string based on a delim

Posted: 22 Nov 2011 07:45
by dbenham
Here is a version that preserves both the last token and the token before it. This can help when the script ends with the delimiter. It builds a macro and then executes it.

Code: Select all

@echo off
setlocal

set script=windows
call :lastToken
set script=c:\dos
call :lastToken
set script=fail,back,Apex,Production department,QWERTY\KEYBOARD\VMPAPP06
call :lastToken
set script=fail back,Apex Production department,SUPER\QWERTY\KEYBOARDe\VM
call :lastToken
set script=fail back,Apex Production department,SUPER\QWERTY\KEYBOARDe\VM\
call :lastToken
exit /b

:lastToken
set getLast=%script:"=%"
set "getLast=set "lastToken=%getLast:\="&set "prevToken=!lastToken!"&set "lastToken=%"
setlocal enableDelayedExpansion
set "prevToken="
%getLast%
set script
set lastToken
set prevToken
echo(
exit /b

Here are the results:

Code: Select all

script=windows
lastToken=windows
Environment variable prevToken not defined

script=c:\dos
lastToken=dos
prevToken=c:

script=fail,back,Apex,Production department,QWERTY\KEYBOARD\VMPAPP06
lastToken=VMPAPP06
prevToken=KEYBOARD

script=fail back,Apex Production department,SUPER\QWERTY\KEYBOARDe\VM
lastToken=VM
prevToken=KEYBOARDe

script=fail back,Apex Production department,SUPER\QWERTY\KEYBOARDe\VM\
Environment variable lastToken not defined
prevToken=VM


Same limitations as before, except now script cannot contain % or !. This can probably be overcome with pre and post substitution, but it is a bit more complicated then just worrying about %.


Dave Benham

Re: Capture trailing characters of a string based on a delim

Posted: 23 Nov 2011 02:17
by sherlock99
That's great Dave. Thanks again for your interest in and resolution to my problem.

Re: Capture trailing characters of a string based on a delim

Posted: 26 Nov 2011 01:20
by paultomasi
A trick is to use the 'filename' property of a FOR's '%'-variable as in the following example:

Code: Select all

set script=fail back,Apex Production department,SUPER\QWERTY\KEYBOARDe\VM

for /f "delims=" %%a in ("%script%") do echo %%~na
This will display:

    VM


So, getting the required values from your example strings, I would do the following:

Code: Select all

@echo off
setlocal enabledelayedexpansion

set script[1]=windows
set script[2]=c:\dos
set script[3]=fail,back,Apex,Production department,QWERTY\KEYBOARD\VMPAPP06
set script[4]=fail back,Apex Production department,SUPER\QWERTY\KEYBOARDe\VM

for /l %%i in (1,1,4) do (
   for /f "delims=" %%a in ("!script[%%i]!") do echo %%~na
)
This will display the following:

    windows
    dos
    VMPAPP06
    VM


Or simply:

Code: Select all

@echo off
set script=windows
for /f "delims=" %%a in ("%script%") do echo %%~na

set script=c:\dos
for /f "delims=" %%a in ("%script%") do echo %%~na

set script=fail,back,Apex,Production department,QWERTY\KEYBOARD\VMPAPP06
for /f "delims=" %%a in ("%script%") do echo %%~na

set script=fail back,Apex Production department,SUPER\QWERTY\KEYBOARDe\VM
for /f "delims=" %%a in ("%script%") do echo %%~na
This will also display the following:

    windows
    dos
    VMPAPP06
    VM