Hassan
Hassan

Reputation:

How do I get rid of mid spaces in an environment variable

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?

Upvotes: 0

Views: 199

Answers (1)

Joey
Joey

Reputation: 354366

You can use

set size=%size: =%

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 cmd is limited to signed 32-bit math (using set /a).

Your second for statement won't work properly, by the way, since it will just set size to the last digit group. That can be circumvented but I'd rather advise to use the replacing directly on commas/dots:

set size=%size:,=%
set size=%size:.=%

Upvotes: 1

Related Questions