标题: 批处理编程的异类:游戏、数学、时钟(延时)等 [打印本页]
作者: Batcher 时间: 2009-3-12 19:48 标题: 批处理编程的异类:游戏、数学、时钟(延时)等
学过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,对本帖某些程序有任何疑问和建议的朋友,请访问下面的主题
http://www.cn-dos.net/forum/viewthread.php?tid=12388
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表达式计算批处理”
作者: Batcher 时间: 2009-3-12 19:49 标题: 批处理编程异类——游戏(Game)
复制代码
作者: Batcher 时间: 2009-3-12 19:51 标题: 批处理编程的异类——数学(Math)
1、数学——随机
1)随机数发生器:运行后可产生一个00~99的随机整数,并保存于环境变量 %_random% 中。
如果要产生更复杂的随机数,也可以利用这个程序进行修改来得到,比如两次产生的随机数组合,即可得到0000~9999的随机数;在随机数前加上小数点,即可得到0.00-0.99的随机小数;将 prompt _$T$_:: 中的第一个下划线去除,可以得到0~9的随机数;用debug可以去除某些随机数的前缀0。根据以上方法,即可组合得到任意形式的随机数了。- :: 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
复制代码
2)随机阵列发生器:运行后将生成一张随机概率分布矩阵图。
此图24行63列,由字符“-”(假)和“o”(真)组成,从上到下每三行为一组,呈现出o的分布概率依次降低。其中矩阵中的左图为真假序列分布,右图为真假数目对比。- :: 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
复制代码
2、数学——表达式
1)2K/XP表达式计算批处理,由 Brglng 原创并维护。转帖时精简了程序中的文档部分,可以通过以下链接查阅完整的代码和文档。
{16534}2K/XP表达式计算批处理
http://www.cn-dos.net/forum/viewthread.php?tid=16534- :: 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
复制代码
作者: Batcher 时间: 2009-3-12 19:53 标题: 批处理编程的异类——时钟(Clock)
1、暂停(Pause)、延迟(Delay)、等待(Wait)、睡眠(Sleep)
0) Win7 / Win10 等系统自带的 timeout 命令- @echo off
- echo %date% %time%
- timeout /nobreak /t 3 >nul
- echo %date% %time%
- pause
复制代码
1)for+set+if,时间精度为0.01秒,适用平台为WinNT/2K/XP/2003。
利用for解析变量%time%并存为两个时间点%start%和%now%,再利用set /a计算两个时间点的时间差,最后用if判断时间差是否达到设定的暂停时间。- @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
复制代码
2)VBS脚本中的sleep函数,时间精度为0.001秒,使用平台为Win9x/WinNT系列。
动态创建一个调用sleep()函数的VBS脚本,然后用Windows脚本宿主的命令行版本cscript调用它。- @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
复制代码
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秒的程序如下:- ping -n 2 -w 500 0.0.0.1>nul
复制代码
4)choice的缺省选择等待功能,时间精度为1秒,适用平台为MS-DOS/Win9x/WinNT系列。
/t:y,2 将缺省选择字符设置为y,等待时间是2秒,/n禁止[y,n]提示出现在命令行上,“rem|”的作用是禁止choice从键盘接受按键,从而暂停不会因为按下y或者其它键而被意外终止。- rem|choice /t:y,2 /n >nul
复制代码
5)ASCII汇编代码,延迟精度为0.001秒,适用平台为MS-DOS/Win9x/WinNT。
德国人Herbert Kleebauer给出了一个通用方案,通过ASCII汇编器间接产生一个sleep.exe程序来实现延迟,这个程序分为DOS和Win两个模块,分别调用DOS系统中断服务和WindowsAPI。- :: 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
复制代码
作者: Batcher 时间: 2009-3-12 19:54
@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 "[s" %2.MAP>NUL
IF ERRORLEVEL 1 GOTO MAPERR3
REM FIND "[25;80H" %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
[ 本帖最后由 Batcher 于 2009-3-12 19:55 编辑 ]
作者: Batcher 时间: 2009-3-12 19:56
@PROMPT { (\__ }
ECHO [1A[30C
@PROMPT { (\ `\__ }
ECHO [1A[30C
@PROMPT {(`, .------.,-' b $B }
ECHO [1A[30C
@PROMPT {`-/ ',__,-"` }
ECHO [1A[30C
@PROMPT { $B `; , $B }
ECHO [1A[30C
@PROMPT { \ ;.----/ ,/ }
ECHO [1A[30C
@PROMPT { ) // / ( ( \ }
ECHO [1A[30C
@PROMPT { \ \`.`\ \ \ \ }
ECHO [1A[30C
@PROMPT { `-` `" `-`" }
ECHO [1A[30C
@PORMPT $P$G
作者: Batcher 时间: 2009-3-12 19:57
- :: 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
复制代码
作者: Batcher 时间: 2009-3-12 19:57
- ::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
复制代码
作者: Batcher 时间: 2009-3-12 19:58 标题: 批处理编程的异类——网络(Network)
- @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
复制代码
- @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
复制代码
作者: Batcher 时间: 2009-3-12 19:59
- @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
复制代码
作者: Batcher 时间: 2009-3-12 19:59
- @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=
复制代码
作者: Batcher 时间: 2009-3-12 20:00
- :: 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
复制代码
作者: Batcher 时间: 2009-3-12 20:01
- @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
复制代码
作者: Batcher 时间: 2009-3-12 20:01
- @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%
复制代码
欢迎光临 批处理之家 (http://bbs.bathome.net/) |
Powered by Discuz! 7.2 |