Parse a string to get part after "\\"

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
brabus
Posts: 6
Joined: 25 Jan 2012 03:48

Parse a string to get part after "\\"

#1 Post by brabus » 25 Jan 2012 03:51

Hi folks,

I need to parse the following string.

set foobar=\\DPTEST\smspkgf$\GLB0001

to extract "DPTEST", ie, inbetween "\\" and first "\".

I had tried
for %%a in ("%foobar:\=" "%") do echo %%~a
but its churning out everything.
The readme for the for loop is not so obvious for this sort of work.

Any ideas at all?

Thanks.

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: Parse a string to get part after "\\"

#2 Post by Ed Dyreen » 25 Jan 2012 04:44

'
[edit] typo !

Code: Select all

set foobar=\\DPTEST\smspkgf$\GLB0001

for /f "tokens=1 delims=\" %%? in ("%foobar%") do echo.?=%%?_

Code: Select all

?=DPTEST_

brabus
Posts: 6
Joined: 25 Jan 2012 03:48

Re: Parse a string to get part after "\\"

#3 Post by brabus » 25 Jan 2012 04:50

Hi Ed,

thanks for reply. But is that last part truncated? As its not working.

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: Parse a string to get part after "\\"

#4 Post by Ed Dyreen » 25 Jan 2012 05:15

'
% has to be doubled in a batch script, I made a typo, sorry..

brabus
Posts: 6
Joined: 25 Jan 2012 03:48

Re: Parse a string to get part after "\\"

#5 Post by brabus » 25 Jan 2012 05:43

Super, thank you so much. I will now read the manual to work out how it works . ;)

orange_batch
Expert
Posts: 442
Joined: 01 Aug 2010 17:13
Location: Canadian Pacific
Contact:

Re: Parse a string to get part after "\\"

#6 Post by orange_batch » 25 Jan 2012 11:31

Yes, read for /?, but I thought I would explain a little myself.

Here's your standard version:

Code: Select all

set string=\\DPTEST\smspkgf$\GLB0001

for /f "delims=\ tokens=1" %%a in ("%string%") do (
echo %%a
)

for /f = for's text-processing mode.

"delims=\ tokens=1" = Options. delims sets which characters act as delimiters in your text. tokens sets which values are retrieved from the delimited text. There are two more options, eol (end of line character, not very important) and usebackq (only important for writing the path to a text file for processing, as explained later).

%%a = Starting letter, case-sensitive, descending alphabetically, which will contain each value specified in tokens. For example, if tokens=2,5-8* and this is %%c:

%%c = token 2
%%d = token 5
%%e = token 6
%%f = token 7
%%g = token 8
%%h = "token *", the remainder of the string unprocessed (so it includes all remaining delimiters)

in ("%string%") = here you specify what type of output for /f will get its text to process from. So:

in (fileset) = text file to open and process the text inside, line by line.
in ("string") = string to process.
in ('command') = command to execute, and any text output is processed.

Obviously fileset cannot contain spaces or special characters in the path to a text file. You also cannot process the content of a string including quotation marks around it (though you could just supply them within your code). To fix this, simply writing the option usebackq into the options area of for /f will change the quotation mark processing as follows:

in ("fileset") = text file to open and process the text inside, line by line.
in ('"string"') = string to process.
in (`command`) = command to execute, and any text output is processed.

In both cases, command can be passed within quotation marks as well ('"command"' or `"command"`), to ease special character handling.

So what our specific example does is, from %%a...

\\DPTEST\smspkgf$\GLB0001

\\token 1\(token 2)\(token 3)

..%%a......_______.._______

brabus
Posts: 6
Joined: 25 Jan 2012 03:48

Re: Parse a string to get part after "\\"

#7 Post by brabus » 25 Jan 2012 11:56

Thats a really nice, sharp explanation. It is clear to me now.

Heres another question - how does the line below work without "/f" switch?? Can you explain to me what is happening? Is it just the standard version condensed in shorthand?

for %%a in ("%foobar:\=" "%") do echo %%~a

Again, thank you !

orange_batch
Expert
Posts: 442
Joined: 01 Aug 2010 17:13
Location: Canadian Pacific
Contact:

Re: Parse a string to get part after "\\"

#8 Post by orange_batch » 25 Jan 2012 12:31

Well,

"%foobar:\=" "%"

outputs this:

"" "" "DPTEST" "smspkgf$" "GLB0001"

The two empty quotation marks cause two iterations in the for loop.

Normal for will iterate for each item within (), using the delimiters space, tab, and comma, except those within quotation marks. It can only process strings. Otherwise, there is no real difference that I know of.

brabus
Posts: 6
Joined: 25 Jan 2012 03:48

Re: Parse a string to get part after "\\"

#9 Post by brabus » 25 Jan 2012 14:50

Thanks orange batch, but what I would like is a detailed explanation of what exactly this means : "%foobar:\=" "%" ie. what do the percentage signs and two sets of double quotes signify? Is this a regular expression?

thanks again

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: Parse a string to get part after "\\"

#10 Post by Ed Dyreen » 25 Jan 2012 15:12

'

Code: Select all

set /?
pause

Code: Select all

"%variable:occurence=replacer%"

orange_batch
Expert
Posts: 442
Joined: 01 Aug 2010 17:13
Location: Canadian Pacific
Contact:

Re: Parse a string to get part after "\\"

#11 Post by orange_batch » 25 Jan 2012 15:23

Ok, you have a variable foobar which is expanded when you write it as %foobar%.

When expanded, you can replace matching text using the syntax: %foobar:apples=oranges%

With %foobar:\=" "%, all \ become " ":
\\DPTEST\smspkgf$\GLB0001 becomes " "" "DPTEST" "smspkgf$" "GLB0001

And it's just surrounded in quotes "%foobar:\=" "%":
\\DPTEST\smspkgf$\GLB0001 becomes "" "" "DPTEST" "smspkgf$" "GLB0001"

brabus
Posts: 6
Joined: 25 Jan 2012 03:48

Re: Parse a string to get part after "\\"

#12 Post by brabus » 26 Jan 2012 00:45

Excellent :D I can see clearly now! Guys, thank you so much. Thumbs up / like / +1 etc :)

Post Reply