批处理之家's Archiver

HAT 发表于 2010-1-12 15:24

DOS系统中获取并分解系统日期时间的批处理

[color=Blue]【方案一】[/color]

To All:

      今天,根据网上的一段代码[1]修改出一个可以在MSDOS下获取并分解系统时钟信息(日期、时间)的批处理。在MSDOS6.22/MSDOS7.10下运行通过。显然易见的是,程序的冗余度过高,存在大量的重复代码,所以它需要简化,也必然可以简化。本来是要自己修改的,只是最近还在忙其他的题目,无法分心;同时又感觉论坛讨论纯DOS批处理的主题日趋萎缩,所以将它贴上来,大家讨论一下,看有什么好的简化和改进方案,并对其中涉及到的DOS下的词法分析算法进行讨论和优化。当然,网上的此类代码应该还有许多,大家也可以提供更多的资源链接或者自己的想法。

[1] %time%...DOS vs. Win98
[url]http://groups.google.com/group/alt.msdos.batch/browse_thread/thread/faa7a7961815c004/7c979271a52e36e7[/url][code]@echo off
if "%1"=="[(" goto parse
echo.|date|find "Current" > _getdate.bat
echo set _dw=%%3> current.bat
echo set _date=%%4>> current.bat
call _getdate.bat

echo.|date|find "Enter" > _getdfmt.bat
echo set _dfmt=%%3> enter.bat
call _getdfmt.bat

echo.|time|find "Current" > _gettime.bat
echo set _time=%%3> current.bat
call _gettime.bat

echo _|choice /c%_dfmt%%_date%_%_time%_ @%0 > _parse.bat
%comspec% /e:2048 /c _parse.bat
goto end

:parse
set _dt1=_%2%3
set _dt2=_%5%6
set _dt3=_%8%9
for %%t in ([,Y,Y,-,M,M,-,D,D,]) do shift

set _vdt=
:loop1
shift
set _tmp=%_vdt%
for %%t in (1,2,3,4,5,6,7,8,9,0) do if "%1"=="%%t" set _vdt=%_vdt%%1
if not "%_vdt%"=="%_tmp%" goto loop1
set %_dt1%=%_vdt%

set _vdt=
:loop2
shift
set _tmp=%_vdt%
for %%t in (1,2,3,4,5,6,7,8,9,0) do if "%1"=="%%t" set _vdt=%_vdt%%1
if not "%_vdt%"=="%_tmp%" goto loop2
set %_dt2%=%_vdt%

set _vdt=
:loop3
shift
set _tmp=%_vdt%
for %%t in (1,2,3,4,5,6,7,8,9,0) do if "%1"=="%%t" set _vdt=%_vdt%%1
if not "%_vdt%"=="%_tmp%" goto loop3
set %_dt3%=%_vdt%

set _vdt=
:loop4
shift
set _tmp=%_vdt%
for %%t in (1,2,3,4,5,6,7,8,9,0) do if "%1"=="%%t" set _vdt=%_vdt%%1
if not "%_vdt%"=="%_tmp%" goto loop4
set _HH=%_vdt%

set _vdt=
:loop5
shift
set _tmp=%_vdt%
for %%t in (1,2,3,4,5,6,7,8,9,0) do if "%1"=="%%t" set _vdt=%_vdt%%1
if not "%_vdt%"=="%_tmp%" goto loop5
set _MN=%_vdt%

set _vdt=
:loop6
shift
set _tmp=%_vdt%
for %%t in (1,2,3,4,5,6,7,8,9,0) do if "%1"=="%%t" set _vdt=%_vdt%%1
if not "%_vdt%"=="%_tmp%" goto loop6
set _SS=%_vdt%

set _vdt=
:loop7
shift
set _tmp=%_vdt%
for %%t in (1,2,3,4,5,6,7,8,9,0) do if "%1"=="%%t" set _vdt=%_vdt%%1
if not "%_vdt%"=="%_tmp%" goto loop7
set _CS=%_vdt%

echo DTN:%_YY% %_MM% %_DD% %_HH% %_MN% %_SS% %_CS%
:end[/code][color=Blue]【方案二】[/color]

我来贴一个,效率没有提高.减少了一些代码[code]@echo off
if "%1"=="[(" goto parse
echo.|date|find "Current" > _getdate.bat
echo set _dw=%%3> current.bat
echo set _date=%%4>> current.bat
call _getdate.bat

echo.|date|find "Enter" > _getdfmt.bat
echo set _dfmt=%%3> enter.bat
call _getdfmt.bat

echo.|time|find "Current" > _gettime.bat
echo set _time=%%3> current.bat
call _gettime.bat

echo _|choice /c%_dfmt%%_date%_%_time%_ @%0 > _parse.bat
%comspec% /e:2048 /c _parse.bat
goto end

:loop
set _vdt=

:loop1
shift
set _tmp=%_vdt%
for %%t in (1,2,3,4,5,6,7,8,9,0) do if "%1"=="%%t" set _vdt=%_vdt%%1
if not "%_vdt%"=="%_tmp%" goto loop1
set %_set%=%_vdt%
goto %next%


:parse
set _dt1=_%2%3
set _dt2=_%5%6
set _dt3=_%8%9
set fgf=%4
for %%t in ([,Y,Y,-,M,M,-,D,D,]) do shift

:dt1
set _set=%_dt1%
set next=dt2
goto loop

:dt2
set _set=%_dt2%
set next=dt3
goto loop

