Page 1 of 1

Parse a string to get part after "\\"

Posted: 25 Jan 2012 03:51
by brabus
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.

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

Posted: 25 Jan 2012 04:44
by Ed Dyreen
'
[edit] typo !

Code: Select all

set foobar=\\DPTEST\smspkgf$\GLB0001

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

Code: Select all

?=DPTEST_

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

Posted: 25 Jan 2012 04:50
by brabus
Hi Ed,

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

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

Posted: 25 Jan 2012 05:15
by Ed Dyreen
'
% has to be doubled in a batch script, I made a typo, sorry..

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

Posted: 25 Jan 2012 05:43
by brabus
Super, thank you so much. I will now read the manual to work out how it works . ;)

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

Posted: 25 Jan 2012 11:31
by orange_batch
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......_______.._______

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

Posted: 25 Jan 2012 11:56
by brabus
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 !

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

Posted: 25 Jan 2012 12:31
by orange_batch
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.

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

Posted: 25 Jan 2012 14:50
by brabus
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

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

Posted: 25 Jan 2012 15:12
by Ed Dyreen
'

Code: Select all

set /?
pause

Code: Select all

"%variable:occurence=replacer%"

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

Posted: 25 Jan 2012 15:23
by orange_batch
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"

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

Posted: 26 Jan 2012 00:45
by brabus
Excellent :D I can see clearly now! Guys, thank you so much. Thumbs up / like / +1 etc :)