delims question

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
dossod
Posts: 5
Joined: 28 Feb 2013 18:59

delims question

#1 Post by dossod » 28 Feb 2013 19:04

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

foxidrive
Expert
Posts: 6033
Joined: 10 Feb 2012 02:20

Re: delims question

#2 Post by foxidrive » 28 Feb 2013 19:25

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.

dossod
Posts: 5
Joined: 28 Feb 2013 18:59

Re: delims question

#3 Post by dossod » 28 Feb 2013 19:28

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.

Queue
Posts: 31
Joined: 16 Feb 2013 14:31

Re: delims question

#4 Post by Queue » 28 Feb 2013 19:50

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

dossod
Posts: 5
Joined: 28 Feb 2013 18:59

Re: delims question

#5 Post by dossod » 28 Feb 2013 20:05

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

dossod
Posts: 5
Joined: 28 Feb 2013 18:59

Re: delims question

#6 Post by dossod » 28 Feb 2013 22:23

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.

Squashman
Expert
Posts: 4111
Joined: 23 Dec 2011 13:59

Re: delims question

#7 Post by Squashman » 01 Mar 2013 06:38

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.

Squashman
Expert
Posts: 4111
Joined: 23 Dec 2011 13:59

Re: delims question

#8 Post by Squashman » 01 Mar 2013 06:58

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.

dossod
Posts: 5
Joined: 28 Feb 2013 18:59

Re: delims question

#9 Post by dossod » 01 Mar 2013 19:36

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!

Post Reply