当前位置: 首页 > 工具软件 > BAT.jl > 使用案例 >

bat实现命令行打字程序基本框架

霍建章
2023-12-01

四个文件和一个文件夹组成,需要在同一个目录,代码原理简单,初学者也能看懂
typing_test.bat是启动程序,负责运行程序
目前还有部分代码没有完成

@echo off
cd/d %~dp0
title 更新日志
if exist "result7.txt" (del result7.txt)
if not exist "%temp%\typing_testlog\" (md %temp%\typing_testlog\)
set log=%temp%\typing_testlog\typing_test.log
set ywpmdr=folder\篇目\英文\
set zwpmdr=folder\篇目\中文\
set cxm=typing_test
cls
echo [%date%_%time%] >>%log%
echo %cxm% starting...>>%log%
:1
cls
set bb=3.0.1.81.1
title Typing_test var %bb%
echo.
echo        输入"1"开始英文打字测试                  输入"2"开始中文打字测试
echo.                         
echo.       输入"set"进入set设置面板                 输入"q"返回      
echo. 
echo.
echo.
set /p "choice=请输入:
echo get var choice=%choice%>>%log%
if "%choice%"=="1" cls & goto yw
if "%choice%"=="2" cls & goto zw
if "%choice%"=="set" cls & goto set
if "%choice%"=="SET" cls & goto set
goto 1
:yw
set tmp1=yw
if exist "yw.vbs" (del yw.vbs)
if exist "folder\tmp\pmxz.txt" (del folder\tmp\pmxz.txt)
rem 生成gui--vbs
set a1=1.I saw a terrible movie 
set a2=2.Have you done the part-time job
set a3=3.you can get the kids
set a4=4.How to Be Happy
set a5=5.Life Experience
set a6=6.monsters
set a7=7.Eliminate Internal Negative Chatter
set a8=8.positive Affirmations
set a9=9.positive Scripts
set a10=10.about Insomnia
set a11=11.Problem in my life
set a12=12.Provide a better Life for senior citizen 
set a13=13.The Necesssary of practical Experience
echo Dim name,msg >>%tmp1%.vbs
echo msg="请输入你要选择的篇目:"+chr(13)+"%a1%"+chr(13)+"%a2%"+chr(13)+"%a3%"+chr(13)+"%a4%"+chr(13)+"%a5%"+chr(13)+"%a6%"+chr(13)+"%a7%"+chr(13)+"%a8%"+chr(13)+"%a9%"+chr(13)+"%a10%"+chr(13)+"%a11%"+chr(13)+"%a12%"+chr(13)+"%a13%" >>%tmp1%.vbs
echo name=Inputbox(msg) >>%tmp1%.vbs
echo set fso=CreateObject("Scripting.FileSystemObject") >>%tmp1%.vbs
echo set fw=fso.createtextfile("folder\tmp\pmxz.txt",2) >>%tmp1%.vbs
echo fw.writeline("0") >>%tmp1%.vbs
echo fw.writeline(name) >>%tmp1%.vbs
echo fw.writeline("0")>>%tmp1%.vbs
echo fw.close>>%tmp1%.vbs
echo type %tmp1%.vbs-command=>>%LOG%
cscript %tmp1%.vbs
for  /f  "skip=1 delims="  %%a  in  (folder\tmp\pmxz.txt)  do  (
set  ywpm=%%~a
echo get var ywpm=%ywpm% >>%LOG%
goto  checkywpm
)
:checkywpm
if not exist "%ywpmdr%%ywpm%.txt" (goto checkywmiss) else goto starting1
:checkywmiss
ECHO cannot find %ywpmdr%%ywpm%.txt
echo msgbox "不能找到%ywpmdr%%ywpm%.txt"+chr(13)+"输入有误,请重新输入",16,"typing_test" >tmp2.vbs
cscript tmp2.vbs
type tmp2.vbs >>%log%
del tmp2.vbs
cls & goto yw
:starting1
echo staring yw.bat...>>%log%
start yw.bat
ECHO log end >>%LOG%
exit
:zw
echo 暂未开放,任意键返回
pause & cls & goto 1
:set
echo 暂未开放,任意键返回
pause & cls & goto 1

yw.bat是对应英文打字练习的程序

