为什么这些 DOS 变量是空的? (例如 %DATE%、%ERRORLEVEL%)

Why are these DOS Variables Empty? (e.g. %DATE%, %ERRORLEVEL%)

我正在使用 Windows XP SP3。

如果我打开命令提示符 window,然后键入

ECHO %DATE%

我得到:

14-08-2018

就像我应该的那样。

但是如果我退出 Windows 并从具有 DOS 的可启动 USB 闪存驱动器启动我的计算机
(真正的DOS,不是Windows里面的命令提示符),
并输入相同的命令,
那么 %DATE% 似乎是空的。

命令

ECHO %DATE%

回复

ECHO is On

因为它认为我刚刚写了ECHO,
因为从 %DATE% 返回的值是 "".

我尝试了 2 个不同的 DOS 版本..
Win98的DOS,WinME的DOS。
(2 个不同的 USB 闪存驱动器)

有谁知道为什么我无法在真正的 DOS 中获取日期,
当我在 Windows' 命令提示符下时可以成功获取它?

%ERRORLEVEL% 不是 DOS 中的变量

%ERRORLEVEL%

In COMMAND.COM of DR-DOS 7.02 and higher, this pseudo-variable returns the last error level returned by an external program or the RETURN command, f.e. "0".."255". See also the identically named pseudo-variable %ERRORLEVEL% under Windows and the IF ERRORLEVEL conditional command.

https://en.wikipedia.org/wiki/Environment_variable#DOS

如您所见,它仅存在于 DR-DOS 7.02 及 Windows 之后。 DR-DOS 还有 %ERRORLVL%

OTOH %DATE% 仅存在于 Windows

%DATE%

This pseudo-variable expands to the current date. The date is displayed according to the current user's date format preferences.

https://en.wikipedia.org/wiki/Environment_variable#Windows

它们是 cmd.exe 的新功能,需要 command extension 才能启用

If Command Extensions are disabled, the following dynamic variables will be not accessible:

%CD% %DATE% %TIME% %RANDOM% %ERRORLEVEL% %CMDEXTVERSION% %CMDCMDLINE% %HIGHESTNUMANODENUMBER%

https://ss64.com/nt/syntax-variables.html

因此,如果您为 DOS 兼容性关闭命令扩展,您也会在 Windows cmd.exe

上丢失这些变量