Board logo

标题: [其他] 求助,我做了一个升级版的cmd,运行不正常 [打印本页]

作者: LTSC1809    时间: 2023-7-1 22:29     标题: 求助,我做了一个升级版的cmd,运行不正常

本帖最后由 LTSC1809 于 2023-7-1 22:42 编辑

它的同目录下有两个后缀为.ldt的文件,config.ldt的内容为
  1. true或者false;true或者false;颜色代码
复制代码
doskey.ldt的内容为
  1. 代号=要执行的命令
复制代码
,doskey.ldt有好几行,但都是这种格式。这个bat开头会读取它们的内容并用到它们。现在打开以后没有显示出来加载用时,而且没有出现宏命令加载中的文字,还有就是出现输入文字的地方以后输入任何内容回车以后都会闪退,用cmd运行它,发现不管输入什么都显示此处不应有啥啥啥,求解。我自己看半天没看出问题来,代码如下:
  1. @echo off
  2. echo 加载文件:
  3. echo 加载配置文件...
  4. rem time
  5. set "t=%time%"
  6. rem code
  7. setlocal
  8. rem time
  9. set "t=%time%"
  10. rem code
  11. for /f "delims=; tokens=1" %%i in (%cd%\config.ldt) do set dbg=%%i
  12. for /f "delims=; tokens=2" %%t in (%cd%\config.ldt) do set dk=%%t
  13. for /f "delims=; tokens=3" %%b in (%cd%\config.ldt) do set color=%%b
  14. if %dbg% equ true (set dbgx=a) else (if %dbg% equ false (set dbgx=b) else (goto configerr))
  15. if %dk% equ true (set dkif=a) else (if %dk% equ false (set dkif=b) else (goto configerr))
  16. for %%i in (01,02,03,04,05,06,07,08,09,0a,0b,0c,0d,0e,0f,10,12,16,14,15,16,17,18,19,1a,1b,1c,1d,1e,1f,20,21,23,24,25,26,27,28,29,2a,2b,2c,2d,2e,2f,30,31,32,34,35,36,37,38,39,3a,3b,3c,3d,3e,3f,40,41,42,43,45,46,47,48,49,4a,4b,4c,4d,4e,4f50,51,52,53,54,56,57,58,59,5a,5b,5c,5d,5e,5f,60,61,62,63,64,65,67,68,69,6a,6b,6c,6d,6e,6f,70,71,72,73,74,75,76,77,78,79,7a,7b,7c,7d,7e,7f,80,81,82,83,84,85,86,87,89,8a,8b,8c,8d,8e,8f,90,91,92,93,94,95,96,97,98,9a,9b,9c,9d,9e,9f,a0,a1,a2,a3,a4,a5,a6,a7,a8,a9,ab,ac,ab,ac,ad,ae,af,b0,b1,b2,b3,b4,b5,b6,b7,b8,b9,ba,bc,bd,be,bf,c0,c1,c2,c3,c4,c5,c6,c7,c8,c9,ca,cb,cd,ce,cf,d0,d1,d2,d3,d4,d5,d6,d7,d8,d9,da,db,dc,de,df,e0,e1,e2,e3,e4,e5,e6,e7,e8,e9,ea,eb,ec,ed,ef,f0,f1,f2,f3,f4,f5,f6,f7,f8,f9,fa,fb,fc,fd,fe) do if %color% equ %%i=goto colok
  17. goto configerr
  18. rem time
  19. set "t1=%time%"
  20. if "%t1:~,2%" lss "%t:~,2%" set "add=+24"
  21. set /a "times=(%t1:~,2%-%t:~,2%%add%)*360000+(1%t1:~3,2%%%100-1%t:~3,2%%%100)*6000+(1%t1:~6,2%%%100-1%t:~6,2%%%100)*100+(1%t1:~-2%%%100-1%t:~-2%%%100)" ,"ss=(times/100)%%60","mm=(times/6000)%%60","hh=times/360000","ms=times%%100"
  22. echo 完成 用时%hh%:%mm%:%ss%.%ms%(时:分:秒.毫秒)
  23. rem endtime
  24. if %dkif% equ b=goto nodk
  25. rem time
  26. set "t=%time%"
  27. rem code
  28. echo 加载宏命令...
  29. for /f %%a in (%cd%\doskey.ldt) do doskey %%a
  30. rem time
  31. set "t1=%time%"
  32. if "%t1:~,2%" lss "%t:~,2%" set "add=+24"
  33. set /a "times=(%t1:~,2%-%t:~,2%%add%)*360000+(1%t1:~3,2%%%100-1%t:~3,2%%%100)*6000+(1%t1:~6,2%%%100-1%t:~6,2%%%100)*100+(1%t1:~-2%%%100-1%t:~-2%%%100)" ,"ss=(times/100)%%60","mm=(times/6000)%%60","hh=times/360000","ms=times%%100"
  34. echo 完成 用时%hh%:%mm%:%ss%.%ms%(时:分:秒.毫秒)
  35. endlocal
  36. :nodk
  37. rem time
  38. set "t1=%time%"
  39. if "%t1:~,2%" lss "%t:~,2%" set "add=+24"
  40. set /a "times=(%t1:~,2%-%t:~,2%%add%)*360000+(1%t1:~3,2%%%100-1%t:~3,2%%%100)*6000+(1%t1:~6,2%%%100-1%t:~6,2%%%100)*100+(1%t1:~-2%%%100-1%t:~-2%%%100)" ,"ss=(times/100)%%60","mm=(times/6000)%%60","hh=times/360000","ms=times%%100"
  41. echo 加载完成 总用时%hh%:%mm%:%ss%.%ms%(时:分:秒.毫秒)
  42. :colok
  43. color %color%
  44. if %dbgx% equ a=goto dbg
  45. echo.
  46. echo BetterCommand Development version 0.1a
  47. echo.
  48. :inputloop
  49. set /p com=^>
  50. if %com% equ help=%cd%\help.txt
  51. if %com% equ exit=exit
  52. if %com% equ shutdown (set /p shutyn=Shutdown now?(Y/N):
  53. if %shutyn% equ y=shutdown /s /t 0 /f)
  54. %com%
  55. goto inputloop
  56. :configerr
  57. echo 配置文件可能存在问题,加载失败,请检查config.ldt。
  58. pause
  59. exit
  60. :dbg
  61. @echo on
  62. :inputloopdbg
  63. set /p com=^>
  64. if %com% equ help=%cd%\help.txt
  65. if %com% equ exit=exit
  66. if %com% equ shutdown (set /p shutyn=Shutdown now?(Y/N):
  67. if %shutyn% equ y=shutdown /s /t 0 /f)
  68. %com%
  69. pause
  70. goto inputloopdbg
