Puzzled by Delims

Microsoft Windows
Post Reply
Simon_Weel
Posts: 36
Joined: 2021-Dec-13, 3:53 pm

Puzzled by Delims

Post 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?
User avatar
Simon Sheppard
Posts: 191
Joined: 2021-Jul-10, 7:46 pm
Contact:

Re: Puzzled by Delims

Post 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.
Last edited by Simon Sheppard on 2024-Feb-26, 8:36 pm, edited 1 time in total.
Reason: removed the == derail
Simon_Weel
Posts: 36
Joined: 2021-Dec-13, 3:53 pm

Re: Puzzled by Delims

Post 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=^="
User avatar
Simon Sheppard
Posts: 191
Joined: 2021-Jul-10, 7:46 pm
Contact:

Re: Puzzled by Delims

Post by Simon Sheppard »

^ Ah good point, I edited my post to remove that, thanks
jeb
Posts: 12
Joined: 2023-May-10, 1:28 pm

Re: Puzzled by Delims

Post by jeb »

"delims=^=" is worse, as it defines two delim characters, a caret and an equal sign
Post Reply