保存数值批处理文件 [英] Save values batch file
问题描述
我写这个剧本,以寻找具有足够的可用空间量:
I wrote this script to looking for volume with enough free space:
@echo on
setlocal
set gbsize=1,073,741,824
Set gbsize=%gbsize:,=%
for %%A in (A B C D) do (
for /f "tokens=3,4,5,*" %%B in ('dir %%A:\') do (
set bytesfree=%%B
set bytesfree=%bytesfree:,=%
if %%D == free If %bytesfree% gtr %gbsize% echo hi
)
)
我的问题是,bytesfree变量dosent保存其值。输出(回波上)
My problem is that the bytesfree variable dosent save its value. the output is(echo is on)
C:\Users\Desktop>(
set bytesfree=**780,607,488**
set bytesfree=**23167987712**
if free == free If 23167987712 GTR 1073741824 echo hi
)
hi
看起来像bytesfree游离缺失其值。
任何人都可以请帮助?并提供一些外植?
谢谢。
looks like the bytesfree losed its value. Can anyone please help? and provide some explantation? thanks.
推荐答案
使用
setlocal enabledelayedexpansion
而不仅仅是 SETLOCAL
,然后用!bytesfree!
来引用变量(只需更换%
按!
)。
instead of just setlocal
and then use !bytesfree!
to refer to the variable (just replace %
by !
).
这是因为 CMD
扩展变量,因为它的分析的命令,而不是当运行命令(其中,显然会发生的之后的解析)。 A 命令的在这种情况下也可以是一个完整的为
语句的包括的后它块。因此,%bytesfree%
的所有实例的 的内环路由它们的值替换的前的循环,这恰好是一个空字符串。
This is because cmd
expands variables as it parses a command, not when the command is run (which, obviously happens after parsing). A command in this case could also be a complete for
statement including the block after it. So all instances of %bytesfree%
within the loop are replaced by their value before the loop, which happens to be an empty string.
延迟扩展是变量扩展的一种特殊形式,当一个命令,而不是运行分析,当它的它扩展变量。它可与启用
Delayed expansion is a special form of variable expansion which expands variables when a command is run instead of when parsing it. It can be enabled with
setlocal enabledelayedexpansion
(内一个批处理文件)或
(within a batch file) or
cmd /v:on
(一个完整的会话,但是的不的批处理文件,除非你不想要它恢复)。
(for a complete session, but not in a batch file, unless you don't want it to resume).
然后语法!bytesfree!
被用来指变量然后将其膨胀之前,为了运行的命令。因此,当设置的和的使用同一个变量的在的为循环块(,
如果
等)你pretty多少总是要使用延迟扩展。
Then the syntax !bytesfree!
is used to refer to variables which are then expanded just prior to running a command. So when setting and using the same variable within a block (for
loop, if
, etc.) you pretty much always want to use delayed expansion.
这篇关于保存数值批处理文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!