@echo off
cd/d %~dp0
set file=yw.bat
if exist "folder\tmp\text1.txt" (del folder\tmp\text1.txt)
goto YW
:error2
tasklist
set e1=typing_test出现了崩溃
set e2=错误代码:2
set e3=请联系软件开发者,并提交日志文件
set e7=崩溃程序:%file%
set e4=email:xiaohui032901@foxmail.com
set e5=QQ:2151335401
set e6=QQ:3135144152
:error1
set e1=typing_test出现了崩溃
set e2=错误代码:1
set e3=请联系软件开发者,并提交日志文件
set e7=崩溃程序:%file%
set e4=email:xiaohui032901@foxmail.com
set e5=QQ:2151335401
set e6=QQ:3135144152
echo msgbox "%e1%"+chr(13)+"%e2%"+chr(13)+"%e7%"+chr(13)+"%e3%"+chr(13)+"%e4%"+chr(13)+"%e5%"+chr(13)+"%e6%",48,"typing_test崩溃文件" >error.vbs
cscript error.vbs
del error.vbs
exit
echo msgbox "%e1%"+chr(13)+"%e2%"+chr(13)+"%e7%"+chr(13)+"%e3%"+chr(13)+"%e4%"+chr(13)+"%e5%"+chr(13)+"%e6%",48,"typing_test崩溃文件" >error.vbs
cscript error.vbs
del error.vbs
exit
:YW
if not exist "%temp%\typing_testlog\" (md %temp%\typing_testlog\)
set log=%temp%\typing_testlog\yw.log
set lj1=folder\篇目\英文\
echo [%date%_%time%]>>%log%
echo yw.bat starting....>>%log%
title yw.bat
echo start result2.bat....
if not exist "result2.bat" (echo result2.bat has loss>>%log% & goto error1)
start/min result2.bat
if errorlevel 0 (echo starting result2.bat succeed >>%log%) else echo starting result2.bat failed>>%log% goto error2
rem 获取打字的篇目
for  /f  "skip=1 delims="  %%a  in  (folder\tmp\pmxz.txt)  do  (
set  ywpm=%%~a
echo get var ywpm=%ywpm%>>%log%
goto main
)
:main
if not exist "%lj1%%ywpm%.txt" (echo %lj1%%ywpm%.txt has loss >>%log% & goto error1)
echo type txt >>%log%
type %lj1%%ywpm%.txt >>%log%
rem c篇目行数
for /f %%a in (' find /c /v "" ^<"%lj1%%ywpm%.txt" ') do set/a c=%%a
set/a c=%c%-1
echo get var c=%c%>>%log%
rem b是当前行数
set/a b=%b%+1 
echo get var b=%b%>>%log%
if %b% EQU %c%  (title 循环:Typing test var %bb%  & set/a b=0 & goto main)
for  /f  "skip=%b% delims="  %%a  in  (%lj1%%ywpm%.txt)  do  (
set  txt=%%~a
goto 1
)
:1
cls
if exist "result7.txt" (echo log end >>%log% & title 时间到 & start typing_test.bat & exit)
:3
echo.                         
echo ——————————————————————————————————
echo.
echo                     typing_test
echo.
echo.
echo --------------------------------------------------------------------
echo 原文:
echo.
echo %txt%
echo:
echo 输入:
echo.
set/p "a=
echo get var txt=%txt% >>%log%
echo get var a=%a% >>%log%   
if "%a%"=="%txt%" echo 0 >>folder\tmp\text1.txt
if "%a%"== "return" cls & goto return
if "%a%"=="q" cls & goto q
if "%a%"=="%empty%" (cls & echo 未检测到输出内容请重新输入 & echo input=empty>>%log% & goto 3) 
echo %a% >> folder\cache\ywpm1cache.txt
echo %a% >> folder\cache\ywpm2cache.txt
goto main 
:return
echo an return command is in progress >>%log%
start typing_test.bat
echo 0 >>resultexit
exit
:q
echo [yw1]an exit command is in progress >>%log%
echo 0 >>resultexit
exit

result.bat是配套程序,负责计时

