This article uses the Subroutines published in the two threads:
(1) Subject: Files or folders named from current date and time
Date: Wed, 13 Apr 2005 19:57:21 +0100
(2) Subject: Find date order for locale
Date: Thu, 14 Apr 2005 23:34:00 +0100
A PC keeps time with a Real Time Clock (RTClock), which is shielded
from most interference arising from software in use. At each boot time,
a software or "MS-DOS" clock is initialised from the RTClock. During
use, the MS-DOS clock suffers from interference by software and may
drift relative to the RTClock (think of the RTC as matching your
wristwatch - indeed, you may have used your wristwatch to set it).
When you set the time with either command-line TIME or DATE, or with
Windows Date/Time properties, both clocks are synchronized.
If you are creating files or folders with date/time styled names,
you need to consider which clock strategy is best for you:
(a) Use the RTClock to match external events, say to match dates
or times on remote networks or for FTP operations
(b) Use the MS-DOS clock to match file/folder creation dates/times
(c) Synchronize both clocks before generating date/time tokens
The demo below shows how to synchronize both clocks prior to creating
date/time tokens for file names. It also shows how to determine the
locale date order, and has two ECHO{demo}-ed built-in examples:
(1) Create a folder named to YYYY-MM-DD current date
(2) Create a folder named to Locale-order current date
The demo uses no third-party utilities.
It also:
(i) has a two-stage confirm of the go-ahead to synchronize
(ii) includes rollover avoidance (operation is paused if used in the
two minutes prior to the midnight rollover) to avoid problems
that can arise at the rollover instant, then resumes. The
two-minute time is illustrated so that the method is easily
extended to longer time periods by adding more HourMinute checks
to the :AVOID IF command (as you prefer)
(iii) accepts an "auto" parameter: syncdemo.bat auto
which bypasses the two-stage confirm for use in Batch operations
running automatically (the rollover avoidance is _not_ by-passed)
It's an example of how to build a complex Batch file from isolated
Subroutines that can be tested/developed separately. In this case
the separate Subroutines have been developed in the previous posts.
Lines that don't begin with two spaces have wrapped accidentally
====Begin cut-and-paste (omit this line)
@ECHO OFF
IF [GOTO:]==[%1] GOTO %2 {Subroutine-Handler}
:: Recall self with increased environment space, auto=%1
%COMSPEC%/e:4096/c %0 GOTO: _MAIN %1
GOTO EOF {=Main-batch-code-is-below=}
:_MAIN (increased environment space, auto parameter now %3)
CALL %0 GOTO: _DTM
ECHO. RTC time: Y=%Y% MO=%MO% D=%D% H=%H% MI=%MI% S=%S%
CALL %0 GOTO: _DTMD
ECHO. DOS time: Y=%Y% MO=%MO% D=%D% H=%H% MI=%MI% S=%S% U=%U%
:: Skip CHOICE if in non-interactive mode
IF [%3]==[auto] GOTO AVOID
choice /c:ny /n " Set DOS time from RTC time? y/n "
IF ERRORLEVEL 1 IF NOT ERRORLEVEL 2 ECHO. No action taken
IF ERRORLEVEL 1 IF NOT ERRORLEVEL 2 GOTO EOF
choice /c:ng /n " Confirm with g to go ahead g/n "
IF ERRORLEVEL 1 IF NOT ERRORLEVEL 2 ECHO. No action taken
IF ERRORLEVEL 1 IF NOT ERRORLEVEL 2 GOTO EOF
:AVOID (midnight rollover on RTClock time)
:: Pause for 2 minutes + 1 second =PING -n 121+1 if current
:: RTClock time is in the two minutes before midnight rollover
CALL %0 GOTO: _DTM
IF NOT (%H%%MI%)==(2359) IF NOT (%H%%MI%)==(2358) GOTO RESUME
ECHO. Waiting for 23:59/00:00 rollover
ping -n 122 127.0.0.1>NUL
:: Loop back (in case Ctrl-C was used to interrupt PING delay)
GOTO AVOID
:RESUME
:: First establish DATE element order for current locale
:: Recall _DTM to get current RTClock time tokens
CALL %0 GOTO: _DTM
:: Create a file
TYPE NUL>%TEMP%.\_DFORM1.TMP
:: Check date format
SET DF=???
:: Check this (more likely) trio first, in suborder more likely first
CALL %0 GOTO: _DFM 12-31-2099 %MO%-%D%-%C%%Y% 31-12-2099 %D%-%MO%-%C%%Y% 2099-12-31 %C%%Y%-%MO%-%D%
:: If necessary check the rest (unlikely)
IF %DF%==??? CALL %0 GOTO: _DFM 2099-31-12 %C%%Y%-%D%-%MO% 12-2099-31 %MO%-%C%%Y%-%D% 31-2099-12 %D%-%C%%Y%-%MO%
:: Clear the temporary file
DEL %TEMP%.\_DFORM1.TMP
IF %DF%==??? ECHO. No valid date order found, aborting...
IF %DF%==??? GOTO EOF
:: With DATE order established, do the reset
DATE %DF%
:: Echo the command just executed
ECHO. (executed) DATE %DF%
:: Give example of making a folder
:: Remove ECHO.{demo*} to activate command
ECHO.{demo-YMD} MD C:\DATA\%C%%Y%-%MO%-%D%.DAT
ECHO.{demo-Locale}MD C:\DATA\%DF%.DAT
:: Redisplay the two clocks
CALL %0 GOTO: _DTM
ECHO. RTC time: Y=%Y% MO=%MO% D=%D% H=%H% MI=%MI% S=%S%
CALL %0 GOTO: _DTMD
ECHO. DOS time: Y=%Y% MO=%MO% D=%D% H=%H% MI=%MI% S=%S% U=%U%
GOTO EOF {=Subroutine-section-below=}
:_DTM (Usage: CALL %0 GOTO: _DTM)
%COMSPEC%/c %0 GOTO: 2_DTM ECHO. REM:|debug|find ":">%TEMP%.\_DTM.BAT
%COMSPEC%/c %0 GOTO: 2_DTM REM: ECHO.|debug %TEMP%.\_DTM.BAT>NUL
CALL %TEMP%.\_DTM.BAT %0 GOTO: 1_DTM
DEL %TEMP%.\_DTM.BAT
GOTO EOF
:1_DTM
FOR %%V IN ("H=%7" "MI=%6" "S=%3" "C=%9" "Y=%8" "MO=%5" "D=%4") DO SET %%V
GOTO EOF
:2_DTM
%3e100 e8 0 0 80 c3 2 88 dc cd 1a 89 97 1e 1 89 8f 22 1 c3
FOR %%F IN (g d120L8 q) DO %3%%F
FOR %%F IN ("e100'CALL %%1 %%2 %%3'" w q) DO %4%%F
GOTO EOF {=Subroutine-section-below=}
:_DTMD (Usage: CALL %0 GOTO: _DTMD)
%COMSPEC% /c %0 GOTO: 2_DTMD ECHO. %TEMP%.\_DTMD.BAT|debug>NUL
FOR %%C IN (CALL DEL) DO %%C %TEMP%.\_DTMD.BAT
CALL %0 GOTO: 1_DTMD %D%
GOTO EOF
:1_DTMD
FOR %%V IN ("Y=%7" "MO=%9" "D=%8" "H=%3" "MI=%4" "S=%6" "U=%5") DO SET %%V
GOTO EOF
:2_DTMD Create Debug Script
%3f100 11c 20
%3e100'SET D='
%3e90 eb d d4 a 5 30 30 88 24 46 88 4 46 46 c3 be 6 1 b4
%3ea3 2c cd 21 88 e8 e8 e7 ff e8 8 0 b4 2a cd 21 81 e9 d0
%3eb5 7 88 c8 e8 d7 ff 88 d0 e8 d2 ff 88 f0 e8 cd ff c3
%3e11a d a
FOR %%C IN (rip 90 g rcx 1c n%4 w q) DO %3%%C
GOTO EOF {=Subroutine-section-below=}
:_DFM (Usage: CALL %0 GOTO: _DFM PossibleDate)
IF NOT %DF%==??? GOTO EOF
xcopy /l /d:%3 %TEMP%.\_DFORM1.TMP>NUL
IF NOT ERRORLEVEL 4 SET DF=%4
SHIFT
SHIFT
GOTO _DFM
:EOF {End-of-file}
====End cut-and-paste (omit this line)
For Win95/98/ME study/demo use. Cut-and-paste as plain-text Batch file.
Batch file troubleshooting: http://www.allenware.com/find?UsualSuspects
============Screen capture Windows 95
C:\WORK>syncdemo auto
RTC time: Y=05 MO=04 D=22 H=06 MI=57 S=36
DOS time: Y=05 MO=04 D=22 H=06 MI=57 S=37 U=01
(executed) DATE 22-04-2005
{demo-YMD} MD C:\DATA\2005-04-22.DAT
{demo-Locale}MD C:\DATA\22-04-2005.DAT
RTC time: Y=05 MO=04 D=22 H=06 MI=57 S=38
DOS time: Y=05 MO=04 D=22 H=06 MI=57 S=38 U=76
C:\WORK>
============End screen capture
Notes:
(1) Source code for the hex-packed routine in _DTMD was published
in the previous (13 Apr 2005) post. Other documentation in our
Dates/times StudyPack http://www.allenware.com/find?DatesTimes
(2) The synchronization works by issuing a DATE command for the
current locale. When a DATE command is executed, the two clocks
are synchronized by your system to avoid 24-hour out-of-sync date
errors. This feature of the DATE command was first drawn to my
attention by John Stockton. As a result, I decided to use DATE to
synchronize clocks instead of feeding the time tokens from one
clock to the other, to avoid time lags in generating the tokens.
(3) The date 31 December 2099 used in the XCOPY /L command is not a
critical one, and you can use any suitable date. It's better on
balance to use a far-future date: that way, if you need to run
the Batch file without the XCOPY /L switch (which suppresses
copying), there is still no copy action. All that is required of
this date is that it should be invalid for the locale in all but
one possible order. Any 4-digit Year and any Day>=13 will suffice.
For details of how to use and write Batch Subroutines, you can read our
Subroutine StudyPack at: http://www.allenware.com/find?BatchSubroutines
This StudyPack includes an interactive Tutorial, in colour, which walks
you through the logic flow of a Subroutine to show how it works and how
parameters are passed to it. There is also a template that you can use.
--
William Allen
Free interactive Batch Course http://www.allenware.com/icsw/icswidx.htm
Batch Reference with examples http://www.allenware.com/icsw/icswref.htm
Header email is rarely checked. Contact us at http://www.allenware.com/
> Synchronize Real Time Clock with MS-DOS Clock
All this is no longer very interesting, when on most systems clocks are
synchronized with an atomic clock whzenever they access the internet. In
DOS-only times, however, this was all very relevant, because even the
system clock tended to drift (no chance to get a chronometer certificate
:-)) One method to correct that drift automatically was to measure the
drift for a few days (better weeks) against a time signal (e. g. from
your radio or wristwatch). You could then calculate a correction factor
which could be applied e. g. on every boot.
This is exactly what I did (under 4DOS) in on of my 4XBTM batches
(XCORTIME.BTM): the batch can be used to start an evaluation period, end
that period and calculate a correction factor, and apply it when called
with the appropriate switches.
This went well with my BOOTEX batch which handled different jobs at boot
time dayly or in specified intervals.
:-) Just a little promotion for my old batches, for a change :-)
--
* Klaus Meinhard *
www.4dos.info
- 4DOS Info -
- Info for DOS -
Most, maybe; but not all.
In a measurement system that I worked on, it was not even practical to
run Windows (if measuring properly) while working, let alone Net
software; it was absolutely necessary to support a high and regular
processing rate for the fibre-optic ring interrupt, with considerable
processing for each interrupt received. Therefore, DOS; and no disc I/O
during actual measurement either. But accurate clock time was not
needed there; the DOS clock was more than adequate, if checked against a
wall or wrist standard.
Some commercial/military/government systems may for various reasons of
policy be isolated from the Net, and fitting a high-grade time server
may not be justifiable.
But I see the RTC clock as substantially a boot-time convenience; the
proportion of applications isolated from a proper time server and for
which DOS time is inadequate but RTC time suffices cannot be large.
--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 MIME. ©
Web <URL:http://www.merlyn.demon.co.uk/> - w. FAQish topics, links, acronyms
PAS EXE etc : <URL:http://www.merlyn.demon.co.uk/programs/> - see 00index.htm
Dates - miscdate.htm moredate.htm js-dates.htm pas-time.htm critdate.htm etc.