取得纯DOS当前日期并赋值给变量,有两种方法:
GetDATE.BAT 内容如下,使用版本: MS-DOS 6.22
@echo off
echo exit|%comspec% /k prompt set mydate=$D$_|find "-">$redtek.bat
call $redtek.bat
for %%. in (%mydate%) do set mydate=%%.
del $redtek.bat >nul
echo %mydate%
第一种方法使用了外部命令(Find.exe 和 Command.com),
但Prompt足可以获得更好玩的内容:)
还有一种方法:(这个批处理文件名必须命名为: CURRENT.BAT)
(因为我在写这个批处理里的时候为了减少一个临时文件,就让它自己调用自己--回调)
我们能得到的 MS-DOS 6.22 “先天”环境变量就这么多:
PATH=C:\DOS
PROMPT=$P$G
COMSPEC=C:\COMMAND.COM
这种方法不用任何外部命令,但只适用于MS-DOS 6.22等纯DOS(由于日期或时间格式问题)。
CURRENT.BAT 内容如下:
@echo %dbg% off
if not == goto :end
echo.|date>$redtek.bat
$redtek.bat
:end
del $redtek.bat > nul
set mydate=%4
echo %mydate%
原理: 当执行 DATE 内部命令要取得系统日期时,你必须要按回车键。
因为这个内部命令即是看系统日期又是更改系统日期的命令。
ECHO. 它代表输出一个空行(回车)
所以, 需要给 DATE 命令一个“额外”的“自动”的回车符,省得我们按。
ECHO.|DATE 这样就无需我们按回车键了,自动显示了当前的日期。
ECHO.|DATE>$REDTEK.BAT 我们它输出的日期重定向到一个文件中。
这个被重定向的文件被命令为一个批处理类型的文件,因为我们还要它执行。
这是系统日期的输出格式:(在我们重定向的那个批处理中就是这样的格式)
Current date is Sun 10-29-2006
Enter new date (mm-dd-yy):
如果我们经常使用带有参数(%1....%9)这样的批处理,
如:(暂定文件名为 A.BAT)
@echo off
echo %1 %2 %3 %4
那么当我们执行上面批处理并如下方式带参数执行时:
A.BAT A B C D E
会如下显示: A B C D ,这个A B C D 就是我们输出的被带入的参数%1到%4
所以,我们已经把当含有当前日期的内容重定向到了一个批处理文件内:
Current date is Sun 10-29-2006
Enter new date (mm-dd-yy):
你为什么不能把那行“Current date is Sun 10-29-2006”的内容中“Current”也当做是一个外部批处理文件呢?
象下面这样:
Current.BAT date is Sun 10-29-2006
这样不行吗?当然可以!所以为什么要将那个输出日期重定向成一个可以执行的批处理文件内。
所以,我们要建一个Current.bat的批处理,让它来输出(显示)第4个参数,
当然就是我们想要取得的“10-29-2006”这个参数了。
新建一个Current.bat又要在主批处理程序之外再新建一个临时文件,
所以,干脆我们就用Current.bat来做主批处理文件,然后只生成一个装有被重定向的系统日期的批处理文件即可。
所以,在Current.bat中,我们让Echo.|date>重定向到一个文件中,
然后让那个文件执行,就相当于带了参数了,
那个文件一执行,会调用Current.bat(现在就是我们自己),
让它自己调用自己,
所以在开始部分我们判断一下如果参数非空(说明那个带日期内容的临时批处理文件执行并带参数进来了),
所以我们就流程转到:END段来执行赋值与显示的操作。
如果我们初次运行CURRENT.BAT这个批处理时,当然不会有参数被带进来,
所以系统按照将日期重定向到一个临时文件……的方法来按照我们的思路执行。
即然可以将一个日期或时间什么的内容赋值给一个变量,
那么是否再找它写入一个文件或用它做什么,还会是很难的事情吗?
Last edited by redtek on 2006-10-28 at 11:10 PM ]
There are two methods to obtain the current date in pure DOS and assign it to a variable:
Method 1: The content of GetDATE.BAT is as follows, using version: MS-DOS 6.22
@echo off
echo exit|%comspec% /k prompt set mydate=$D$_|find "-">$redtek.bat
call $redtek.bat
for %%. in (%mydate%) do set mydate=%%.
del $redtek.bat >nul
echo %mydate%
The first method uses external commands (Find.exe and Command.com), but Prompt can obtain more interesting content :)
Another method: (This batch file must be named: CURRENT.BAT)
(Because when I was writing this batch file, in order to reduce a temporary file, I let it call itself - callback)
The "inherent" environment variables of MS-DOS 6.22 that we can get are as follows:
PATH=C:\DOS
PROMPT=$P$G
COMSPEC=C:\COMMAND.COM
This method does not use any external commands, but it is only applicable to pure DOS such as MS-DOS 6.22 (due to date or time format issues).
The content of CURRENT.BAT is as follows:
@echo %dbg% off
if not == goto :end
echo.|date>$redtek.bat
$redtek.bat
:end
del $redtek.bat > nul
set mydate=%4
echo %mydate%
Principle: When executing the internal command DATE to obtain the system date, you must press the Enter key.
Because this internal command is both to view the system date and to change the system date.
ECHO. represents outputting an empty line (Enter)
So, we need to give the DATE command an "extra" "automatic" Enter key, saving us from pressing it.
ECHO.|DATE does not require us to press the Enter key, and the current date is automatically displayed.
ECHO.|DATE>$REDTEK.BAT We redirect the output date to a file.
This redirected file is commanded as a batch file type because we still need it to execute.
This is the output format of the system date: (This is the format in the batch file we redirected)
Current date is Sun 10-29-2006
Enter new date (mm-dd-yy):
If we often use batch files with parameters (%1....%9),
For example: (tentatively named A.BAT)
@echo off
echo %1 %2 %3 %4
Then when we execute the above batch file and execute it with parameters in the following way:
A.BAT A B C D E
It will display as follows: A B C D, and this A B C D are the parameters %1 to %4 that we output and brought in. So, we have redirected the content containing the current date to a batch file:
Current date is Sun 10-29-2006
Enter new date (mm-dd-yy):
Why can't you also treat the word "Current" in the line "Current date is Sun 10-29-2006" as an external batch file?
Like the following:
Current.BAT date is Sun 10-29-2006
Can't this work? Of course it can! So why redirect the output date into an executable batch file.
So, we need to create a Current.bat batch file to output (display) the 4th parameter,
Of course, it is the "10-29-2006" parameter that we want to obtain.
Creating a new Current.bat also requires creating another temporary file outside the main batch program,
So, simply use Current.bat as the main batch file, and then only generate a batch file with the redirected system date.
So, in Current.bat, we redirect Echo.|date> to a file,
Then let that file execute, which is equivalent to bringing in parameters,
As soon as that file executes, it will call Current.bat (now it's ourselves),
Let it call itself,
So at the beginning, we judge whether the parameter is non-empty (indicating that the temporary batch file with the date content has executed and brought in parameters),
So we transfer the process to the :END segment to perform the assignment and display operations.
If we run the CURRENT.BAT batch file for the first time, of course, no parameters will be brought in,
So the system follows the method of redirecting the date to a temporary file... and executes according to our idea.
Since we can assign a date or time content to a variable,
Then is it still a difficult thing to find it to write to a file or do something with it?
Last edited by redtek on 2006-10-28 at 11:10 PM ]