I'm trying to split a string in a batch file using a string (rather than a character) as the delimiter.
The string has the format:
string1 by string2.txt
The delimiter is by
(yes, space, the word 'by', followed by space). The output I want is:
string1
string2
So, basically, split the string into 2 parts by the delimiter by
and remove the suffix from the second string. How can I do this?
I expanded Magoos answer to get both desired strings:
EDIT: just to prove, my solution also works with the additional requirements:
Output:
I've found two older scripts that use an indefinite or even a specific string to split. As an approach, these are always helpful.
https://www.administrator.de/contentid/226533#comment-1059704 https://www.administrator.de/contentid/267522#comment-1000886
Try this:
that is, take the first and third tokens delimited by space or point...
The above SET command will remove the unwanted data. Result shown between + to demonstrate absence of spaces.
Formula: set var=%somevar:*string1=string2%
will assign to var the value of somevar with all characters up to string1 replaced by string2. The enclosing quotes in a set command ensure that any stray trailing spaces on the line are not included in the value assigned.
I recently discovered an interesting trick that allows to "Split String With String As Delimiter", so I couldn't resist the temptation to post it here as a new answer. Note that "obviously the question wasn't accurate. Firstly, both string1 and string2 can contain spaces. Secondly, both string1 and string2 can contain ampersands ('&')". This method correctly works with the new specifications (posted as a comment below Stephan's answer).
For further details on the split method, see this post.