Remove space at end / f

Hi guys, I'm trying to use the FOR /F command in a batch file, but I have a problem.

I run it ....

 rem finding model number for /F "delims= skip=1 tokens=*" %%a in ('wmic csproduct get name') do if not defined model set model=%%a 

and it works well, I get the result I want

 result: 637263G 

Now let's say I create a folder called "637263G" in the c: \ windows \ folder

 eg:c:\windows\637263G\test 

and I create a package to go to the path ...

 for /F "skip=1 tokens=*" %%a in ('wmic csproduct get name') do if not defined model set model=%%a cd windows\%model%\test 

................................

it's a bomb because% model% returns "c:\windows\637263G \test" and not "c:\windows\637263G\test"

This leaves a space after the model name.

Does anyone know how to remove the back space?

+4
source share
4 answers

you can suppress all space in %% a using wildcard:

 set a=%a: =% 
+3
source

You do not have to remove all spaces, as some answers suggest, as some computers have spaces in the middle of the value. See the WMIC CSProduct Test Results List .

Not only do I get a few trailing spaces, but I also get a carriage return ( <CR> ), which is an artifact of automatically converting WMIC unicode output to ASCII.

The final <CR> can be deleted by passing the value through another FOR / F loop. This also removes the phantom "empty" string (actually a <CR> ), so the IF statement is no longer needed.

Trailing spaces can be removed using the ~nx modifier. This works because file names cannot end with a space or a period and are automatically deleted by the OS. Modifiers normalize the "file name" by removing endpoints and spaces. This will not work properly if the value contains \ , / , * or ? , or if it begins with a letter followed by :

 for /F "skip=1 delims=" %%A in ( 'wmic csproduct get name' ) do for /f "delims=" %%B in ("%%A") do set "model=%%~nxB" 

Another option is to use the CSV format wmz sentence, although I still have to remove the final <CR> .

 for /F "skip=2 tokens=2 delims=," %%A in ( 'wmic csproduct get name /format:csv' ) do for /f "delims=" %%B in ("%%A") do set "model=%%B" 


EDIT - Why wmz solution can work without deleting <CR>

The <CR> problem may not be obvious, depending on how you access the model variable after setting it. A normal extension will strip any <CR> , but a slower extension will retain it.

 @echo off setlocal enableDelayedExpansion for /f "skip=2 tokens=2 delims=," %%A in ( 'wmic csproduct get name /format:csv' ) do set "model=%%A" echo Normal expansion strips ^<CR^>: [%model%] echo Delayed expansion preserves ^<CR^>: [!model!] 

--- OUTPUT ---

  Normal expansion strips <CR>: [LX4710-01] ] Delayed expansion preserves <CR>: [LX4710-01 


EDIT is another way to avoid the <CR> problem

You can use the CSV format and request an additional attribute that appears after the desired value. <CR> will be discarded along with an additional value when analyzing the desired token.

 for /f "skip=2 tokens=2 delims=," %%A in ( 'wmic csproduct get name^,vendor /format:csv' ) do set "model=%%A" 
+3
source

In addition to the trim return line (as already suggested), you can also reformat the wmic output so that it does not return padding (spaces), for example:

for /f "skip=2 tokens=2 delims=," %%a in ('wmic csproduct get name /format:csv')

This is especially useful if you want to return more than one column.
(it will return the data as is, so if it includes the trailing space, it will be saved. If you want to accurately trim spaces, use the variable swap)

+2
source

As Kayasax suggested, you can use a variable replacement.
But this only works with regular variables, not with% 1, nor with loop parameters.

 set "model=%model: =%" 

Or, if you are sure that wmic always adds one space, you can also use

 set "model=%model:~0,-1%" 

This removes only the last character

+1
source

All Articles