批处理之家's Archiver

Batcher 发表于 2009-3-12 19:48

批处理编程的异类:游戏、数学、时钟(延时)等

[quote]原文地址:[url]http://www.cn-dos.net/forum/viewthread.php?tid=8905[/url][/quote]
[quote]  学过DOS的人,或多或少都听过批处理。它小巧轻便,而且是系统级源代码解释,不用编译和连接而直接运行,曾经吸引了不少DOS的爱好者和编程者。众多人士的参与,无数奇思妙想的涌现,构成了批处理编程中的"异类"。它们并不是单纯为解决某些"成批处理的问题"而设计,而是真正将批处理做为一种语言,来体现它无限的创造力。

  下面是我搜集的一些批处理程序,他们或许并非是技术上的精品,但绝对是构思上的突破。我会不断的更新和完善这个主题,也希望大家同我一道努力,完成这个"异类"收藏。

  索引
======
1楼:本主题
2楼:游戏(Game):
    1、三子棋(TicTacToe)
3楼:数学(Math):
    1、随机数(Random)生成、随机矩阵生成,共2个
    2、2K/XP表达式计算
4楼:时钟(Clock):
    1、暂停(Pause)、延迟(Delay)、等待(Wait)或睡眠(Sleep),共5个

  声明
======

  1,为保持本帖的结构,仅为赞美和支持的同志请尽量不要在此回帖;
  2,对本帖某些程序有任何疑问和建议的朋友,请访问下面的主题
    [url]http://www.cn-dos.net/forum/viewthread.php?tid=12388[/url]
  3,有意推荐程序的朋友,请注意以下的选择标准:
   (1),程序实现的功能具有一定的通用性和创新性;
   (2),程序不依赖于任何特殊或定制的本地环境(比如程序的路径);
   (3),程序未使用任何第三方工具或者软件;
   (4),程序最好适用于所有的 Windows 和 DOS 平台,如有特殊要求请注明;
  4,本主题采用淘汰制,即完成相同或相似功能的代码将采用编写质量最优秀者,无法区分优劣时,或者同时在一帖内列出,或者融合二者的优秀之处改遍成新的代码;被淘汰者或者单独发表主题列出,或者列出可引用的链接。

 更新记录
======
2004.06.20: 发出"[讨论]批处理编程的异类"主题,并与"[分享]批处理编程的异类"互相链接
2005.07.16: 更新7楼的 prompt 变量获取程序
2005.08.18: 修改3楼为数学(Math)主题,并增加随机数和随机阵列生成程序
2005.08.19: 修改4楼为时钟(Clock)主题,增加“暂停”程序5个
2005.09.11: 3楼数学(Math)主题增加“2K/XP表达式计算批处理”[/quote]

Batcher 发表于 2009-3-12 19:49

批处理编程异类——游戏(Game)

[quote]程序:TicTacToe(三子棋、井字棋、打井、提担挑)
作者:M0eRtH
原文:TicTacToe (a.k.a. Noughts''''''''n''''''''Crosses) BATch version 1.1
   [url]http://users.volja.net/sarcasmus/M0/tictactoe/tttbat.html[/url]
下载:[url]http://users.volja.net/sarcasmus/M0/tictactoe/tictactoebat.zip[/url]
环境:1,程序应该在英文的命令行环境下运行;
   2,程序使用了大量的环境变量,应该修改程序运行时环境空间的大小;
   3,程序在WIN2K/XP运行需要CHOICE的支持;
备注:
  这是我见到第一个用批处理编写的真正具有游戏性的程序,虽然程序并不提供AI对手,但是其优美的结构仍然值得称道。正文中出现的乱码字是因为作者使用了ANSI的表格字符。
同类:
  福尔摩斯问答:[url]http://dos.e-stone.cn/dosbbs/dispbbs.asp?boardID=6&ID=8272[/url]
  ZD庄园的迷魂阵:[url]http://zdland.xiloo.com/planets/psa.html[/url]

正文:[/quote][code]@echo off
mode con cols=40 lines=25
cls
echo.
echo   TicTacToe (a.k.a. Noughts''''''''n''''''''Crosses)
echo            BATch Version 1.1
echo             (c)2002 M0eRtH
echo.
echo      Updates and more free stuff at
echo          http://m0erth.cjb.net
echo.
echo The keys:
echo.
echo          - X -            - O -
echo.
echo        7 ?8 ?9        Q ?W ?E
echo       哪呐哪呐哪?     哪呐哪呐哪?
echo        4 ?5 ?6        A ?S ?D
echo       哪呐哪呐哪?     哪呐哪呐哪?
echo        1 ?2 ?3       Z/Y?X ?C
echo.
echo   Ctrl+R = Reset game   Ctrl+Q = Quit
echo.
echo Note:
echo   X, make sure the NumLock is ON. ;-)
echo.
pause

set a1=-
set a2=-
set a3=-
set b1=-
set b2=-
set b3=-
set c1=-
set c2=-
set c3=-
set turn=X
set win=-
set result=-
set scoreX=-
set scoreO=-
set games=-
set maxgames=-++++++++++++++++++++

