[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖

[文本处理] [分享]用批处理+vbs写的简易文本数据库(初级版本)

文本数据库会被程序加载到内存中进行处理,因此运行快来还是挺快的,但加载的速度会比较慢,所以数据库的大小请保持在500kb以下。
源代码如下:
  1. @echo off
  2. color 80
  3. path=c:\windows\system32\;%cd%
  4. setlocal enabledelayedexpansion
  5. if not "%1"=="" set file=%1 & goto start
  6. set /p file=请输入要新建或加载的文本数据库,注意路径中不能有引号:
  7. if %file%==q exit
  8. :start
  9. if not exist %file% echo.>%file%
  10. for /f %%a in (%file%) do (
  11.     set /a lines+=1
  12. )
  13. if "%lines%"=="" goto mysj
  14. if %lines%==0 goto mysj
  15. findstr "PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH" %file%>%temp%\tmp
  16. for /f %%a in (%temp%\tmp) do (if not "%%a"=="" (goto start1) else goto start0)
  17. :start0
  18. set geshi=总线型
  19. title 总线型
  20. del %temp%\tmp
  21. set lines=0
  22. if not exist %file% echo.>%file%
  23. call :start01 %file% 正在加载数据
  24. echo 数据加载完成!
  25. ping -n 2 127.0>nul
  26. set status2=1
  27. goto readloop1
  28. :start1
  29. title 变量型
  30. set geshi=变量型
  31. del %temp%\tmp
  32. if not exist %file% echo.>%file%
  33. set lines=0
  34. for /f "" %%a in (%file%) do (
  35.     set /a lines+=1
  36. )
  37. for /f " tokens=1* delims==" %%a in (%file%) do (
  38.     set /a var80+=1
  39.     set /a var90=!var80!*100/%lines%
  40.     cls
  41.     echo 正在加载文件...!var90!%%
  42.     set %%a=%%b
  43. )
  44. echo 数据加载完成!
  45. ping -n 2 127.0>nul
  46. if "%a1%"=="" goto mysj
  47. if %a1%==0 goto mysj
  48. set status2=1
  49. :readloop1
  50. for /l %%a in (%status2%,1,%a1%) do (
  51.         set lines=0
  52.         cls
  53.         call :showtopic %%a & call :readloop2 %%a
  54.     )
  55. goto readloop1
  56. :msgbox
  57. echo a="第" ^& %1 ^& "条:" ^& %2>%temp%\TMP.vbs
  58. echo c=%4>>%temp%\TMP.vbs
  59. echo msgbox a,%3,c>>%temp%\TMP.vbs
  60. %temp%\TMP.vbs
  61. del %temp%\TMP.vbs
  62. goto :eof
  63. :addneiyong
  64. call :add 新增内容 请键入您要添加的新内容
  65. for /f "tokens=* delims=" %%a in (%temp%\tmp) do (set /a topic%1lines+=1 & set /a num0=%2+1 & set topic%1neiyong!num0!=%%a
  66. set xiugai=y)
  67. del %temp%\tmp
  68. goto :eof
  69. :baochun
  70. call :geshi
  71. if %geshi%==总线型 (
  72. set num3=0
  73. set num4=0
  74. del %file%
  75. for /l %%a in (1,1,%a1%) do (
  76.          cls
  77.          set /a num3+=1
  78.          set /a num4=!num3!*100/%a1%
  79.          echo 保存进度...!num4!%%
  80.     echo ----------▋!topic%%a!>>%file%
  81.     for /l %%b in (1,1,!topic%%alines!) do (
  82.          
  83.          echo !topic%%aneiyong%%b!>>%file%
  84.       )
  85.    )
  86. call :msgbox1 "内存中的数据已经存入文件!" 0 "当前文件:%file%"
  87. set xiugai=n
  88. )
  89. if %geshi%==变量型  (
  90. del %file%
  91. set >%file%
  92. call :msgbox1 "内存中的数据已经存入文件!" 0 "当前文件:%file%"
  93. set xiugai=n
  94. )
  95. goto :eof
  96. :msgbox1
  97. echo a=%1>%temp%\TMP.vbs
  98. echo c=%3>>%temp%\TMP.vbs
  99. echo msgbox a,%2,c>>%temp%\TMP.vbs
  100. %temp%\TMP.vbs
  101. del %temp%\TMP.vbs
  102. goto :eof
  103. :readloop2
  104. set status2=1
  105. for /l %%b in (1,1,!topic%1lines!) do (
  106.         if !lines!==300 (
  107.         call :gongneng %1
  108.         cls
  109.         call :showtopic %1
  110.         set lines=0
  111.         )
  112.         set /a lines+=1
  113.         call echo 第%%b条:!topic%1neiyong%%b:~0,300!
  114.         )
  115.         call :gongneng %1
  116. goto :eof
  117. :shanchu
  118. if "!topic%2lines!"=="" call :meiyouxingxi & goto :eof
  119. if "!topic%2lines!"=="0" call :meiyouxingxi & goto :eof
  120. set num3=0
  121. set num4=0
  122. set Tmp0=%1
  123. set Tmp1=!topic%2neiyong%1!
  124. for /l %%b in (1,1,!topic%2lines!) do (
  125.     set /a num2=!topic%2lines!+1
  126.          cls
  127.          set /a num1=%%b+1
  128.          set /a num3+=1
  129.          set /a num4=!num3!*100/!topic%2lines!
  130.          echo 删除进度...!num4!%%
  131.          if %%b GEQ %1 call set "topic%2neiyong%%b=%%topic%2neiyong!num1!%%"   
  132.   )
  133. set /a topic%2lines-=1
  134. set topic%2neiyong%3=
  135. call :msgbox "!Tmp0!" "!Tmp1!" 0 "删除完毕"
  136. set xiugai=y
  137. set Tmp0=
  138. set Tmp1=
  139. set num1=
  140. set num2=
  141. goto :eof
  142. :shanchubiaoti
  143. set num0=0
  144. if "%a1%"=="" call :meiyoubiaoti & goto :eof
  145. if "%a1%"=="0" call :meiyoubiaoti & goto :eof
  146. set str11=!topic%1!
  147. for /l %%a in (1,1,!a1!) do (
  148.         set /a num1=%%a*100/!a1!
  149.         set /a var0=%%a+1
  150.         if %%a GEQ %1 (call set "topic%%a=%%topic!var0!%%"
  151.         call set var1=%%topic!var0!lines%%
  152.         set "topic%%alines=!var1!" )
  153.     for /l %%b in (1,1,!var1!) do (
  154.         if %%a GEQ %1 call set "topic%%aneiyong%%b=%%topic!var0!neiyong%%b%%"
  155.   )
  156. cls
  157.         echo 正在删除标题%1:!str11!...!num1!%%
  158. )
  159. for /l %%a in (1,1,!topic%a1%lines!) do (set topic%a1%neiyong%%a=)
  160. set topic%a1%lines=
  161. set /a a1=!a1!-1
  162. call :msgbox1 "标题%1:!str11!" 0 "删除完毕"
  163. set xiugai=y
  164. goto :eof
  165. :check
  166. if not %xiugai%==y exit
  167. echo.>%temp%\tmp
  168. echo response=msgbox^("您有未保存的信息!是否保存?",4,"退出提示"^)>%temp%\TMP.vbs
  169. echo Const ForReading = 1, ForWriting = 2 >>%temp%\TMP.vbs
  170. echo Dim fso, f>>%temp%\TMP.vbs
  171. echo set fso=createobject^("scripting.filesystemobject"^).opentextfile^("%temp%\tmp",2^)>>%temp%\TMP.vbs
  172. echo fso.Write response >>%temp%\TMP.vbs
  173. call %temp%\TMP.vbs
  174. del %temp%\TMP.vbs
  175. if %xiugai%==y for /f %%a in (%temp%\tmp) do (
  176. if %%a == 6 call :baochun
  177. )
  178. del %temp%\tmp
  179. exit
  180. goto :eof
  181. :gongneng
  182.         echo -----------------------------------------------------------------------------------------------------
  183.         echo 功能选项(所有命令均忽略大小写,以上所显示均为简略信息):
  184.         echo 回车:往后翻页 D:删除本标题下所有数据 条数+D:删除数据 DR:导入 N:新增内容 Q:退出程序 P:上一标题
  185.         echo 条数+空格+ZE:进入与某条信息相同的标题 条数+空格+ZN:建立与某条信息相同的子标题
  186.         echo S:保存数据 SO:搜寻 T:跳转 TD:删除标题 TN:新增标题 TX:修改标题 条数+空格+W:详细信息 条数+空格+X:修改信息
  187.         set choice=
  188.         set /p choice=请输入操作条码:
  189.         if /i !choice! == TD call :shanchubiaoti %1
  190.         if /i !choice! == T call :tiaozhuan
  191.         if /i !choice! == P call :pre %1
  192.         if /i !choice! == DR call :daoru
  193.         if /i !choice! == SO call :soxun
  194.         if /i !choice! == TN call :addbiaoti
  195.         if /i !choice! == TX call :xiugaibiaoti %1
  196.         if /i !choice! == D call :shanchuall %1
  197.         if /i !choice! == N call :addneiyong %1 !topic%1lines!
  198.         if /i !choice! == S call :baochun
  199.         if /i !choice! == Q goto check
  200.         if not "!choice!"=="" for /f "tokens=1-2 delims= " %%c in ("!choice!") do (
  201.                    if not "%%d"=="" (
  202.                    if "!topic%1neiyong%%c!"=="" call :msgbox1 "对不起,不存在此条信息,无法使用此功能!" 0 "出错提示" & set status2=%1 & goto readloop1
  203.                    if /i %%d == W call call :msgbox "%%c" "!topic%1neiyong%%c!" 0 "当前文件:%file%"
  204.                    if /i %%d == D call :shanchu %%c %1 !topic%1lines!
  205.                    if /i %%d == X call :xiugai %1 %%c
  206.                    if /i %%d == ZE call :findzi %1 %%c
  207.                    if /i %%d == ZN call :addzi %1 %%c
  208.     )
  209. )
  210. if not "!choice!"=="" if %1 LEQ !a1! set status2=%1 & goto readloop1
  211. if not "!choice!"=="" if not %1 LSS !a1! set /a status2=%1-1 & goto readloop1
  212. goto :eof
  213. :shanchuall
  214. if "!topic%1lines!"=="" call :meiyouxingxi & goto :eof
  215. if "!topic%1lines!"=="0" call :meiyouxingxi & goto :eof
  216. set num3=
  217. for /l %%b in (1,1,!topic%1lines!) do (
  218.          cls
  219.          set /a num3+=1
  220.          set /a num4=!num3!*100/!topic%1lines!
  221.          echo 删除进度...!num4!%%
  222.          set topic%1neiyong%%b=
  223.   )
  224. set topic%1lines=0
  225. call :msgbox1 "标题%1的所有信息已删除完毕!" 0 "删除完毕"
  226. set xiugai=y
  227. goto :eof
  228. :xiugai
  229. if "!topic%1lines!"=="" call :meiyouxingxi & goto :eof
  230. if "!topic%1lines!"=="0" call :meiyouxingxi & goto :eof
  231. call :msgbox1 "!topic%1neiyong%2!" 0 "修改前的内容"
  232. set str0=!topic%1neiyong%2!
  233. echo i = InputBox ^("修改标题%1的第%2条信息",VbOnlyOk,"!topic%1neiyong%2!"^)>%temp%\TMP.vbs
  234. call :xiugai1
  235. for /f "tokens=* delims=" %%a in (%temp%\tmp) do (set topic%1neiyong%2=%%a
  236. set xiugai=y)
  237. del %temp%\tmp
  238. call :msgbox1 "标题%1的第%2条信息已被修改为!topic%1neiyong%2!" 0 "修改完毕"
  239. goto :eof
  240. :meiyouxingxi
  241. call :msgbox1 "对不起,本标题下无信息,不能使用此功能!" 0 "提示"
  242. goto :eof
  243. :addbiaoti
  244. call :add 新增标题 请键入您要添加的新标题
  245. for /f "tokens=* delims=" %%a in (%temp%\tmp) do (set /a a1+=1 & set topic!a1!=%%a
  246. set xiugai=y)
  247. del %temp%\tmp
  248. call :msgbox1 "标题新增完成!" 0 "提示"
  249. goto :eof
  250. :add
  251. echo i = InputBox ^("%1",VbOnlyOk, "%2"^)>%temp%\TMP.vbs
  252. call :xiugai1
  253. goto :eof
  254. :showtopic
  255.         if "!topic%1lines!"=="" set topic%1lines=0
  256.         if %a1%==0 call :msgbox1 "标题总数不能为0,程序将在保存数据后重启" 0 "错误提示" & call :baochun & start 简易数据库.cmd & exit
  257.         title 已加载文件:%file% 类型:%geshi% 当前为标题%1/总有%a1%个标题 本标题共有!topic%1lines!条信息
  258.         echo 标题%1  :!topic%1!
  259.         echo -----------------------------------------------------------------------------------------------------
  260. goto :eof
  261. :xiugaibiaoti
  262. if "!a1!"=="" call :meiyoubiaoti & goto :eof
  263. if "!a1!"=="0" call :meiyoubiaoti & goto :eof
  264. call :msgbox1 "!topic%1!" 0 "修改前的标题"
  265. set str0=!topic%1!
  266. echo i = InputBox ^("修改标题%1",VbOnlyOk,"!topic%1!"^)>%temp%\TMP.vbs
  267. call :xiugai1
  268. for /f "tokens=* delims=" %%a in (%temp%\tmp) do (set topic%1=%%a
  269. set xiugai=y)
  270. del %temp%\tmp
  271. call :msgbox1 "标题%1已被修改为!topic%1!" 0 "修改完毕"
  272. goto :eof
  273. :xiugai1
  274. echo.>%temp%\tmp
  275. echo Const ForReading = 1, ForWriting = 2>>%temp%\TMP.vbs
  276. echo Dim fso, f>>%temp%\TMP.vbs
  277. echo set fso=createobject^("scripting.filesystemobject"^).opentextfile^("%temp%\tmp",2^)>>%temp%\TMP.vbs
  278. echo fso.Write i >>%temp%\TMP.vbs
  279. call %temp%\TMP.vbs
  280. del %temp%\TMP.vbs
  281. goto :eof
  282. :meiyoubiaoti
  283. call :msgbox1 "对不起,标题总数为0,不能使用此功能!" 0 "提示"
  284. goto :eof
  285. :tiaozhuan
  286. echo i = InputBox ^("您要跳转到第几个标题",VbOnlyOk,"跳转"^)>%temp%\TMP.vbs
  287. call :xiugai1
  288. for /f %%a in (%temp%\tmp) do (if %%a LEQ %a1% (set status2=%%a & del %temp%\tmp &goto readloop1) else call :msgbox1 "对不起,数值超出范围!" 0 "出错提示" )
  289. goto :eof
  290. :clear
  291. for /l %%a in (1,1,%a1%) do (
  292.     for /l %%b in (1,1,!topic%%alines!) do (
  293.          set topic%%aneiyong%%b=
  294.       )
  295.    )
  296. set a1=0
  297. set topic%%alines=
  298. goto :eof
  299. :geshi
  300. set geshi=总线型
  301. echo i = msgbox ^("请选择数据库格式,选'是'为总线型,选'否'为变量型",vbyesno,"提示"^)>%temp%\TMP.vbs
  302. call :xiugai1
  303. for /f %%a in (%temp%\tmp) do (if %%a==6 set geshi=总线型
  304.     if %%a==7 set geshi=变量型)
  305. call :msgbox1 "您选择的数据库类型为'%geshi%'" 0 "提示"
  306. del %temp%\tmp
  307. goto :eof
  308. :mysj
  309. call  :msgbox1 "数据库还没有信息,请您新增标题!" 0 "警告" & call :addbiaoti & call :baochun & call :clear & if !geshi!==总线型 (goto start0) else goto start1
  310. goto :eof
  311. :soxun
  312. if "!a1!"=="" call :meiyoubiaoti & goto :eof
  313. if "!a1!"=="0" call :meiyoubiaoti & goto :eof
  314. del %temp%\tmp
  315. echo i = InputBox ^("请输入您要搜索的内容。注意:搜索结果过多会遗漏信息^!",VbOnlyOk,"搜索"^)>%temp%\TMP.vbs
  316. call :xiugai1
  317. for /f %%a in (%temp%\tmp) do (set soxun=%%a)
  318. del %temp%\tmp
  319. set| findstr "%soxun%" |findstr "=" |findstr "topic">>%temp%\tmp
  320. set var91=
  321. set str14=
  322. set str13=
  323. for /f "tokens=1* delims==" %%a in (%temp%\tmp) do (
  324.     set var91=%%a
  325.     set var91=!var91:topic=标题!
  326.     set var91=!var91:neiyong=信息!
  327.     set str14=%%b
  328.     set str14=!str14:~0,200!
  329.     set str13=!str13! "!var91!:!str14!" ^& vbnewline ^&
  330.     )
  331. echo a=!str13! vbnewline>%temp%\TMP.vbs
  332. echo msgbox a,0,"搜寻结果">>%temp%\TMP.vbs
  333. %temp%\TMP.vbs
  334. del %temp%\TMP.vbs
  335. del %temp%\tmp
  336. call :msgbox1 "结果显示完毕!" 0 "提示"
  337. goto :eof
  338. :daoru
  339. echo 请将要导入的文件拖入窗口中,不能有引号,文件名后加" 0"可导入数据库文件
  340. set /p file1=文件:
  341. for /f "tokens=1-2 delims= " %%a in ("%file1%") do (
  342.         set lines=0
  343.         set lines0=0
  344.         set per0=0
  345.         set per=0
  346.         set a2=0
  347.     title %%a
  348.     if "%%b"=="" (
  349.         set /a a1+=1
  350.         set topic!a1!=%file1%
  351.         for /f "" %%a in (%file1%) do (
  352.             set /a lines+=1
  353.             )
  354.        if "!lines!"=="" call :mysl & goto :eof
  355.       if !lines!==0 call :mysl & goto :eof
  356.         for /f "tokens=* delims=" %%f in (%file1%) do (
  357.             echo 正在读取数据...!per!%%
  358.             set a=%%f
  359.             set /a a2+=1 & set topic!a1!lines=!a2! & set topic!a1!neiyong!a2!=%%f
  360.             set /a lines0+=1
  361.             set /a per0=!lines0!*100/!lines!
  362.             set /a per=!per0!+1
  363.             cls
  364.             )
  365.      )
  366.     if %%b==0 (
  367.         findstr "PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH" %%a>%temp%\tmp
  368.         set lines=0
  369.         for /f %%z in (%temp%\tmp) do (set /a lines+=1)
  370.         if !lines!==0 (call :start01 %%a 正在导入数据) else call :start11 %%a
  371.     )
  372. )
  373. call :msgbox1 "文件已导入完毕" 0 "提示"
  374. set xiugai=y
  375. del %temp%\tmp
  376. goto :eof
  377.         :start01
  378.         for /f "" %%g in (%1) do (
  379.             set /a lines+=1
  380.         )
  381.       if "!lines!"=="" call :mysl & goto :eof
  382.       if !lines!==0 call :mysl & goto :eof
  383.         if %2==正在导入数据 call :msgbox1 "要导入的数据库为总线型" 0 "提示"
  384.         for /f " tokens=1* delims=▋" %%h in (%1) do (
  385.             echo %2...!per!%%
  386.             set a=%%h
  387.             if "!a:~0,10!" == "----------" set a2=0 & set /a a1+=1 & set topic!a1!=%%i
  388.             if not "!a:~0,10!" == "----------" set /a a2+=1 & set topic!a1!lines=!a2! & set topic!a1!neiyong!a2!=%%h
  389.             set /a lines0+=1
  390.             set /a per0=!lines0!*100/!lines!
  391.             set /a per=!per0!+1
  392.             cls
  393.        )
  394. goto :eof
  395.          :start11
  396. set var23=!a1!
  397. set var80=
  398. set var90=
  399. set var43=
  400. for /f "" %%i in (%1) do (
  401.     set /a lines+=1
  402. )
  403.       if "!lines!"=="" call :mysl & goto :eof
  404.       if !lines!==0 call :mysl & goto :eof
  405. call :msgbox1 "要导入的数据库为变量型" 0 "提示"
  406. for /f " tokens=1,* delims==" %%n in (%1) do (
  407.     if %%n==a1 set au=%%o
  408. )
  409. for /f " tokens=1,* delims==" %%i in (%1) do (
  410.     set /a var80+=1
  411.     set /a var90=!var80!*100/%lines%
  412.     cls
  413.     echo 正在导入数据...!var90!%%
  414.     for /l %%l in (1,1,!au!) do (
  415.    
  416.     set /a var43=%%l+!a1!
  417.     if %%i==topic%%l set topic!var43!=%%j
  418.     if %%i==topic%%llines set topic!var43!lines=%%j & call set tmp67=%%topic!var43!lines%%
  419.     for /l %%m in (1,1,!tmp67!) do (
  420.            if %%i==topic%%lneiyong%%m set topic!var43!neiyong%%m=%%j
  421.         )
  422.     )
  423.   )
  424. set /a a1=!a1!+!au!
  425. goto :eof
  426. :mysl
  427. call :msgbox1 "要导入的文件不存在或没有数据!" 0 "出错"
  428. goto :eof
  429. :pre
  430. if %1 == 1 call :msgbox1 "已到最前页!" 0 "提示" & goto :eof
  431. set /a status2=%1-1
  432. goto readloop1
  433. :findzi
  434. for /l %%j in (1,1,%a1%) do (if "!topic%%j!"=="!topic%1neiyong%2!" set status2=%%j & goto readloop1)
  435. call :msgbox1 "对不起,不存在名称为!topic%1neiyong%2!的子标题!" 0 "出错提示"
  436. goto :eof
  437. :addzi
  438. set /a a1+=1
  439. call set topic%a1%=!topic%1neiyong%2!
  440. call :msgbox1 "建立同名标题!topic%1neiyong%2!完成!" 0 "提示"
  441. goto :eof
  442. goto :eof
复制代码
本程序所用的数据库格式如下:


(一)总线型


----------▋请键入您要添加的新标题
1
2
3
----------▋12
123


(二)变量型


a=----------
a1=2
a2=0
ALLUSERSPROFILE=M:\Documents and Settings\All Users
APPDATA=M:\Documents and Settings\Administrator\Application Data
choice=s
CLIENTNAME=Console
CommonProgramFiles=C:\Program Files\Common Files
COMPUTERNAME=WWW-F674FB526B4
ComSpec=M:\windows\system32\cmd.exe
file=data.mdb
FP_NO_HOST_CHECK=NO
geshi=变量型
HOMEDRIVE=M:
HOMEPATH=\Documents and Settings\Administrator
lines=1
lines0=1
LOGONSERVER=\\WWW-F674FB526B4
num0=1
num3=2
num4=100
NUMBER_OF_PROCESSORS=2
OS=Windows_NT
Path=c:\windows\system32\;M:\Documents and Settings\Administrator\桌面
PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH
per=101
per0=100
PROCESSOR_ARCHITECTURE=x86
PROCESSOR_IDENTIFIER=x86 Family 6 Model 15 Stepping 13, GenuineIntel
PROCESSOR_LEVEL=6
PROCESSOR_REVISION=0f0d
ProgramFiles=C:\Program Files
PROMPT=$P$G
SESSIONNAME=Console
status2=1
SystemDrive=M:
SystemRoot=M:\windows
TEMP=Z:\Temp
TMP=Z:\Temp
topic1=请键入您要添加的新标题
topic1lines=3
topic1neiyong1=1
topic1neiyong2=2
topic1neiyong3=3
topic2=12
topic2lines=1
topic2neiyong1=123

USERDOMAIN=WWW-F674FB526B4
USERNAME=Administrator
USERPROFILE=M:\Documents and Settings\Administrator
windir=M:\windows
xiugai=n

用ping就没有意思了

TOP

返回列表