复制代码

作者: Batcher    时间: 2023-7-1 22:50

回复 1# LTSC1809


if %color% equ %%i=goto colok
这种格式的if命令你写了很多个,先把等号改成空格试试吧。
作者: LTSC1809    时间: 2023-7-1 22:59

好的,等号是不是容易被当成赋值号啊,刚接触bat学判断输入内容的时候看百度知道说的if就是用等号,营销号真是害人不浅啊,我现在都习惯了等号了,基本只要不是多行命令用括号其他都用的等号[捂脸哭]
作者: Batcher    时间: 2023-7-1 23:09

回复 3# LTSC1809


    不知道你看的是哪篇文章,也可能是你理解错了。
作者: LTSC1809    时间: 2023-7-2 00:15

回复 4# Batcher 刚刚看了一下那篇教程,确实是空格欸,我用了这么久的等号竟然不报错能用,堪称奇迹
作者: 77七    时间: 2023-7-2 06:38

回复 5# LTSC1809


   等号、逗号、分号、空格、制表符 大概都可以
http://bbs.bathome.net/viewthread.php?tid=2286
这个代码就用的 逗号
作者: 77七    时间: 2023-7-2 07:16

本帖最后由 77七 于 2023-7-2 07:37 编辑
  1. @echo off
  2. ::第7行
  3. setlocal enabledelayedexpansion
  4. ::第11-15行
  5. for /f "useback tokens=1-3 delims=;" %%a in ("%cd%\config.ldt") do (
  6. set color=%%c
  7. if "%%a" equ "true" (
  8. set dbgx=a
  9. ) else if "%%a" equ "false" (
  10. set dbgx=b
  11. ) else (
  12. goto :configerr
  13. )
  14. rem ...
  15. )
  16. ::第16行
  17. echo %color%|findstr /rbec:"[0-9a-f][0-9a-f]" |findstr /ivc:"%color:~0,1%%color:~0,1%" 1>nul && (
  18. goto :colok
  19. ) || (
  20. goto :configerr
  21. )
  22. ::第18-41行,不会被执行
  23. ::第52-53行
  24. if "%com%" equ "shutdown" (
  25. set /p shutyn=Shutdown now?[Y/N]:
  26. if /i "!shutyn!" equ "y" (
  27. shutdown /s /t 0 /f
  28. )
  29. )
  30. ::后续未修改
  31. ::耗时部分
  32. set "t=%time%"
  33. rem ...
  34. set "t1=%time%"
  35. call :c
  36. set "t=%time%"
  37. rem ...
  38. set "t1=%time%"
  39. call :c
  40. ::这段放代码 exit 命令之后
  41. :c
  42. if "%t1:~,2%" lss "%t:~,2%" set "add=+24"
  43. set /a "times=(%t1:~,2%-%t:~,2%%add%)*360000+(1%t1:~3,2%%%100-1%t:~3,2%%%100)*6000+(1%t1:~6,2%%%100-1%t:~6,2%%%100)*100+(1%t1:~-2%%%100-1%t:~-2%%%100)"
  44. echo 检索时间 %times% ms
  45. exit /b