:draw
cls
echo.
echo   TicTacToe (a.k.a. Noughts''''''''n''''''''Crosses)
echo            BATch Version 1.1
echo             (c)2002 M0eRtH
echo.
echo Score
echo  X:%scoreX%
echo  O:%scoreO%
echo.
echo                %a1% ?%a2% ?%a3%
echo               哪呐哪呐哪?
echo                %b1% ?%b2% ?%b3%
echo               哪呐哪呐哪?
echo                %c1% ?%c2% ?%c3%
echo.

:chkWin
set result=%a1%%a2%%a3%
if %result%==XXX goto XWon
if %result%==OOO goto OWon
set result=%b1%%b2%%b3%
if %result%==XXX goto XWon
if %result%==OOO goto OWon
set result=%c1%%c2%%c3%
if %result%==XXX goto XWon
if %result%==OOO goto OWon
set result=%a1%%b1%%c1%
if %result%==XXX goto XWon
if %result%==OOO goto OWon
set result=%a2%%b2%%c2%
if %result%==XXX goto XWon
if %result%==OOO goto OWon
set result=%a3%%b3%%c3%
if %result%==XXX goto XWon
if %result%==OOO goto OWon
set result=%a1%%b2%%c3%
if %result%==XXX goto XWon
if %result%==OOO goto OWon
set result=%c1%%b2%%a3%
if %result%==XXX goto XWon
if %result%==OOO goto OWon

:getKeys
echo                %turn%''''''''s turn
choice /c:789456123qweasdzyxc /n :
if errorlevel 21 goto reset
if errorlevel 20 goto quit
if errorlevel 19 goto setOC3
if errorlevel 18 goto setOC2
if errorlevel 17 goto setOC1
if errorlevel 16 goto setOC1
if errorlevel 15 goto setOB3
if errorlevel 14 goto setOB2
if errorlevel 13 goto setOB1
if errorlevel 12 goto setOA3
if errorlevel 11 goto setOA2
if errorlevel 10 goto setOA1
if errorlevel 9 goto setXC3
if errorlevel 8 goto setXC2
if errorlevel 7 goto setXC1
if errorlevel 6 goto setXB3
if errorlevel 5 goto setXB2
if errorlevel 4 goto setXB1
if errorlevel 3 goto setXA3
if errorlevel 2 goto setXA2
if errorlevel 1 goto setXA1
goto draw

:reset
set a1=-
set a2=-
set a3=-
set b1=-
set b2=-
set b3=-
set c1=-
set c2=-
set c3=-
goto draw

:setXA1
if not %a1%==- goto draw
if %turn%==O goto draw
set a1=X
goto changeTurn
:setXA2
if not %a2%==- goto draw
if %turn%==O goto draw
set a2=X
goto changeTurn
:setXA3
if not %a3%==- goto draw
if %turn%==O goto draw
set a3=X
goto changeTurn

:setXB1
if not %b1%==- goto draw
if %turn%==O goto draw
set b1=X
goto changeTurn
:setXB2
if not %b2%==- goto draw
if %turn%==O goto draw
set b2=X
goto changeTurn
:setXB3
if not %b3%==- goto draw
if %turn%==O goto draw
set b3=X
goto changeTurn

:setXC1
if not %c1%==- goto draw
if %turn%==O goto draw
set c1=X
goto changeTurn
:setXC2
if not %c2%==- goto draw
if %turn%==O goto draw
set c2=X
goto changeTurn
:setXC3
if not %c3%==- goto draw
if %turn%==O goto draw
set c3=X
goto changeTurn


:setOA1
if not %a1%==- goto draw
if %turn%==X goto draw
set a1=O
goto changeTurn
:setOA2
if not %a2%==- goto draw
if %turn%==X goto draw
set a2=O
goto changeTurn
:setOA3
if not %a3%==- goto draw
if %turn%==X goto draw
set a3=O
goto changeTurn

:setOB1
if not %b1%==- goto draw
if %turn%==X goto draw
set b1=O
goto changeTurn
:setOB2
if not %b2%==- goto draw
if %turn%==X goto draw
set b2=O
goto changeTurn
:setOB3
if not %b3%==- goto draw
if %turn%==X goto draw
set b3=O
goto changeTurn

:setOC1
if not %c1%==- goto draw
if %turn%==X goto draw
set c1=O
goto changeTurn
:setOC2
if not %c2%==- goto draw
if %turn%==X goto draw
set c2=O
goto changeTurn
:setOC3
if not %c3%==- goto draw
if %turn%==X goto draw
set c3=O
goto changeTurn


:changeTurn
if %turn%==X goto setTurnO
set turn=X
goto draw
:setTurnO
set turn=O
goto draw


:XWon
echo.
echo - X won! -
pause
set scoreX=%scoreX%+
set games=%games%+
if %games%==%maxgames% goto quit
goto reset

:OWon
echo.
echo - O won! -
pause
set scoreO=%scoreO%+
set games=%games%+
if %games%==%maxgames% goto quit
goto reset

:quit
mode con cols=80 lines=25
echo.
echo   TicTacToe (a.k.a. Noughts''''''''n''''''''Crosses)
echo            BATch Version 1.1
echo             (c)2002 M0eRtH
echo.
echo      Updates and more free stuff at
echo          http://m0erth.cjb.net
echo.
echo.
echo  X scored: %scoreX%
echo  O scored: %scoreO%
echo.
set a1=
set a2=
set a3=
set b1=
set b2=
set b3=
set c1=
set c2=
set c3=
set turn=
set win=
set result=
set scoreX=
set scoreO=
set games=
set maxgames=
pause[/code]

