Search code examples
batch-filecmdwindows-10

Strip trailing spaces from variable


Windows 10 Batch File

Trying to strip trailing spaces from variable. Here's what I have so far...

For /F "Tokens=1* Delims= " %%A In ('WMIC Path Win32_VideoController Get Name^,DriverVersion^|FindStr /I "Intel"') Do @Set GFXNAM=%%B&SetGFXVER=%%A

With that, I get the two variables populated correctly as such...

GFXNAM=[Intel(R) UHD Graphics 630          ]
GFXVER=[27.20.100.9316]
(brackets have been added to visualize trailing spaces in the variables)

However, I need a way to trim those trailing spaces at the end of the GFXNAM variable. Unfortunately, due to the parenthesis within the GFXNAM variable value, I have been unable to use a for loop along with variable manipulation along the lines of...

(set NEWGFXNAM=%GFXNAM:~0,-1%)

It fails due to those lovely poison characters. All I need to do is to be able to strip those trailing spaces from that GFXNAM variable. It shouldn't be this difficult, but I just can't see it.

Any help would be greatly appreciated.

Thanks in advance.


Solution

  • Here is a quick example using my comment, and based upon only having one Intel graphics card in your PC:

    @Echo Off
    SetLocal EnableExtensions
    Set "GFXNam="
    Set "GFXVer="
    For /F Tokens^=6^ Delims^=^" %%G In ('%SystemRoot%\System32\wbem\WMIC.exe Path
     Win32_VideoController Where "Name Like 'Intel%%'" Get Name^, DriverVersion
     /Format:MOF 2^>NUL') Do If Not Defined GFXVer (Set "GFXVer=%%G"
    ) Else Set "GFXNam=%%G"
    Set GFX 2>NUL
    Pause
    

    If you want to check that it did correctly define the variables, you can replace Set GFX 2>NUL with If Defined GFXVer If Defined GFXNam Echo Name: [%GFXNam%], Version: [%GFXVer%].