vince said:
Foxidrive,
I was hoping you know some set command, I don't have any script at hand.
Any help, greatly apprecaited.
V
Essentially, you can't.
%date% and %time% are set up by the OS and evaluated at the time of
execution. You CAN set an environment variable "date" (or "time") to
anything you want, and this value then overrides the "system" value until
the USER value is deleted (set to null) but I don't think that helps too
much here..
Here's a demo using "time" (which has a habit of changing faster than
"date") :
----- batch begins -------
[1]@echo off
[2]for /l %%i in (1,1,3) do echo %time%&call :later
[3]for /f "tokens=1,2" %%i in ( ' date /t ' ) do if [%%j]==[] (set yds=%%i)
else (set yds=%%j)
[4]echo %yds%
[5]goto :eof
[6]
[7]:later
[8]echo time is %time%
[9]:: set time=user
[10]echo time is %time%
[11]ping -n 2 127.0.0.1 >nul
[12]goto :eof
------ batch ends --------
Lines start [number] - any lines not starting [number] have been wrapped
and should be rejoined. The [number] that starts the line should be removed
The label :eof is defined in NT+ to be end-of-file but MUST be expressed
as :eof
The spaces surrounding the single-quotes are for emphasis only. The SPACES
are not required but the single-quotes ARE required.
With this demo, with line [9] as a comment, and [11] is a 2-second delay,
there are three reports of 3 lines each
* original-time when [2] was commenced
* time when [8] is executed
* time when [10] is executed
This demonstrates that [2] produces the parse-time value (a well-established
principle) and shows that indiscriminate use of %time% (and by extension,
%date%) can cause problems because it CAN change within a script.
If you then take the leading "::" from line [9] then the report changes:
* original-time when [2] was commenced
* time when [8] is executed
* user
* original-time when [2] was commenced
* user
* user
* original-time when [2] was commenced
* user
* user
which shows that the environment value set by the USER overrides that
supplied by the system.
Note however that [3] will correctly set YDS (your Date String) to the
correct value, regardless of whether the format had a leading dayname.
Hence [3] may be used on both XP and 2K (restoring commonality) to establish
the date-string to be used; possibly by substituting "date" for "yds" OR by
using "%yds%" in place of "%date%" in your batches - provided [3] is
executed at the start of each %date%-using batch. "yds" naturally can be
any variable-name you choose.