Batcher 发表于 2009-3-12 19:51

批处理编程的异类——数学(Math)

1、数学——随机

    1)随机数发生器:运行后可产生一个00~99的随机整数,并保存于环境变量 %_random% 中。

      如果要产生更复杂的随机数,也可以利用这个程序进行修改来得到,比如两次产生的随机数组合,即可得到0000~9999的随机数;在随机数前加上小数点,即可得到0.00-0.99的随机小数;将 prompt _$T$_:: 中的第一个下划线去除,可以得到0~9的随机数;用debug可以去除某些随机数的前缀0。根据以上方法,即可组合得到任意形式的随机数了。[code]:: GenARnd.Bat - Generate a random integer (00~99)
:: Will Sort - 12:06 2005-8-18 - V2 - MSDOS7.10/CMD@WinXP/MSDOS6.22
@echo off
echo exit|%comspec% /k prompt _$T$_::> _GenARnd.bat
echo e 100 "set _random=">_GenARnd.asd
for %%s in (w q) do echo %%s>>_GenARnd.asd
debug _GenARnd.bat < _GenARnd.asd > nul
call _GenARnd.bat
for %%f in (_GenARnd*.*) do del %%f
if not "%_random%"=="" if not [%1]==[@] echo Random number : %_random%
:End[/code]2)随机阵列发生器:运行后将生成一张随机概率分布矩阵图。

      此图24行63列,由字符“-”(假)和“o”(真)组成,从上到下每三行为一组,呈现出o的分布概率依次降低。其中矩阵中的左图为真假序列分布,右图为真假数目对比。[code]:: RndArray.bat - Generate a serial of random numbers(- or o)
:: Will Sort - 2004/11/24
@echo off
if "%1"==":" goto %2

set no=
set num=0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9

:loop
set no=I%no%
set line=
set -=
set o=
for %%c in (%num%) do call %0 : random %no%
echo %line% : %-%%o%
set line=
set -=
set o=
for %%c in (%num%) do call %0 : random %no%
echo %line% : %-%%o%
set line=
set -=
set o=
for %%c in (%num%) do call %0 : random %no%
echo %line% : %-%%o%
if not [%no%]==[IIIIIIII] goto loop

:quit
del _Random.tmp
for %%e in (- o no num line) do set %%e=
goto end

:random
echo. | time > _Random.tmp
::echo exit | %comspec% /k prompt $T>_Random.tmp
goto %3

find ".0"<_Random.tmp>nul
if not errorlevel 1 goto true
:I
find ".3"<_Random.tmp>nul
if not errorlevel 1 goto true
:II
find ".6"<_Random.tmp>nul
if not errorlevel 1 goto true
:III
find ".9"<_Random.tmp>nul
if not errorlevel 1 goto true
:IIII
find ".2"<_Random.tmp>nul
if not errorlevel 1 goto true
:IIIII
find ".5"<_Random.tmp>nul
if not errorlevel 1 goto true
:IIIIII
find ".8"<_Random.tmp>nul
if not errorlevel 1 goto true
:IIIIIII
find ".1"<_Random.tmp>nul
if not errorlevel 1 goto true
:IIIIIIII
find ".4"<_Random.tmp>nul
if not errorlevel 1 goto true
find ".7"<_Random.tmp>nul
if not errorlevel 1 goto true
::del _Random.tmp

:false
set line=%line%-
set -=-%-%
goto end

:true
set line=%line%o
set o=o%o%
goto end

:end[/code]2、数学——表达式

    1)2K/XP表达式计算批处理,由 Brglng 原创并维护。转帖时精简了程序中的文档部分,可以通过以下链接查阅完整的代码和文档。

{16534}2K/XP表达式计算批处理
[url]http://www.cn-dos.net/forum/viewthread.php?tid=16534[/url][code]:: Eval.cmd - 2K/XP表达式计算批处理
:: Brglng - 2005.8.31
@echo off
setlocal
if /i "%1"=="/A" if "%2"=="" echo 必须指定文件路径!& goto end
if /i not "%1"=="/A" if /i not "%1"=="/N" if not "%1"=="" goto EvalArgument

echo 请输入表达式(输入"Q"退出) & goto inputExpr

:EvalArgument
set /a EvalExpr="%1"
if "%EvalExpr%"=="" echo 表达式输入错误! & goto end
echo %1=%EvalExpr%
if "%2"=="" goto end
echo %1=%EvalExpr% >>"%2"
echo 算式和计算结果已存入"%2"!
goto end

:inputExpr
set Expr=
set EvalExpr=
set /p Expr=^>
if "%Expr%"=="" goto inputExpr
if /i "%Expr%"=="Q" goto end
set /a EvalExpr="%Expr%"
if "%EvalExpr%"=="" echo 输入错误! & goto inputExpr
echo 计算结果为:%EvalExpr%
if /i "%1"=="/A" goto autosave
if /i "%1"=="/N" goto inputExpr

:if_save
set SaveEval=
set /p SaveEval=是否将表达式和计算结果存入文件[Y/N]?
if /i "%SaveEval%"=="Y" goto save
if /i "%SaveEval%"=="N" goto inputExpr
goto if_save

