Page 1 of 1

Puzzled by Delims

Posted: 2024-Feb-22, 3:45 pm
by Simon_Weel
I want to know the name / make of a pc. So I use this command:

Code: Select all

wmic csproduct get name /format:list
This returns Name=HP Z240 Tower Workstation
Now I would like to strip the Name= part, so I use this:

Code: Select all

FOR /F "usebackq tokens=* delims=Name=" %%F IN (`wmic csproduct get name /format:list ^| findstr/c:"Name="`) DO (echo Unknown PC type: %%F)
And this returns, as desired Unknown PC type: HP Z240 Tower Workstation
But notice the delims=Name=
In my ignorance, I just specified the text I don't want. And it works. But then I read again about delims and it's a list of delimiting characters - not words. So I change the line to this, thinking I'm doing it right:

Code: Select all

FOR /F "usebackq tokens=* delims==" %%F IN (`wmic csproduct get name /format:list ^| findstr/c:"Name="`) DO (echo Unknown PC type: %%F)
This returns Unknown PC type: Name=HP Z240 Tower Workstation

Now I'm confused. The result is ok if I use delims the wrong way and vice versa. What's my mistake?

Re: Puzzled by Delims

Posted: 2024-Feb-22, 8:38 pm
by Simon Sheppard
instead of tokens=*
you need to use tokens=2

Otherwise you will break up the string into 2 parts and the * will just return both of them anyway.

Re: Puzzled by Delims

Posted: 2024-Feb-26, 4:08 pm
by Simon_Weel
Okay, changing tokens=2 solves the problem. It doesn't seem to matter whether delims==" or delims=^=" is used? For clarity, this is IMO the better option delims=^="

Re: Puzzled by Delims

Posted: 2024-Feb-26, 8:37 pm
by Simon Sheppard
^ Ah good point, I edited my post to remove that, thanks

Re: Puzzled by Delims

Posted: 2024-Mar-05, 1:01 pm
by jeb
"delims=^=" is worse, as it defines two delim characters, a caret and an equal sign