复制代码
根据我的经验和习惯改的,有些地方改的可能不正确。
作者: LTSC1809    时间: 2023-7-2 07:56

回复 7# 77七 感谢!尤其是findstr判断颜色代码是不是正确,比我用for一种一种判断好多了
作者: LTSC1809    时间: 2023-7-2 08:03

我自己发现一个问题,就是有两个变量在加载的时候被赋值,后面要判断它们的内容,但是我限制了变量作用域,这样它们就没办法在后面判断了,加变量作用域是为了计时器,计时器有好几个,变量冲突了,这个计时器是照搬论坛某贴的代码,我不会改变量名啊
作者: LTSC1809    时间: 2023-7-2 08:08

回复 7# 77七
网上看到说bat的if不能else if,要用嵌套形式,即else (if xxx equ xxx exit)这种,真的吗您的代码中好像有else if
作者: Batcher    时间: 2023-7-2 10:04

回复 10# LTSC1809


    不能elseif,能else if,有可能是你看的不仔细吧
作者: LTSC1809    时间: 2023-7-2 12:23

我改过的代码,现在还是故障依旧,Batcher说的if 等于号换成空格我改了,还有就是去掉了变量作用域,但是现在还是不显示正在加载宏命令,也不显示加载用时,还有就是一输入命令就闪退,改完的代码如下:
  1. @echo off
  2. echo 加载文件:
  3. echo 加载配置文件...
  4. rem time
  5. set "t=%time%"
  6. rem code
  7. rem time
  8. set "tx=%time%"
  9. rem code
  10. for /f "delims=; tokens=1" %%i in (%cd%\config.ldt) do set dbg=%%i
  11. for /f "delims=; tokens=2" %%t in (%cd%\config.ldt) do set dk=%%t
  12. for /f "delims=; tokens=3" %%b in (%cd%\config.ldt) do set color=%%b
  13. if %dbg% equ true (set dbgx=a) else (if %dbg% equ false (set dbgx=b) else (goto configerr))
  14. if %dk% equ true (set dkif=a) else (if %dk% equ false (set dkif=b) else (goto configerr))
  15. for %%i in (01,02,03,04,05,06,07,08,09,0a,0b,0c,0d,0e,0f,10,12,16,14,15,16,17,18,19,1a,1b,1c,1d,1e,1f,20,21,23,24,25,26,27,28,29,2a,2b,2c,2d,2e,2f,30,31,32,34,35,36,37,38,39,3a,3b,3c,3d,3e,3f,40,41,42,43,45,46,47,48,49,4a,4b,4c,4d,4e,4f50,51,52,53,54,56,57,58,59,5a,5b,5c,5d,5e,5f,60,61,62,63,64,65,67,68,69,6a,6b,6c,6d,6e,6f,70,71,72,73,74,75,76,77,78,79,7a,7b,7c,7d,7e,7f,80,81,82,83,84,85,86,87,89,8a,8b,8c,8d,8e,8f,90,91,92,93,94,95,96,97,98,9a,9b,9c,9d,9e,9f,a0,a1,a2,a3,a4,a5,a6,a7,a8,a9,ab,ac,ab,ac,ad,ae,af,b0,b1,b2,b3,b4,b5,b6,b7,b8,b9,ba,bc,bd,be,bf,c0,c1,c2,c3,c4,c5,c6,c7,c8,c9,ca,cb,cd,ce,cf,d0,d1,d2,d3,d4,d5,d6,d7,d8,d9,da,db,dc,de,df,e0,e1,e2,e3,e4,e5,e6,e7,e8,e9,ea,eb,ec,ed,ef,f0,f1,f2,f3,f4,f5,f6,f7,f8,f9,fa,fb,fc,fd,fe) do if %color% equ %%i goto colok
  16. goto configerr
  17. rem time
  18. set "t1x=%time%"
  19. if "%t1x:~,2%" lss "%tx:~,2%" set "add=+24"
  20. set /a "timesx=(%t1x:~,2%-%tx:~,2%%add%)*360000+(1%t1x:~3,2%%%100-1%tx:~3,2%%%100)*6000+(1%t1x:~6,2%%%100-1%tx:~6,2%%%100)*100+(1%t1x:~-2%%%100-1%tx:~-2%%%100)" ,"ssx=(timesx/100)%%60","mmx=(timesx/6000)%%60","hhx=timesx/360000","msx=timesx%%100"
  21. echo 完成 用时%hhx%:%mmx%:%ssx%.%msx%(时:分:秒.毫秒)
  22. rem endtime
  23. if %dkif% equ b goto nodk
  24. rem time
  25. set "tn=%time%"
  26. rem code
  27. echo 加载宏命令...
  28. for /f %%a in (%cd%\doskey.ldt) do doskey %%a
  29. rem time
  30. set "t1n=%time%"
  31. if "%t1n:~,2%" lss "%tn:~,2%" set "add=+24"
  32. set /a "timesn=(%t1n:~,2%-%tn:~,2%%add%)*360000+(1%t1n:~3,2%%%100-1%tn:~3,2%%%100)*6000+(1%t1n:~6,2%%%100-1%tn:~6,2%%%100)*100+(1%t1n:~-2%%%100-1%tn:~-2%%%100)" ,"ssn=(timesn/100)%%60","mmn=(timesn/6000)%%60","hhn=timesn/360000","msn=timesn%%100"
  33. echo 完成 用时%hhn%:%mmn%:%ssn%.%msn%(时:分:秒.毫秒)
  34. :nodk
  35. rem time
  36. set "t1=%time%"
  37. if "%t1:~,2%" lss "%t:~,2%" set "add=+24"
  38. set /a "times=(%t1:~,2%-%t:~,2%%add%)*360000+(1%t1:~3,2%%%100-1%t:~3,2%%%100)*6000+(1%t1:~6,2%%%100-1%t:~6,2%%%100)*100+(1%t1:~-2%%%100-1%t:~-2%%%100)" ,"ss=(times/100)%%60","mm=(times/6000)%%60","hh=times/360000","ms=times%%100"
  39. echo 加载完成 总用时%hh%:%mm%:%ss%.%ms%(时:分:秒.毫秒)
  40. :colok
  41. color %color%
  42. if %dbgx% equ a goto dbg
  43. echo.
  44. echo BetterCommand Development version 0.1a
  45. echo.
  46. :inputloop
  47. set /p com=^>
  48. if %com% equ help %cd%\help.txt
  49. if %com% equ exit exit
  50. if %com% equ shutdown (set /p shutyn=Shutdown now?(Y/N):
  51. if %shutyn% equ y shutdown /s /t 0 /f)
  52. %com%
  53. goto inputloop
  54. :configerr
  55. echo 配置文件可能存在问题,加载失败,请检查config.ldt。
  56. pause
  57. exit
  58. :dbg
  59. @echo on
  60. :inputloopdbg
  61. set /p com=^>
  62. if %com% equ help %cd%\help.txt
  63. if %com% equ exit exit
  64. if %com% equ shutdown (set /p shutyn=Shutdown now?(Y/N):
  65. if %shutyn% equ y shutdown /s /t 0 /f)
  66. %com%
  67. pause
  68. goto inputloopdbg