:save
set /p SaveEvalPath=请输入文件路径及文件名(如C:\Windows\file.txt。若只输入文件名则自动存入当前文件夹,留空则为上一次输入的路径):
echo "%Expr%"=%EvalExpr% >>"%SaveEvalPath%"
echo 表达式以及计算结果已存入"%SaveEvalPath%"!
goto inputExpr

:autosave
echo "%Expr%"=%EvalExpr% >>"%2"
goto inputExpr

:end[/code]

Batcher 发表于 2009-3-12 19:53

批处理编程的异类——时钟(Clock)

1、暂停(Pause)、延迟(Delay)、等待(Wait)、睡眠(Sleep)

0) Win7 / Win10 等系统自带的 timeout 命令[code]@echo off
echo %date% %time%
timeout /nobreak /t 3 >nul
echo %date% %time%
pause[/code]1)for+set+if,时间精度为0.01秒,适用平台为WinNT/2K/XP/2003。

  利用for解析变量%time%并存为两个时间点%start%和%now%,再利用set /a计算两个时间点的时间差,最后用if判断时间差是否达到设定的暂停时间。[code]@echo off
setlocal enableextensions
echo %time%
call :ProcDelay 200
echo %time%
goto :EOF

:ProcDelay delayMSec_
setlocal enableextensions
for /f "tokens=1-4 delims=:. " %%h in ("%time%") do set start_=%%h%%i%%j%%k
  :_procwaitloop
  for /f "tokens=1-4 delims=:. " %%h in ("%time%") do set now_=%%h%%i%%j%%k
  set /a diff_=%now_%-%start_%
if %diff_% LSS %1 goto _procwaitloop
endlocal & goto :EOF[/code]2)VBS脚本中的sleep函数,时间精度为0.001秒,使用平台为Win9x/WinNT系列。

   动态创建一个调用sleep()函数的VBS脚本,然后用Windows脚本宿主的命令行版本cscript调用它。[code]@echo off & setlocal enableextensions enabledelayedexpansion
echo WScript.Sleep 2000 > %temp%\tmp$$$.vbs
echo %time%
cscript //nologo %temp%\tmp$$$.vbs
echo %time%
for %%f in (%temp%\tmp$$$.vbs) do if exist %%f del %%f
endlocal & goto :EOF[/code]3)ping的消息发送间隔,时间精度为1秒,使用平台为Win9x/WinNT系列。

   需要你的Windows系统中正常安装了网卡的TCP/IP协议,有两种方案,方案二不建议采用,见主题 [讨论]批处理编程的异类 12、13楼:

(1) 利用ping两次发送消息之间的间隔时间。ping在发送多个消息时,在得到上一次消息的回应后,它会再等待1秒的时间才发送下一次消息,而这个回应时间因机型、系统和网络配置而不同,其中IP地址尤其关键,只有本地的回送地址 127.0.0.1 因为是立即回应所以才是相对恒定的,其他地址大多会产生显著的差异。此种方法要等待更大的时间,是指定发送的消息数为等待的时间加一,因为第一个消息是不等待的,IP 地址必须是 127.0.0.1 。其等待2秒的程序如下:

::根据植树原则,n后的数字为暂停秒数加一
ping -n 3 127.0.0.1>nul
(2) 利用ping的最大等待时间。ping向0.0.0.1发送一次“网际消息控制协议 (ICMP)”回响请求消息,因为 0.0.0.1 是个无法响应 ping 请求的IP地址,所以默认情况下,ping会等待一定的最大响应时间——4秒后便停止等待,而-w可以改变这个每发送一次消息所等待的最大响应时间。而如果将 IP 地址改为立即响应的127.0.0.1或其他响应时间很短的IP ,那 -w 便失去了作用,因为每次发送等待时间都不会达到最大的响应时间,此时延时会缩短。此种方法要等待更大的时间,可以直接使用 -w 指定,但是需要减去一定的发送延时,这个延时需要自己事先测定一下,否则会有不到1秒的误差;而且 IP 地址必须是不响应请求的地址。其等待2秒的程序如下:[code]ping -n 2 -w 500 0.0.0.1>nul[/code]4)choice的缺省选择等待功能,时间精度为1秒,适用平台为MS-DOS/Win9x/WinNT系列。

  /t:y,2 将缺省选择字符设置为y,等待时间是2秒,/n禁止[y,n]提示出现在命令行上,“rem|”的作用是禁止choice从键盘接受按键,从而暂停不会因为按下y或者其它键而被意外终止。[code]rem|choice /t:y,2 /n >nul[/code]5)ASCII汇编代码,延迟精度为0.001秒,适用平台为MS-DOS/Win9x/WinNT。

  德国人Herbert Kleebauer给出了一个通用方案,通过ASCII汇编器间接产生一个sleep.exe程序来实现延迟,这个程序分为DOS和Win两个模块,分别调用DOS系统中断服务和WindowsAPI。[code]:: Sleep.bat - Sleep/Delay/Wait n seconds
