【发布时间】:2017-12-28 23:38:14
【问题描述】:
我目前正在编写一个批处理文件,记录一个 ip 的 ping 并记录时间。 不幸的是,当我尝试使用 %time% 命令并存储它时,它总是什么都不返回。
当我单独运行命令时它可以工作,只是不能与我的其他代码一起运行
Set packet=0
Set Down=0
Set Up=0
Set time=0
SET /P IP=enter ip.
set counter=0
:ping
Rem ping -n 1 -w 2500 %IP%>>ping.txt
ping -n 1 -w 2500 %IP% > nul 2>&1 && set Test=Pass || set Test=Fail
FOR /F "tokens=1-9 delims==< " %%a IN ('PING -n 1 -w 2500 %IP%') DO IF "%%h"=="TTL" SET RESPONSE=%%g
mode 100
Set time= %TIME:~0,5%
echo %time%
pause
【问题讨论】:
-
不要使用系统变量的名称来存储局部变量。
-
键入
set以查看所有设置的变量,set /?(在末尾)查看易失性变量的名称(变化类似于%time%)。 -
阅读Defining a variable in a batch file not working 上的答案,其中包含有关动态环境变量在命令提示符窗口中运行
set /?时输出的命令SET 输出的相关部分乙>。帮助中清楚地描述了像Set time=0这样的行会覆盖动态变量TIME,使其不再像您希望的那样有用。另见Why is no string output with 'echo %var%' after using 'set var = text' on command line?
标签: batch-file time cmd syntax command