复制代码

作者: 77七    时间: 2023-7-2 13:57

回复 12# LTSC1809


   7楼代码
if "%com%" equ "shutdown" (
        set /p shutyn=Shutdown now?[Y/N]:
        if /i "!shutyn!" equ "y" (
                shutdown /s /t 0 /f
        )
)

不是比较数值大小,就把变量用引号括起来,否则变量为空值就闪退,就像这里的 %shutyn%肯定是空值,必须开启变量延迟扩展,使用 !shutyn!;

在括号内使用set /p 后面不要用(),或者使用 ^ 转义,^(  ^),否则闪退。
作者: LTSC1809    时间: 2023-7-2 14:05

回复 13# 77七
谢了!那么,开头的加载时间还有正在加载宏命令这些东西不显示是为什么
作者: 77七    时间: 2023-7-2 14:09

回复 14# LTSC1809


   1楼代码第17行,使用 goto 跳转了。(7楼代码第23行)
作者: LTSC1809    时间: 2023-7-2 14:09

还有就是发现宏命令不起作用,有的人说只有在交互式的地方doskey才有用,但是我之前亲测过,只做一个简单的循环用set /p获取输入然后执行,通过set /p输入的指令也能用doskey定义的指令,所以这里也是一样用set /p获取输入,为什么宏命令不起作用
作者: LTSC1809    时间: 2023-7-2 14:11

回复 15# 77七
也就是说宏命令根本没被加载?我说怎么用不了doskey.ldt里定义的内容,但是我的config.ldt写到doskey功能是开了的啊,为什么不行
作者: LTSC1809    时间: 2023-7-2 14:13

回复 15# 77七
不对啊,一楼代码17行是跳转到configerr这个标签,但是跳转了的话应该显示配置文件有问题啊,为什么还是让你输入命令了
作者: 77七    时间: 2023-7-2 14:18

回复 18# LTSC1809


   1楼代码16行还存在一个可能的goto 跳转。
作者: LTSC1809    时间: 2023-7-2 14:47

