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循环片段获取某个磁盘的可用磁盘空间大小。
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?
如何在没有中间空格或使用原生工具的分隔符的情况下获得它?
1 个解决方案
#1
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
).
但请记住(a)分隔符更改,它对我来说是一个点,而不是逗号。这可能取决于您的操作系统语言或区域设置。并且(b)之后你无法用该值进行数学运算,因为cmd仅限于带符号的32位数学运算(使用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:
顺便说一句,你的第二个for语句将无法正常工作,因为它只会将大小设置为最后一个数字组。这可以被规避,但我宁愿建议直接用逗号/点替换:
set size=%size:,=%
set size=%size:.=%
#1
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
).
但请记住(a)分隔符更改,它对我来说是一个点,而不是逗号。这可能取决于您的操作系统语言或区域设置。并且(b)之后你无法用该值进行数学运算,因为cmd仅限于带符号的32位数学运算(使用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:
顺便说一句,你的第二个for语句将无法正常工作,因为它只会将大小设置为最后一个数字组。这可以被规避,但我宁愿建议直接用逗号/点替换:
set size=%size:,=%
set size=%size:.=%