PnP
PnP

Reputation: 3185

How to get first part of a delimited string in batch?

I have a string stored in variable %var% which looks like:

something.contains.text - a basic string delimited by "."

I need to end up with just "Something" in a new variable.

So far I can only seem to get contains.text or just contains, but cannot get something

Upvotes: 3

Views: 2821

Answers (2)

MC ND
MC ND

Reputation: 70923

While I would solve it with the code in npocmaka's answer, here's a one-liner for basic strings:

set var=something.contains.text
set new_var=%var:.=&rem %

The way it works is that it replaces the dots with a command concatenation character (&) and starts a comment (rem)

Upvotes: 4

npocmaka
npocmaka

Reputation: 57252

for /f "tokens=1 delims=." %%a in ("%var%") do set "new_var=%%a"

Upvotes: 1

Related Questions