:dt3
set _set=%_dt3%
set next=hh
goto loop

:hh
set _set=_hh
set next=MN
goto loop

:mn
set _set=_mn
set next=ss
goto loop

:ss
set _set=_ss
set next=cs
goto loop

:cs
set _set=_cs
set next=echo1
goto loop

:echo1
echo DTN:%_YY% %_MM% %_DD% %_HH% %_MN% %_SS% %_CS%
:end[/code][color=Blue]【方案三】[/color]

      今天有闲可以回顾修改这个程序了,解决这个代码的冗余性问题,其实最主要的就是如何实现一系列变量连续性的取值和赋值。本来打算使用for对变量进行循环探测,遇到未赋值的变量即跳出赋值,但是此算法实现起来较为复杂,而且对最初变量的重复探测问题也是代码冗余性的一种表现。现在,看到兄的代码,联想起可以使用数据结构中的链表结构,而它正可通过命令行参数来实现,所以写下了这个修改版本。

      这个版本,同时解决了变量没有被“传出”的问题,也增加了退出前清扫现场的语句。但是这个程序还存在许多缺陷,最致命的是,程序中的词法分析核心 choice 无法正确分析以/作为分隔符的字符串,而诸如国家代码为001(United States)的日期信息恰是以/作为分隔符,而代码中的所有语句都是以不依赖于“国家或区域代码”的通用性为设计目标,而这个缺陷完全架空了其他语句所作的努力。

      另外,它还无法处理时间信息中的12小时制时间。[code]@echo off
if "%1"==":" goto %2
echo.|date|find "Current" > _getdate.bat
echo set _dw=%%3> current.bat
echo set _date=%%4>> current.bat
call _getdate.bat

echo.|date|find "Enter" > _getdfmt.bat
echo set _dfmt=%%3> enter.bat
call _getdfmt.bat

echo.|time|find "Current" > _gettime.bat
echo set _time=%%3> current.bat
call _gettime.bat

echo _|choice /c"%_dfmt%%_date%_%_time%" @%0 : parse > _parse.bat
%comspec% /e:2048 /c _parse.bat
call _setdt.bat
for %%e in (_date _dfmt _time) do set %%e=
for %%f in (_*.bat enter.bat* current.bat*) do del %%f
echo DTN:%_YY%-%_MM%-%_DD% %_HH%:%_MN%:%_SS%.%_CS%
goto end

:parse
set _me=%0
shift
shift
set _tok=%2%3 %5%6 %8%9 HH MN SS CS
for %%t in ([,Y,Y,-,M,M,-,D,D,]) do shift

:NextToken
set _vdt=
:GetToken
shift
set _tmp=%_vdt%
for %%t in (1,2,3,4,5,6,7,8,9,0) do if "%1"=="%%t" set _vdt=%_vdt%%1
if not "%_vdt%"=="%_tmp%" goto GetToken
if "%1"=="" goto end
call %_me% : SetToken %_tok%
goto NextToken

:SetToken
set _tok=%4 %5 %6 %7 %8 %9
echo set _%3=%_vdt%>> _setdt.bat
goto end

:end[/code][color=Blue]【方案四】[/color]

      在[1]找到了解决choice对/的兼容性问题,可以支持001的时钟信息了;而prompt的选择不仅简化了代码,同时还使时间保持为24小时制。对帖中代码进行了简单的修改得到以下代码,未进行日期变量名的匹配。

[1] alt.msdos.batch > Documentation of CHOICE streaming parser
[url]http://groups.google.com/group/alt.msdos.batch/msg/5e1d0f360f172118[/url][code]@echo off
if "%1"==":" goto %2 (subroutine handler at %2)

echo.exit|%comspec%/e:4096/k prompt set v=$d $t$_:>_tm.bat
call _tm.bat
echo.%v%&|%comspec%/e:4096/c %0 : Parse>_tm.bat
for %%p in (_tm.bat* _tmset.bat*) do call %%p

echo.Note that d1 d2 and d3 are in your system date order
echo.DTN:d1=%d1% d2=%d2% d3=%d3% hh=%hh% mm=%mm% ss=%ss% cs=%cs%.

for %%e in (v d1 d2 d3 hh mm ss cs) do set %%e=
for %%f in (_tm.bat* _tm.tmp* _tmset.bat*) do del %%f
goto end

:Parse (usage: call %0 : Parse +input redirect date/time)
set me=%0
set tok=d1 d2 d3 hh mm ss cs
set d1=
:NextToken
for %%v in (v1 v) do set %%v=
choice /c&0123456789 /n>_tm.tmp
for %%n in (0 1 2 3 4 5 6 7 8 9) do if errorlevel 77%%n set v=%%n
if not errorlevel 2 %me% : SetToken %tok%
find ""<_tm.tmp>nul
if not errorlevel 1 if not "%d1%"=="" call %me% : SetToken %tok%
set d1=%d1%%v%
goto NextToken

:SetToken
set tok=%4 %5 %6 %7 %8 %9
echo set %3=%d1%>> _tmset.bat
set d1=
goto end

:end (end-of-file)[/code]转自:[url]http://www.cn-dos.net/forum/viewthread.php?tid=19756[/url]

lxzzr 发表于 2010-1-12 16:48

这个倒是不错,最近在研究启动盘,这个有点用处。HAT辛苦了,^_^

页: [1]

Powered by Discuz! Archiver 7.2  © 2001-2009 Comsenz Inc.