Capture trailing characters of a string based on a delimiter

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
sherlock99
Posts: 8
Joined: 08 Oct 2010 08:07

Capture trailing characters of a string based on a delimiter

#1 Post by sherlock99 » 22 Nov 2011 05:44

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.

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

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

#2 Post by dbenham » 22 Nov 2011 07:00

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

sherlock99
Posts: 8
Joined: 08 Oct 2010 08:07

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

#3 Post by sherlock99 » 22 Nov 2011 07:41

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.

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

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

#4 Post by dbenham » 22 Nov 2011 07:45

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

sherlock99
Posts: 8
Joined: 08 Oct 2010 08:07

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

#5 Post by sherlock99 » 23 Nov 2011 02:17

That's great Dave. Thanks again for your interest in and resolution to my problem.

paultomasi
Posts: 25
Joined: 24 Feb 2009 14:52
Location: UK
Contact:

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

#6 Post by paultomasi » 26 Nov 2011 01:20

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

Post Reply