Parse a string to get part after "\\"
Moderator: DosItHelp
Parse a string to get part after "\\"
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.
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 "\\"
'
[edit] typo !
[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 "\\"
Hi Ed,
thanks for reply. But is that last part truncated? As its not working.
thanks for reply. But is that last part truncated? As its not working.
Re: Parse a string to get part after "\\"
'
% has to be doubled in a batch script, I made a typo, sorry..
% has to be doubled in a batch script, I made a typo, sorry..
Re: Parse a string to get part after "\\"
Super, thank you so much. I will now read the manual to work out how it works .
-
- Expert
- Posts: 442
- Joined: 01 Aug 2010 17:13
- Location: Canadian Pacific
- Contact:
Re: Parse a string to get part after "\\"
Yes, read for /?, but I thought I would explain a little myself.
Here's your standard version:
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......_______.._______
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 "\\"
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 !
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 !
-
- Expert
- Posts: 442
- Joined: 01 Aug 2010 17:13
- Location: Canadian Pacific
- Contact:
Re: Parse a string to get part after "\\"
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.
"%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 "\\"
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
thanks again
-
- Expert
- Posts: 442
- Joined: 01 Aug 2010 17:13
- Location: Canadian Pacific
- Contact:
Re: Parse a string to get part after "\\"
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"
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 "\\"
Excellent I can see clearly now! Guys, thank you so much. Thumbs up / like / +1 etc