Page 1 of 1

delims question

Posted: 28 Feb 2013 19:04
by dossod
I'd like to use delims to have my tokens separated by strings instead of a single character.

For instance, in the string below foo and bar would be my delims

thisfooisbarmyString

So token 1 would be this, 2 is, 3 myString

However this would only work if I can get delims to use multi-character strings for each delimiter.

Possible?

Thanks in advance

Re: delims question

Posted: 28 Feb 2013 19:25
by foxidrive
This is a workaround:

Code: Select all

set "str=thisfooisbarmyString"
set "str=%str:foo= %"
set "str=%str:bar= %"
echo "%str%"



You can't use a string in delims= and make it work, unless the characters in the string are unique and are all removed from the source text.

Re: delims question

Posted: 28 Feb 2013 19:28
by dossod
Figured it out, sort of.

I can do this and have it split on xyz

tokens=1,2,3 delims=(xyz)

so this myxyzfilexyzhere

can become my file here

and that's a big improvement, but I still haven't gotten it to work on two multi-character delims

or more properly, "tokens=1,2,3 delims=(foo)" just to show I'm ending the delims set there.

Re: delims question

Posted: 28 Feb 2013 19:50
by Queue
Can you do delims=(xyz)(abc)?
for /f "tokens=2 delims=(jk)(lm)" %a in ("onejktwolmthree") do echo %a
for /f "tokens=3 delims=(jk)(lm)" %a in ("onejktwolmthree") do echo %a
at the command prompt gaves the results "two" and "three"

I'm not sure if it's actually working, or working coincidentally.

Edit - It was working coincidentally; it's a red herring. I'd never even considered trying to work multi-char delims in for.

foxidrive's suggestion is your solution; preprocess the string with string substitutions so you have proper delimiters, then you can pass it through a for statement.

Queue

Re: delims question

Posted: 28 Feb 2013 20:05
by dossod
My "solution" was also a red herring. Using the delims I showed above;

thisxismyxyxyzfilexxyzxyeah

returns

this ism file

Edit: I don't think foxidrive's solution will work for me because of the nature of what I'm trying to do. I'm attempting to read directories and substitute parts of filenames with replacement strings. It may be possible to do in a for loop but my DOS skills are very rusty. Now if I was doing this on a UNIX box I'd just use Perl and be done with it posthaste. I haven't done much with DOS since '94

Re: delims question

Posted: 28 Feb 2013 22:23
by dossod
Got it!

rem Starting string is thisxismyxyxyzfilexxyzxyeah

setlocal enabledelayedexpansion

for /f "tokens=*" %%a in (filesToRen.txt) do (
echo Moving %%a
set newString=%%a
set "newString=!newString:xyz=.!"
echo "!newString!"
)

Returns:

Moving thisxismyxyxyzfilexxyzxyeah
thisxismyxy.filex.xyeah

Thank you for pointing me in the right direction folks. My first exposure to programming was batch files and I do still enjoy them.

Re: delims question

Posted: 01 Mar 2013 06:38
by Squashman
dossod wrote: Now if I was doing this on a UNIX box I'd just use Perl and be done with it posthaste.

So install Perl on your computer. It's not for Unix only.

Re: delims question

Posted: 01 Mar 2013 06:58
by Squashman
Queue wrote:Can you do delims=(xyz)(abc)?
for /f "tokens=2 delims=(jk)(lm)" %a in ("onejktwolmthree") do echo %a
for /f "tokens=3 delims=(jk)(lm)" %a in ("onejktwolmthree") do echo %a
at the command prompt gaves the results "two" and "three"

I'm not sure if it's actually working, or working coincidentally.

Edit - It was working coincidentally; it's a red herring. I'd never even considered trying to work multi-char delims in for.

foxidrive's suggestion is your solution; preprocess the string with string substitutions so you have proper delimiters, then you can pass it through a for statement.

Queue

It works because JK and LM are not part of any of the output you need and consecutive delimiters are treated as one.

Re: delims question

Posted: 01 Mar 2013 19:36
by dossod
The for loop with the reassignment of %%a worked fine. It doesn't give me the option of using multiple multi-character strings, but using one long one multiple times is nearly as good.

Yes Perl can be installed on a PC, and I have done so in the past. But while I play on my PC I don't do enough on it to require Perl. Probably better anyway so that my DOS skills don't totally leave me.

Thanks again for the help!