Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove space at the end of /F

Hi there guys I'm trying to use the FOR /F command in batch file but I'm getting 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 lets say i create a folder called "637263G" under c:\windows\

eg:c:\windows\637263G\test

and i create a batch 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

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

this bombs out because the %model% is returning "c:\windows\637263G \test" and not "c:\windows\637263G\test"

It's leaving a space after the model name.

Does anyone know how to remove the space at the back?

like image 892
user2072055 Avatar asked Jan 15 '23 05:01

user2072055


2 Answers

you can suppress all the space in %%a by using substitution :

set a=%a: =%
like image 68
Loïc MICHEL Avatar answered Jan 24 '23 11:01

Loïc MICHEL


You should not remove all spaces, as some answers suggest, because some computers have spaces in the middle of the value. See List of WMIC CSProduct Get Name Results.

Not only do I get multiple trailing spaces, I also get a trailing carriage return (<CR>) that is an artifact of the automatic conversion of WMIC's unicode output into ASCII.

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

The trailing spaces can then be removed by using the ~nx modifier. This works because file names cannot end with space or dot and are automatically removed by the OS. The modifiers normalize the "file name" by removing trailing dots and spaces. This won't work properly if the value contains \, /, *, or ?, or if it starts 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 wmz's suggestion of CSV format, though I still have to remove the trailing <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 the wmz solution may appear to work, without stripping <CR>

The <CR> problem may not be apparent, depending on how you access the model variable after it has been set. Normal expansion will strip any <CR>, but delayed expansion preserves 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 - another way to avoid the <CR> problem

You can use the CSV format and request an extra attribute that appears after the value you want. The <CR> will be discarded along with the extra value when you parse out the desired token.

for /f "skip=2 tokens=2 delims=," %%A in (
  'wmic csproduct get name^,vendor /format:csv'
) do set "model=%%A"
like image 30
dbenham Avatar answered Jan 24 '23 11:01

dbenham