感谢大家的答复,现在已经完全正常了,我还给他加了在配置文件里设置窗口大小的功能,代码如下:
  1. @echo off
  2. rem time
  3. set "t=%time%"
  4. rem code
  5. rem time
  6. set "tx=%time%"
  7. rem code
  8. for /f "delims=; tokens=1" %%o in (%cd%\config.ldt) do set conls=%%o
  9. for /f "delims=' tokens=1" %%z in ("%conls%") do set modc=%%z
  10. for /f "delims=' tokens=2" %%e in ("%conls%") do set modl=%%e
  11. for /f "delims=; tokens=2" %%i in (%cd%\config.ldt) do set dbg=%%i
  12. for /f "delims=; tokens=3" %%t in (%cd%\config.ldt) do set dk=%%t
  13. for /f "delims=; tokens=4" %%b in (%cd%\config.ldt) do set color=%%b
  14. if %dbg% equ true (set dbgx=a) else (if %dbg% equ false (set dbgx=b) else (goto configerr))
  15. if %dk% equ true (set dkif=a) else (if %dk% equ false (set dkif=b) else (goto configerr))
  16. for %%i in (01,02,03,04,05,06,07,08,09,0a,0b,0c,0d,0e,0f,10,12,16,14,15,16,17,18,19,1a,1b,1c,1d,1e,1f,20,21,23,24,25,26,27,28,29,2a,2b,2c,2d,2e,2f,30,31,32,34,35,36,37,38,39,3a,3b,3c,3d,3e,3f,40,41,42,43,45,46,47,48,49,4a,4b,4c,4d,4e,4f50,51,52,53,54,56,57,58,59,5a,5b,5c,5d,5e,5f,60,61,62,63,64,65,67,68,69,6a,6b,6c,6d,6e,6f,70,71,72,73,74,75,76,77,78,79,7a,7b,7c,7d,7e,7f,80,81,82,83,84,85,86,87,89,8a,8b,8c,8d,8e,8f,90,91,92,93,94,95,96,97,98,9a,9b,9c,9d,9e,9f,a0,a1,a2,a3,a4,a5,a6,a7,a8,a9,ab,ac,ab,ac,ad,ae,af,b0,b1,b2,b3,b4,b5,b6,b7,b8,b9,ba,bc,bd,be,bf,c0,c1,c2,c3,c4,c5,c6,c7,c8,c9,ca,cb,cd,ce,cf,d0,d1,d2,d3,d4,d5,d6,d7,d8,d9,da,db,dc,de,df,e0,e1,e2,e3,e4,e5,e6,e7,e8,e9,ea,eb,ec,ed,ef,f0,f1,f2,f3,f4,f5,f6,f7,f8,f9,fa,fb,fc,fd,fe) do if %color% equ %%i goto colok
  17. goto configerr
  18. :colok
  19. rem time
  20. set "t1x=%time%"
  21. if "%t1x:~,2%" lss "%tx:~,2%" set "add=+24"
  22. set /a "timesx=(%t1x:~,2%-%tx:~,2%%add%)*360000+(1%t1x:~3,2%%%100-1%tx:~3,2%%%100)*6000+(1%t1x:~6,2%%%100-1%tx:~6,2%%%100)*100+(1%t1x:~-2%%%100-1%tx:~-2%%%100)" ,"ssx=(timesx/100)%%60","mmx=(timesx/6000)%%60","hhx=timesx/360000","msx=timesx%%100"
  23. rem endtime
  24. if %dkif% equ b goto nodk
  25. rem time
  26. set "tn=%time%"
  27. rem code
  28. for /f "delims=" %%a in (%cd%\doskey.ldt) do doskey %%a
  29. rem time
  30. set "t1n=%time%"
  31. if "%t1n:~,2%" lss "%tn:~,2%" set "add=+24"
  32. set /a "timesn=(%t1n:~,2%-%tn:~,2%%add%)*360000+(1%t1n:~3,2%%%100-1%tn:~3,2%%%100)*6000+(1%t1n:~6,2%%%100-1%tn:~6,2%%%100)*100+(1%t1n:~-2%%%100-1%tn:~-2%%%100)" ,"ssn=(timesn/100)%%60","mmn=(timesn/6000)%%60","hhn=timesn/360000","msn=timesn%%100"
  33. :nodk
  34. rem time
  35. set "t1=%time%"
  36. if "%t1:~,2%" lss "%t:~,2%" set "add=+24"
  37. set /a "times=(%t1:~,2%-%t:~,2%%add%)*360000+(1%t1:~3,2%%%100-1%t:~3,2%%%100)*6000+(1%t1:~6,2%%%100-1%t:~6,2%%%100)*100+(1%t1:~-2%%%100-1%t:~-2%%%100)" ,"ss=(times/100)%%60","mm=(times/6000)%%60","hh=times/360000","ms=times%%100"
  38. color %color%
  39. mode con cols=%modc% lines=%modl%
  40. if %dbgx% equ a goto dbg
  41. echo 加载文件:
  42. echo 加载配置文件...
  43. echo 完成 用时%hhx%:%mmx%:%ssx%.%msx%(时:分:秒.毫秒)
  44. echo 加载宏命令...
  45. echo 完成 用时%hhn%:%mmn%:%ssn%.%msn%(时:分:秒.毫秒)
  46. echo 加载完成 总用时%hh%:%mm%:%ss%.%ms%(时:分:秒.毫秒)
  47. echo.
  48. echo BetterCommand Development version 0.1a
  49. echo.
  50. :inputloop
  51. set /p com=^>
  52. if "%com%" equ help %cd%\help.txt
  53. if "%com%" equ exit exit
  54. if "%com%" equ shutdown (set /p shutyn=Shutdown now?[Y/N]:
  55. if "%shutyn%" equ y shutdown /s /t 0 /f)
  56. %com%
  57. goto inputloop
  58. :configerr
  59. echo 配置文件可能存在问题,加载失败,请检查config.ldt。
  60. pause
  61. exit
  62. :dbg
  63. @echo on
  64. :inputloopdbg
  65. set /p com=^>
  66. if "%com%" equ help %cd%\help.txt
  67. if "%com%" equ exit exit
  68. if "%com%" equ shutdown (set /p shutyn=Shutdown now?[Y/N]:
  69. if "%shutyn%" equ y shutdown /s /t 0 /f)
  70. %com%
  71. pause
  72. goto inputloopdbg