rem 部分代码来自批处理之家,bat
@echo off
title 配套程序
echo 正在启动程序
echo 0 >> Servicetext.txt
if exist "C:\WINDOWS\system32\Servicetext.txt" (del C:\WINDOWS\system32\Servicetext.txt & set admin=enable)
cd /d %~dp0
if not exist "%temp%\typing_testlog\"
md %temp%\typing_testlog\
set log=%temp%\typing_testlog\result2log.txt
if exist "Servicetext.txt" (del Servicetext.txt & set admin=not enable)
echo administrator mode:%admin% >>%log%
echo [%date%_%time%] >> %log%
echo name=typing_test--result2.bat >> %log%
if exist "result2.txt" (echo error cause found result2.txt  >> %log% & exit)
if exist "result5.txt" (del result5.txt)
if exist "result.vbs"  (del result.vbs)
if exist "result6.txt" (del result6.txt)
if exist "result7.txt" (del result7.txt)
if exist "result.txt" (del result.txt)
if exist "ywpm1cache.txt" (del ywpm1cache.txt)
if exist "ywpm2cache.txt" (del ywpm2cache.txt)
if exist "folder\cache\ywpm1cache.txt" (del folder\cache\ywpm1cache.txt)
set/a x=1
SETLOCAL ENABLEDELAYEDEXPANSION
if not exist "folder\user\timeset\timeset.txt" echo some configuration files and return typing_test.bat >> %log% & start typing_test.bat & start 1.bat
for  /f  "skip=1 delims="  %%a  in  (folder\user\timeset\timeset.txt)  do  (
set/a a=%%~a
echo get variable timeset=1 >> %log%
goto js
)
:js
echo 正在运行中
set/a x=0
echo x=%x%
goto js2
:js2
if "%x%"=="%a%" (goto end)
ping /n 60 127.0.0.1 >> folder\cache\ping2.txt
if exist "resultexit.txt" (del resultexit.txt & taskkill /f /im gray.exe >nul & exit)
if exist "result7.txt" (del result7.txt)
set/a x=%x%+1
FOR /F "delims=" %%i IN (folder\cache\ywpm1cache.txt) DO CALL :strlen %%i
set/a j=%n%/%x%
echo get var j =%j% >> %log%
echo 0 >> result7.txt
echo %j% >> result7.txt
echo 0 >> result7.txt
echo x=%x%
goto js2
:end
start/wait time.vbs
ping /n 5 127.0.0.1 >> folder\cache\ping2.txt
FOR /F "delims=" %%i IN (folder\cache\ywpm1cache.txt) DO CALL :strlen %%i
if not exist "folder\cache\test1.txt" (
set /a c = 0 & goto 0
) else (
 for /f %%a in (' find /c /v "" ^<"folder\cache\test1.txt" ') do set c=%%a & goto 0
)
:0
set /a w = %c% * 6
set /a result = %n%/5
set /a result0 = %n%+%result%
set /a result1 = %result0%/%a%
set /a result2 = %result1%-%w%
echo a=%a% c=%c% w=%w% >>  %log% 
echo result =%result% >>  %log% 
echo result0 =%result0% >>  %log% 
echo result1=%result1% >>  %log% 
echo result2=%result2% >>  %log% 
set a1=测试时长:%a%
ping /n 1 127.0.0.1 >> folder\cache\ping2.txt
if %result2% GTR 160 (set a2=打字水平:top & goto jl) else echo 0 >> folder\cache\pd.txt
if %result2% GTR 140 (set a2=打字水平:专家& goto jl) else echo 0 >> folder\cache\pd.txt
if %result2% GTR 120 (set a2=打字水平:快手  & goto jl) else echo 0 >> folder\cache\pd.txt
if %result2% GTR 100 (set a2=打字水平:高手 & goto jl) else echo 0 >> folder\cache\pd.txt
if %result2% GTR 70 (set a2=打字水平:中手 & goto jl) else echo 0 >> folder\cache\pd.txt
if %result2% LSS 70 (set a2=打字水平:低级& goto jl) else echo 数据无效 & start 7.vbs & echo [errorcode 7] cause:int is warned  amd log end >> %log%  &  exit 
echo log end   >> %log% 
:jl
ping /n 1 127.0.0.1 >> folder\cache\ping2.txt
set a3=打字速度为%result1%字每分钟
set a4=共有%c%处错误 
echo 正确率:%l%%%
ping /n 1 127.0.0.1 >> folder\cache\ping2.txt
set a5=最终得分为%result2%
echo msgbox "%a1%"+chr(13)+"%a2%"+chr(13)+"%a3%"+chr(13)+"%a4%"+chr(13)+"%a5%",64,"typing_test-result2" >>tmp1.vbs
start/wait tmp1.vbs
del tmp1.vbs
exit
:strlen
    SET str=%*
    SET str=%str: =%
    :strlen_loop
    IF "%str%" NEQ "" (
        SET /A n += 1
        SET str=!str:~1!
        GOTO :strlen_loop
    )

vbs实现自动按键,在结束后自动提交

在Set  objeShell = CreateObject("Wscript.Shell")
Set WshShell = WScript.CreateObject("WScript.Shell")
WScript.Sleep 10000 
For i = 1 To 2
WshShell.SendKeys "{ENTER}" 
WScript.Sleep 1000
Next

folder.zip:https://gitee.com/batchbox/typing_test/raw/master/folder.zip
下载后解压到typing_test.bat的目录

 类似资料: