1

I am writing to ask a question about a response provided for a post from 2017 - linked. The original poster wanted to know how to extract all text from a string that came after a specific character (in this case $). The simplest solution which was given is the code shown below. However, I am unclear about how this code actually works - specifically the line set "result=%str:$=" & set "result=%". I have done some testing and found that the variable "result" has a value when called within the line (between the two commands linked by the &) and a different value when called after the line. But I also noticed you will generate an error if you try to remove the command after the & and call the variable on a lower line. You will also get an error if you remove the second command but still try to call the variable within the line. The two commands are clearly working in tandem but I can't figure out what they are communicating or how this piece of code connects the two. Can someone please explain what process is actually occurring to achieve this result?

How to extract text after $ in batch file?

set "str=Te$ting thi$ $cript for $zero"
set "result=%str:$=" & set "result=%"
echo %result%
  • For a further explanation of this method, you may review _the comment_ below [this answer](https://stackoverflow.com/questions/23600775/split-string-with-string-as-delimiter/33131797#33131797), and a lot of similar examples in the [original source](https://www.dostips.com/forum/viewtopic.php?f=3&t=6429) of the method... – Aacini Sep 22 '22 at 05:36

1 Answers1

1

Technically, they aren't two commands. The command is:

set "result=%str:$=" & set "result=%"

(which is NOT (as it seems) two commands set "result=%str:$=" and set "result=%", but just one set (due to the surrounding quotes) sets the variable result to the string str:$=" & set "result= (note the quoting).)

So, what does it do?

Each $ is replaced by (literally) " & set "result=" (which is merely a string at this point, not a command), resulting in the line (which you see when running the script with echo off):

set "result=Te"   & set "result=ting thi"   & set "result= "   & set "result=cript for "   & set "result=zero"

Which (for better readability) is the same as:

set "result=Te"
set "result=ting thi"
set "result= "
set "result=cript for "
set "result=zero"

effectively setting the same variable to each substring (delimited by $) and in the end resulting in the variable holding the last substring.

Stephan
  • 53,940
  • 10
  • 58
  • 91
  • Oh wow. That is a bizarre way to do it, but I suddenly see why it takes the final token after the last $. Can you just clarify that when you say - Each $ is replaced by (literally) " & set "result=" - the last set of quotes would not actually be captured in the replacement, correct? Just confirming that last double quote belongs to the overall set command so it wouldn't go in the string replacement. – DerekNotCool Sep 21 '22 at 15:28
  • Yes, the last quote belongs to the initial `set` command. Like `set "var=value"` - just that `value` is a bit more ... unintuitive. – Stephan Sep 21 '22 at 16:18
  • The quotes between the two `%` don't end the `set` command because they are technically part of the value. This basically works because variables are expanded *before* executing the line. If you want to dive deeper into this rabbit hole, [read this](https://stackoverflow.com/questions/4094699/how-does-the-windows-command-interpreter-cmd-exe-parse-scripts), but take your time and prepare a big pot of coffee - you'll need it. – Stephan Sep 21 '22 at 16:26
  • This has been hugely insightful! Thanks for the tips, Stephan! – DerekNotCool Sep 23 '22 at 14:10