复制代码
doskey.ldt和config.ldt这两个需要放在和主文件同一个目录下,其中doskey.ldt的内容就是一行行的代号=执行的命令,语法就是doskey命令不打出来开头的doskey几个字。config.ldt内容是:
  1. 窗口大小的列数'行数;是否启用debug模式(false或者true,打开这个模式后回显会开启,并且会执行一条命令暂停一次,让你按任意键继续);是否启用doskey宏功能,关掉它则doskey.ldt的内容不会被加载(false或者true);颜色代码(就是color命令的语法不打出来开头的color几个字)
复制代码
这些文件都有了并且格式正确以后,双击打开bat主文件,就能体验了,最后再次感谢大家的帮助,没有你们我不可能完成这项作品。
作者: 77七    时间: 2023-7-2 15:19

回复 20# LTSC1809

代码还有不少错误,和潜在的问题,请仔细看下7楼。

  1. @echo off
  2. ::耗时部分,每次取得前后两个时间,使用call :c
  3. set "str1=%time%"
  4. timeout 1
  5. set "str2=%time%"
  6. call :c "%str1%" "%str2%"
  7. pause
  8. :c
  9. set "t=%~1"
  10. set "t1=%~2"
  11. if "%t1:~,2%" lss "%t:~,2%" set "add=+24"
  12. set /a "times=(%t1:~,2%-%t:~,2%%add%)*360000+(1%t1:~3,2%%%100-1%t:~3,2%%%100)*6000+(1%t1:~6,2%%%100-1%t:~6,2%%%100)*100+(1%t1:~-2%%%100-1%t:~-2%%%100)"
  13. echo 检索时间 %times% ms
  14. exit /b
复制代码

作者: LTSC1809    时间: 2023-7-2 15:50

回复 21# 77七


    不瞒您说,这种百分号里括着引号,波浪号的到底是什么意思啊,我完全看不懂。很多地方都看不懂,就没法按照自己的需求改,所以最后索性放弃了,我还是再去看看教程吧[捂脸哭]
作者: LTSC1809    时间: 2023-7-2 16:05