:: Herbert Kleebauer(Germany) - 2005/05/29
:: Modified by Will Sort - 2005/06/02, 07-25
@echo off
echo q | debug>nul
echo Bj@jzh`0X-`/PPPPPPa(DE(DM(DO(Dh(Ls(Lu(LX(LeZRR]EEEUYRX2Dx=>sleep.com
echo 0DxFP,0Xx.t0P,=XtGsB4o@$?PIyU WwX0GwUY Wv;ovBX2Gv0ExGIuht6>>sleep.com
echo T}{z~~@GwkBG@OEKcUt`~}@MqqBsy?seHB~_Phxr?@zAB`LrPEyoDt@Cj?>>sleep.com
echo pky_jN@QEKpEt@ij?jySjN@REKpEt@jj?jyGjN@SEKkjtlGuNw?p@pjirz>>sleep.com
echo LFvAURQ?OYLTQ@@?~QCoOL~RDU@?aU?@{QOq?@}IKuNWpe~FpeQFwH?Vkk>>sleep.com
echo _GSqoCvH{OjeOSeIQRmA@KnEFB?p??mcjNne~B?M??QhetLBgBPHexh@e=>>sleep.com
echo EsOgwTLbLK?sFU`?LDOD@@K@xO?SUudA?_FKJ@N?KD@?UA??O}HCQOQ??R>>sleep.com
echo _OQOL?CLA?CEU?_FU?UAQ?UBD?LOC?ORO?UOL?UOD?OOI?UgL?LOR@YUO?>>sleep.com
echo dsmSQswDOR[BQAQ?LUA?_L_oUNUScLOOuLOODUO?UOE@OwH?UOQ?DJTSDM>>sleep.com
echo QTqrK@kcmSULkPcLOOuLOOFUO?hwDTqOsTdbnTQrrDsdFTlnBTm`lThKcT>>sleep.com
echo @dmTkRQSoddTT~?K?OCOQp?o??Gds?wOw?PGAtaCHQvNntQv_w?A?it\EH>>sleep.com
echo {zpQpKGk?Jbs?FqokOH{T?jPvP@IQBDFAN?OHROL?Kj??pd~aN?OHROd?G>>sleep.com
echo Q??PGT~B??OC~?ipO?T?~U?p~cUo0x>>sleep.com
sleep.com>sleep.exe
echo wait %1 seconds:
sleep.exe %1000
del sleep.com
del sleep.exe[/code]

Batcher 发表于 2009-3-12 19:54

[quote]    以下是一个用字符方式画图的程序,原文来自网易广州站DOS版。
    [url]http://fe2.gz.163.com/cgi/readsrh?b=DOS&t=15763&i=15763&al=5&n=0&l=40&back=2&st=0&kw=MAP[/url]

    这个程序的工作方式很简陋,不过已经具备了一些基本的功能,比如画,擦,更换画笔字符,保存和提取字符画,备份和恢复字符画,使字符画可执行化等。程序用到了ANSI.SYS 的支持功能,所以 ECHO 后的方框字符实际上是 ASCII 码为27的符号,可在DOS下的 EDIT 中按 CTRL+P 然后按 ESC 输入此字符。[/quote][quote]@ECHO OFF
:START
IF NOT [%3]==[] GOTO MAP%1
IF [%1]==[/?] GOTO MAPHELP
CHOICE Initizing, Please waiting../C:. /T:.,1 /N
IF [%1]==[R:] GOTO MAPRESTORE
IF [%1]==[E:] GOTO MAPEXTRACT
IF [%1]==[T:] GOTO MAPTRYSAVE

MEM /C|FIND "ANSI">NUL
IF ERRORLEVEL 1 GOTO MAPERR1

IF [%1]==[] GOTO MAPINIT
IF [%2]==[] GOTO MAPERR2
IF [%1]==[C:] IF NOT [%2]==[/LINE] IF NOT [%2]==[/PIPE] SET $F=%2
IF [%1]==[C:] GOTO MAPSTART

IF NOT EXIST %2.MAP GOTO MAPERR5
FIND "" %2.MAP>NUL
IF ERRORLEVEL 1 GOTO MAPERR3
REM FIND "" %2.MAP>NUL
REM IF ERRORLEVEL 1 GOTO MAPERR4

IF [%1]==[V:] GOTO MAPVIEW
IF [%1]==[L:] GOTO MAPLOAD
IF [%1]==[M:] GOTO MAPMAKE
:MAPINIT
CLS
SET $F=?
SET PRE=C
GOTO MAPSTART

:MAPHELP
ECHO.
ECHO Use: Draw a map with painter of characterS, and save to map file.
ECHO Usage: %0 option: parameter
ECHO Option:
ECHO            L       Load map file from the specified location.
ECHO            M       Make map file to selfexec map program.
ECHO            E       Extract selfexec map to map file.
ECHO            T       Try save map file from temporary location.
ECHO            R       Restore the map backuped map file.
ECHO            V       View the map file parameter specifle.
ECHO            C       define painter with Character parameter specifle.
ECHO.
CHOICE View guide of draw:
IF ERRORLEVEL 2 GOTO MAPEND
ECHO.
ECHO All operation control by keyboard keys.
ECHO.
ECHO Key:       Use:
ECHO 1          Left and down ----)
ECHO 2          Down              )
ECHO 3          Right and down    )
ECHO 4          Left              )---Painter Move keys
ECHO 6          Right             )
ECHO 7          Left and up       )
ECHO 8          Up                )
ECHO 9          Right and up------)
ECHO 5          painter
ECHO 0(Ins)     change to draw state
ECHO .(Del)     change to delete state
ECHO.
GOTO MAPEND

:MAPERR1
ECHO.
ECHO Error 1 in %0: Not continue for not load ANSI.
ECHO.
GOTO MAPEND
:MAPERR2
ECHO.
ECHO Error 2 in %0: Not specified second parameter.
ECHO.
GOTO MAPEND
:MAPERR3
ECHO.
ECHO Error 3 in %0: False map file.
ECHO.
GOTO MAPEND
:MAPERR4
ECHO.
ECHO Error 4 in %0: Not accomplish map file.
ECHO.
GOTO MAPEND
:MAPERR5
ECHO.
ECHO Error 5 in %0: Not exist map file.
ECHO.
GOTO MAPEND
:MAPERR6
ECHO.
ECHO Error 6 in %0: Not backup map file.
ECHO.
GOTO MAPEND
:MAPERR7
ECHO.
ECHO Error 7 in %0: Not make selfexec map program.
ECHO.
GOTO MAPEND
:MAPERR8
ECHO.
ECHO Error 8 in %0: Not correctly pattern selfexec map program.
ECHO.
GOTO MAPEND
:MAPERR9
ECHO.
ECHO Error 9 in %0: Can''''t view map file.
ECHO.
GOTO MAPEND
:MAPERR10
ECHO.
ECHO Error 10 in %0: enviroment parameter PRE is null.

::MAPPICK
IF EXIST %2.MAP CHOICE Modify this map file
IF ERRORLEVEL 2 GOTO MAPEND
ECHO @CLS>PAINTER.BAT
ECHO @SET $F=%%1>>PAINTER.BAT
ECHO @%0 START %2 /S>>PAINTER.BAT
ECHO Please choose painter of character with style (PAINTER=char).
PROMPT[/quote]

[[i] 本帖最后由 Batcher 于 2009-3-12 19:55 编辑 [/i]]

Batcher 发表于 2009-3-12 19:56

[quote]    以下是个在命名行提示上画一只字符小狗的程序,原文来自网易北京站的DOS版。
    [url]http://fe4.bj.163.com/cgi/readsrh?b=DOS&t=38819&i=38819&al=5&n=0&l=40&back=2&st=0&kw=DOS%B5%C4%CD%BC%D0%CE[/url]

    程序需ANSI支持。其中存在缺陷,解决的方法请查询原网址。[/quote]
[quote]@PROMPT   {             (\__           }
ECHO 
@PROMPT   {            (\   `\__       }
ECHO 
@PROMPT   {(`, .------.,-'   b  $B    }
ECHO 
@PROMPT   {`-/            ',__,-"`     }
ECHO 
@PROMPT   {  $B   `;      ,  $B          }
ECHO 
@PROMPT   {   \   ;.----/ ,/           }
ECHO 
@PROMPT   {    ) // /  ( ( \           }
ECHO 
@PROMPT   {    \ \`.`\  \ \ \          }
ECHO 
@PROMPT   {     `-` `"   `-`"          }
ECHO 
@PORMPT $P$G[/quote]

Batcher 发表于 2009-3-12 19:57

[quote]以下是一个获取PROMPT系统状态数据的程序,原文来自网易广州站的DOS版。
    [url]http://fe2.gz.163.com/cgi/readsrh?b=DOS&t=25501&i=25609&al=5&n=0&l=40&back=2&st=1&kw=willsort[/url]
   
    运行时,加上存放数据的变量名和prompt对应变量名即可,如:
       getvar curver $v   
    可以提取的系统状态数据有:

      $T   Current time
      $D   Current date
      $P   Current drive and path
      $V   System version number
      $N   Current drive[/quote][code]:: GetVar.bat - Get from 'PROMPT' text to environment variable
:: Will Sort - 10:29 2005-7-16 - CMD@XP/COMMAND@Win98/MSDOS6.22
@echo off
echo exit | %comspec% /k prompt set %1=%2$_:: > _GetVar.bat
call _GetVar.bat
for %%f in (_GetVar*.*) do del %%f[/code]

Batcher 发表于 2009-3-12 19:57

[quote]    以下是一个定时器程序,原文来自网易广州站的DOS版。
    [url]http://fe2.gz.163.com/cgi/readsrh?b=DOS&t=13486&i=15375&al=5&n=40&l=40&back=2&st=1&kw=willsort[/url]

    程序中的 CURRENT.BAT 可以更换为自己需要执行的内容。FIND 一句的时间可以根据自己的需要设定,也可用参数%1代替。还可以改为 TIME 改为 DATE 以完成的定期执行的功能。[/quote][code]::CURRENT.BAT
@echo off
echo Haha, it's the time to go!!

::TIMER.BAT
@echo off
:start
echo.|time|find "10:30:00">run.bat
choice /c:y /t:y,59 /n>nul
call run.bat
goto start[/code]

Batcher 发表于 2009-3-12 19:58

批处理编程的异类——网络(Network)

[quote]1、共享
    1)删除win2k/xp系统默认共享

      原文来自《批处理简明教程》
      [url]http://www.realhack.org/article/articleshow.asp?ArticleID=954[/url][/quote][code]@echo preparing to delete all the default shares. when ready press any key.
@pause
@echo off
:Rem check parameters if null show usage.
if {%1}=={} goto :Usage
:Rem code start.
echo.
echo ------------------------------------------------------
echo.
echo Now deleting all the default shares.
echo.
net share %1$ /delete
net share %2$ /delete
net share %3$ /delete
net share %4$ /delete
net share %5$ /delete
net share %6$ /delete
net share %7$ /delete
net share %8$ /delete
net share %9$ /delete
net stop Server
net start Server
echo.
echo All the shares have been deleteed
echo.
echo ------------------------------------------------------
echo.
echo Now modify the registry to change the system default properties.
echo.
echo Now creating the registry file
echo Windows Registry Editor Version 5.00> c:\delshare.reg
echo [HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\lanmanserver\parameters]>> c:\delshare.reg
echo "AutoShareWks"=dword:00000000>> c:\delshare.reg
echo "AutoShareServer"=dword:00000000>> c:\delshare.reg
echo Nowing using the registry file to chang the system default properties.
regedit /s c:\delshare.reg
echo Deleting the temprotarily files.
del c:\delshare.reg
goto :END

:Usage
echo.
echo ------------------------------------------------------
echo.
echo ☆ A example for batch file ☆
echo ☆ [Use batch file to change the sysytem share properties.] ☆
echo.
echo Author:Ex4rch
echo Mail:Ex4rch@hotmail.com QQ:1672602
echo.
echo Error:Not enough parameters
echo.
echo ☆ Please enter the share disk you wanna delete ☆
echo.
echo For instance,to delete the default shares:
echo delshare c d e ipc admin print
echo.
echo If the disklable is not as C: D: E: ,Please chang it youself.
echo.
echo example:
echo If locak disklable are C: D: E: X: Y: Z: ,you should chang the command into :
echo delshare c d e x y z ipc admin print
echo.
echo *** you can delete nine shares once in a useing ***
echo.
echo ------------------------------------------------------
goto :EOF
:END
echo.
echo ------------------------------------------------------
echo.
echo OK,delshare.bat has deleted all the share you assigned.
echo.Any questions ,feel free to mail to Ex4rch@hotmail.com.
echo
echo.
echo ------------------------------------------------------
echo.
:EOF
echo end of the batch file [/code][quote]2)PING某个区段内的所有IP的程序

      原文来自网易北京站的DOS版。
      [url]http://fe4.bj.163.com/cgi/readsrh?b=DOS&t=94897&i=96159&al=5&n=0&l=40&back=2&st=1&kw=willsort[/url]

      在命令行输入 a.bat 0 1 2 3 4 5 6 7 8 9 ,就能显示某区段的所有 IP 地址 192.168.1.0 - 192.168.1.99 ,将其中的 echo 改成 ping 就行,其中的 choice 和 if 语句是等待用户判断是否继续,可以删除,但这样中断程序比较麻烦。[/quote][code]@echo off
:redo
if "%1" == "" goto end
for %%a in (0,1,2,3,4,5,6,7,8,9) do echo 192.168.1.%1%%a
choice continue
if errorlevel 2 goto end
shift
goto redo
:end [/code]

Batcher 发表于 2009-3-12 19:59

[quote]    以下是一个修改文件的更新日期的程序,原文来自网易北京站的DOS版。   
    [url]http://fe4.bj.163.com/cgi/readsrh?b=DOS&t=85861&i=85878&al=5&n=0&l=40&back=2&st=1&kw=willsort[/url]

    原文代码中有若干错误,下为修正版。[/quote][code]@echo off
rem 保存系统时间
echo.|date>sysdate.bat
echo.|time>systime.bat
rem 修改系统时间为指定时间,日期为参数一,时间为参数二,文件名为参数三
date %1
time %2
rem 修改文件的时间
copy %3+,,>nul
rem 恢复系统时间
echo date %%4>current.bat
call sysdate
echo time %%3>current.bat
call systime
rem 程序执行完毕
echo Success.
del systime.bat
del sysdate.bat
del current.bat
echo on[/code]

Batcher 发表于 2009-3-12 19:59

[quote]    以下是一个文件遍历程序,原文来自网易广州站的DOS版。
    [url]http://fe2.gz.163.com/cgi/readsrh?b=DOS&t=21718&i=21882&al=5&n=0&l=40&back=2&st=1&kw=willsort[/url]

    原文中用到了两个外部程序,VARSET 和 LMOD,可能在本站或者新DOS时代就有下载。[/quote][code]@echo off
rem ///////////////////搜索所有目录下的所有txt文件///////////////////

rem 找当前目录(根目录)的*.txt
for %%a in (*.txt) do type %%a

rem 以下为找当前目录(根目录)的所有子目录下的所有*.txt

set i=1
set j=1
set filenum=0
set dirnum=0

varset root=FULL of . >temp.bat
call temp.bat

varset rootdir=string(3) %root% >temp.bat
call temp.bat

dir /ad /b /s >temp.txt

varset dirnum=lines temp.txt >temp.bat
call temp.bat

:loop1

type %root%\temp.txt | %root%\LMOD /L%i% set curdir=[] >%root%\temp.bat
call %root%\temp.bat

varset curdir=string(3) %curdir% >%root%\temp.bat
call %root%\temp.bat

cd %curdir%
echo %curdir%
set j=1
:loop2
     %root%\varset filenum=files *.txt >%root%\temp.bat
     call %root%\temp.bat
     if %filenum%==0 goto end2

     dir *.txt /b | %root%\LMOD /L%j% set curfile=[] >%root%\temp.bat
     call %root%\temp.bat

     type %curfile%

     if %j%==%filenum% goto end2

     %root%\varset j=%j%+1 >%root%\temp.bat
     call %root%\temp.bat
     goto loop2
:end2

cd %rootdir%

if %i%==%dirnum% goto end1

%root%\varset i=%i%+1 >%root%\temp.bat
call %root%\temp.bat

goto loop1

:end1
del %root%\temp.bat
del %root%\temp.txt
set i=
set j=
set filenum=
set dirnum=
set root=
set rootdir=
set curdir=
set curfile=[/code]

Batcher 发表于 2009-3-12 20:00

[quote]    以下是一个接收键盘输入字符串的程序,原文来自本站教学室。
    [url]http://model.chinajewelry.net/dos/dosbbs/dispbbs.asp?boardID=12&replyID=34149&ID=5283&skin=1[/url]

    此文的关键技巧在于 FC 和 DEBUG 的巧妙结合使用。%TEMP%.\filename.ext 的用法也很有技巧性。%%?的用法也很新鲜。[/quote][code]:: Get user input - includes error check
@echo off
echo e102'set %%1='> %temp%.\t1.dat
for %%? in (w q) do echo %%?>> %temp%.\t1.dat
:input
echo.
echo Type input:
FC con nul /lb1 /n |FIND "1:" > %temp%.\t2.bat
:: Error checking begins here
FIND "|" %temp%.\t2.bat > nul
if not errorlevel=1 goto error
FIND "=" %temp%.\t2.bat > nul
if not errorlevel=1 goto error
FIND " nul
if not errorlevel=1 goto error
FIND ">" %temp%.\t2.bat > nul
if not errorlevel=1 goto error
:: Error checking end
DEBUG %temp%.\t2.bat  nul
call %temp%.\t2.bat 字符串
for %%? in (t1.dat t2.bat) do del %temp%.\%%?
echo 字符串=%字符串%
goto eof
:error
echo.
echo @PROMPT Invalid input. Invalid characters: $Q $L $G $B$_> %temp%.\t2.bat
%comspec% /c %temp%.\t2.bat |FIND ":"
goto input
:eof[/code]

Batcher 发表于 2009-3-12 20:01

[quote]下面是一个按表顺序转换字符串的程序,原文来自本站开发室:
   [url]http://model.chinajewelry.net/dos/dosbbs/dispbbs.asp?boardID=6&ID=9250[/url]

    此程序为批处理中字符串值的顺序转换而编写,一般作为子程序被其他程序调用,也可以将此程序稍做修改,添加入特定批处理程序中作为子模块调用。[/quote][code]@echo off
if [%1]==[$] goto %2
if [%1]==[] goto nullargu

:main 主模块: count, match 句参数列表可以根据需要重新设定和增减
set factor=%1
call %0 $ count 1 2 3 4 5 7 8 9
call %0 $ match c d e f g h i j
goto display

:count 计数模块: 根据指定参数 factor 设置变量值 #a1, #a2
if [%3]==[] goto overflow
set #a1=%#a1%-
if not [%#a1%]==[----------] goto count_2
set #a1=
set #a2=%#a2%+
if [%#a2%]==[++++++++++] goto overflow
:count_2
if [%3]==[%factor%] goto end
shift
goto count

:match 匹配模块: 循环递增变量 #b1, #b2, 若分别与 #a1, #a2 相等, 则指定结果值为当前循环的参数 3.
if [%3]==[] goto overflow
set #b1=%#b1%-
if not [%#b1%]==[----------] goto match_2
set #b1=
set #b2=%#b2%+
if [%#b2%]==[++++++++++] goto overflow
:match_2
if [%#a1%]==[%#b1%] if [%#a2%]==[%#b2%] set result=%3
if not [%result%]==[] goto end
shift
goto match

:display 显示模块: 显示赋值结果
if [%error%]==[] echo %factor% convert %result%
set result=
set factor=
set error=
set #a1=
set #a2=
set #b1=
set #b2=
goto end

:nullargu
echo.
echo Error: argument 1 not specified.
echo.
goto end

:overflow
echo.
echo Error: number is overflow.
set error=overflow
echo.
goto end

:end[/code]

Batcher 发表于 2009-3-12 20:01

[quote]    下面是一个获取errorlevel值到环境变量的程序,原文来自本站开发室:
    [url]http://model.chinajewelry.net/dos/dosbbs/dispbbs.asp?boardID=6&ID=9358[/url][/quote][code]@echo off
set el=0
if errorlevel 1 set el=1
if not errorlevel 2 goto end

:partbydec
for %%h in (1 2) do if errorlevel %%h00 set elh=%%h
if [%elh%]==[1] for %%t in (0 1 2 3 4 5 6 7 8 9) do if errorlevel %elh%%%t0 set elt=%elh%%%t
if [%elh%]==[2] for %%t in (0 1 2 3 4 5) do if errorlevel %elh%%%t0 set elt=%elh%%%t
if not [%elt%]==[25] for %%o in (0 1 2 3 4 5 6 7 8 9) do if errorlevel %elt%%%o set el=%elt%%%o
if [%elt%]==[25] for %%o in (0 1 2 3 4 5) do if errorlevel %elt%%%o set el=%elt%%%o
set elh=
set elt=

:end
if not [%1]==[@] echo current errolevel is %el%[/code]

页: [1]

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