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.
Capture trailing characters of a string based on a delimiter
Moderator: DosItHelp
-
- Posts: 8
- Joined: 08 Oct 2010 08:07
Re: Capture trailing characters of a string based on a delim
Here is an interesting solution using search and replace command injection (a sort of dynamic macro):
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
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
-
- Posts: 8
- Joined: 08 Oct 2010 08:07
Re: Capture trailing characters of a string based on a delim
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.
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
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.
Here are the results:
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
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
-
- Posts: 8
- Joined: 08 Oct 2010 08:07
Re: Capture trailing characters of a string based on a delim
That's great Dave. Thanks again for your interest in and resolution to my problem.
-
- Posts: 25
- Joined: 24 Feb 2009 14:52
- Location: UK
- Contact:
Re: Capture trailing characters of a string based on a delim
A trick is to use the 'filename' property of a FOR's '%'-variable as in the following example:
This will display:
So, getting the required values from your example strings, I would do the following:
This will display the following:
Or simply:
This will also display the following:
Code: Select all
set script=fail back,Apex Production department,SUPER\QWERTY\KEYBOARDe\VM
for /f "delims=" %%a in ("%script%") do echo %%~na
- 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
)
- 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
- windows
dos
VMPAPP06
VM