我现在做了一个解释器,逐行获取文本里的内容并执行,使用call命令来调用它并传递两个参数,第一个参数是文件的路径(不包含文件本身名称),第二个参数是目标文件的名称,它还会读取文件路径下的config.ldt和doskey.ldt,其中config.ldt内容最后多了一个分号,后面跟着一个选项是用来决定是否显示加载中的文字和加载时间的,如果它为true,那么加载中提示和加载时间显示将会被关闭。现在它不能正常运行,会闪退,麻烦大家再帮我看看,代码如下:
  1. @echo off
  2. SetLocal EnableDelayedExpansion
  3. for /f "delims=; tokens=5" %%q in (%1\config.ldt) do if %%q equ true goto noecho
  4. rem time
  5. set "t=%time%"
  6. rem code
  7. rem time
  8. set "tx=%time%"
  9. rem code
  10. for /f "delims=; tokens=1" %%o in (%1\config.ldt) do set conls=%%o
  11. for /f "delims=' tokens=1" %%z in ("%conls%") do set modc=%%z
  12. for /f "delims=' tokens=2" %%e in ("%conls%") do set modl=%%e
  13. for /f "delims=; tokens=2" %%i in (%1\config.ldt) do set dbg=%%i
  14. for /f "delims=; tokens=3" %%t in (%1\config.ldt) do set dk=%%t
  15. for /f "delims=; tokens=4" %%b in (%1\config.ldt) do set color=%%b
  16. if %dbg% equ true (set dbgx=a) else (if %dbg% equ false (set dbgx=b) else (goto configerr))
  17. if %dk% equ true (set dkif=a) else (if %dk% equ false (set dkif=b) else (goto configerr))
  18. for %%i in (01,02,03,04,05,06,07,08,09,0a,0b,0c,0d,0e,0f,10,12,16,14,15,16,17,18,19,1a,1b,1c,1d,1e,1f,20,21,23,24,25,26,27,28,29,2a,2b,2c,2d,2e,2f,30,31,32,34,35,36,37,38,39,3a,3b,3c,3d,3e,3f,40,41,42,43,45,46,47,48,49,4a,4b,4c,4d,4e,4f50,51,52,53,54,56,57,58,59,5a,5b,5c,5d,5e,5f,60,61,62,63,64,65,67,68,69,6a,6b,6c,6d,6e,6f,70,71,72,73,74,75,76,77,78,79,7a,7b,7c,7d,7e,7f,80,81,82,83,84,85,86,87,89,8a,8b,8c,8d,8e,8f,90,91,92,93,94,95,96,97,98,9a,9b,9c,9d,9e,9f,a0,a1,a2,a3,a4,a5,a6,a7,a8,a9,ab,ac,ab,ac,ad,ae,af,b0,b1,b2,b3,b4,b5,b6,b7,b8,b9,ba,bc,bd,be,bf,c0,c1,c2,c3,c4,c5,c6,c7,c8,c9,ca,cb,cd,ce,cf,d0,d1,d2,d3,d4,d5,d6,d7,d8,d9,da,db,dc,de,df,e0,e1,e2,e3,e4,e5,e6,e7,e8,e9,ea,eb,ec,ed,ef,f0,f1,f2,f3,f4,f5,f6,f7,f8,f9,fa,fb,fc,fd,fe) do if %color% equ %%i goto colok
  19. goto configerr
  20. :colok
  21. rem time
  22. set "t1x=%time%"
  23. if "%t1x:~,2%" lss "%tx:~,2%" set "add=+24"
  24. set /a "timesx=(%t1x:~,2%-%tx:~,2%%add%)*360000+(1%t1x:~3,2%%%100-1%tx:~3,2%%%100)*6000+(1%t1x:~6,2%%%100-1%tx:~6,2%%%100)*100+(1%t1x:~-2%%%100-1%tx:~-2%%%100)" ,"ssx=(timesx/100)%%60","mmx=(timesx/6000)%%60","hhx=timesx/360000","msx=timesx%%100"
  25. rem endtime
  26. if %dkif% equ b goto nodk
  27. rem time
  28. set "tn=%time%"
  29. rem code
  30. for /f "delims=" %%a in (%cd%\doskey.ldt) do doskey %%a
  31. rem time
  32. set "t1n=%time%"
  33. if "%t1n:~,2%" lss "%tn:~,2%" set "add=+24"
  34. set /a "timesn=(%t1n:~,2%-%tn:~,2%%add%)*360000+(1%t1n:~3,2%%%100-1%tn:~3,2%%%100)*6000+(1%t1n:~6,2%%%100-1%tn:~6,2%%%100)*100+(1%t1n:~-2%%%100-1%tn:~-2%%%100)" ,"ssn=(timesn/100)%%60","mmn=(timesn/6000)%%60","hhn=timesn/360000","msn=timesn%%100"
  35. :nodk
  36. rem time
  37. set "t1=%time%"
  38. if "%t1:~,2%" lss "%t:~,2%" set "add=+24"
  39. set /a "times=(%t1:~,2%-%t:~,2%%add%)*360000+(1%t1:~3,2%%%100-1%t:~3,2%%%100)*6000+(1%t1:~6,2%%%100-1%t:~6,2%%%100)*100+(1%t1:~-2%%%100-1%t:~-2%%%100)" ,"ss=(times/100)%%60","mm=(times/6000)%%60","hh=times/360000","ms=times%%100"
  40. color %color%
  41. mode con cols=%modc% lines=%modl%
  42. if %dbgx% equ a goto dbg
  43. echo 加载文件:
  44. echo 加载配置文件...
  45. echo 完成 用时%hhx%:%mmx%:%ssx%.%msx%(时:分:秒.毫秒)
  46. echo 加载宏命令...
  47. echo 完成 用时%hhn%:%mmn%:%ssn%.%msn%(时:分:秒.毫秒)
  48. echo 加载完成 总用时%hh%:%mm%:%ss%.%ms%(时:分:秒.毫秒)
  49. echo.
  50. echo BetterCommand Interpreter development version 0.1a
  51. echo.
  52. for /f "delims=" %%r in (%1\%2) do (set /p !com!=<%1\%2
  53. if "!com!" equ help %cd%\help.txt
  54. if "!com!" equ exit exit
  55. if "!com!" equ shutdown (set /p shutyn=Shutdown now?[Y/N]:
  56. if "!shutyn!" equ y shutdown /s /t 0 /f)
  57. !com!)
  58. exit
  59. :configerr
  60. echo 配置文件可能存在问题,加载失败,请检查config.ldt。
  61. pause
  62. exit
  63. :dbg
  64. @echo on
  65. for /f "delims=" %%w in (%1\%2) do (set /p com=<%1\%2
  66. if "!com!" equ help %cd%\help.txt
  67. if "!com!" equ exit exit
  68. if "!com!" equ shutdown (set /p shutyn=Shutdown now?[Y/N]:
  69. if "!shutyn!" equ y shutdown /s /t 0 /f)
  70. !com!)
  71. exit
  72. exit
  73. pause
  74. :noecho
  75. for /f "delims=; tokens=1" %%o in (%1\config.ldt) do set conls=%%o
  76. for /f "delims=' tokens=1" %%z in ("%conls%") do set modc=%%z
  77. for /f "delims=' tokens=2" %%e in ("%conls%") do set modl=%%e
  78. for /f "delims=; tokens=2" %%i in (%1\config.ldt) do set dbg=%%i
  79. for /f "delims=; tokens=3" %%t in (%1\config.ldt) do set dk=%%t
  80. for /f "delims=; tokens=4" %%b in (%1\config.ldt) do set color=%%b
  81. if %dbg% equ true (set dbgx=a) else (if %dbg% equ false (set dbgx=b) else (goto configerr))
  82. if %dk% equ true (set dkif=a) else (if %dk% equ false (set dkif=b) else (goto configerr))
  83. for %%i in (01,02,03,04,05,06,07,08,09,0a,0b,0c,0d,0e,0f,10,12,16,14,15,16,17,18,19,1a,1b,1c,1d,1e,1f,20,21,23,24,25,26,27,28,29,2a,2b,2c,2d,2e,2f,30,31,32,34,35,36,37,38,39,3a,3b,3c,3d,3e,3f,40,41,42,43,45,46,47,48,49,4a,4b,4c,4d,4e,4f50,51,52,53,54,56,57,58,59,5a,5b,5c,5d,5e,5f,60,61,62,63,64,65,67,68,69,6a,6b,6c,6d,6e,6f,70,71,72,73,74,75,76,77,78,79,7a,7b,7c,7d,7e,7f,80,81,82,83,84,85,86,87,89,8a,8b,8c,8d,8e,8f,90,91,92,93,94,95,96,97,98,9a,9b,9c,9d,9e,9f,a0,a1,a2,a3,a4,a5,a6,a7,a8,a9,ab,ac,ab,ac,ad,ae,af,b0,b1,b2,b3,b4,b5,b6,b7,b8,b9,ba,bc,bd,be,bf,c0,c1,c2,c3,c4,c5,c6,c7,c8,c9,ca,cb,cd,ce,cf,d0,d1,d2,d3,d4,d5,d6,d7,d8,d9,da,db,dc,de,df,e0,e1,e2,e3,e4,e5,e6,e7,e8,e9,ea,eb,ec,ed,ef,f0,f1,f2,f3,f4,f5,f6,f7,f8,f9,fa,fb,fc,fd,fe) do if %color% equ %%i goto colok
  84. goto configerr
  85. :colokn
  86. if %dkif% equ b goto nodk
  87. for /f "delims=" %%a in (%1\doskey.ldt) do doskey %%a
  88. :nodkn
  89. for /f "delims=" %%y in (%1\%2) do (set /p com=<%1\%2
  90. if "!com!" equ help %cd%\help.txt
  91. if "!com!" equ exit exit
  92. if "!com!" equ shutdown (set /p shutyn=Shutdown now?[Y/N]:
  93. if "!shutyn!" equ y shutdown /s /t 0 /f)
  94. !com!)
  95. exit
  96. :configerrn
  97. echo 配置文件可能存在问题,加载失败,请检查config.ldt。
  98. pause
  99. exit
  100. :dbgn
  101. @echo on
  102. for /f "delims=" %%u in (%1\%2) do (set /p com=<%1\%2
  103. if "!com!" equ help %1\%2
  104. if "!com1" equ exit exit
  105. if "!com!" equ shutdown (set /p shutyn=Shutdown now?[Y/N]:
  106. if "!shutyn!" equ y shutdown /s /t 0 /f)
  107. !com!
  108. pause)
  109. exit
复制代码

作者: Batcher    时间: 2023-7-2 21:07

回复 22# LTSC1809


参考:





欢迎光临 批处理之家 (http://bbs.bathome.net/) Powered by Discuz! 7.2