How do I get rid of mid spaces in an environment variable?
Suppose I have the following for loop fragment that get the free disk space size of a certain disk.
for /f 'tokens=3' %%i in ('dir ^| find 'Dir(s)'') do set size=%%i
would result something like this
C:\>set size=129,028,096
I would like to get rid of delimeters as follows.
for /f 'delims=,' %%i in ('echo %size%') do set size=%%i
and resulted something like this
set size=129 028 096
How can I get it with no mid spaces or delimiters with native tools?
You can use
afterwards which will replace all spaces with nothing.
But you can also do that with commas 🙂
But keep in mind that (a) the delimiter changes, it’s a dot for me, not a comma. That may depend on your OS language or regional settings. And (b) you can’t really do math with that value afterwards as
cmdis limited to signed 32-bit math (usingset /a).Your second
forstatement won’t work properly, by the way, since it will just setsizeto the last digit group. That can be circumvented but I’d rather advise to use the replacing directly on commas/dots: