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 ]