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

[其他] [分享]批处理对ini格式文件的操作

本帖最后由 yiwuyun 于 2014-12-8 21:49 编辑

一直想找个操作ini格式的批纯处理不成,但有时又想用,由于近来无事,就写了一个,作为一个分享吧。

OpenFile:需要一个参数,表明要打开的ini格式文件,它可以是个空文件。这个命令会对ini文件进行语法分析,不合法的去掉,留下合法的
如果有两个及以上相同的section(节),它会合并这两个section为一个。如果一个section有两个及以上相同的儿子,它会合并相同的儿子为
一个,后面的儿子会覆盖前面儿子的值。这个命令很费时,一般在一个批处理文件中只执行一次。
SaveToFile:需要一个参数,表明要保存到哪个文件。可以执行多次,但一般也只最后才执行一次,如果不执行,肯定不会保存文件的。
UpdateChildOfSection:需要三个参数,分别是section,childName,childValue.section是[]括起的部分,另两个分别是=左右两个
AddSectionAndChild:可以有一个参数,表明增加一个空的section,或者三个参数,分别是section,childName,childValue。childValue是
=右边的那部分,如果有空格,要用""括起。
DeleteChildOfSection:需要二个参数,section,childName.表示要删除section下的childName.
DeleteSection:需要一个参数,section.表示删除section和其下的所有儿子。
GetChildValueOfSection:设置变量childValueOfSection的置为指定section下的儿子的值。childValueOfSection在接下来的程序中可使用。
test.bat 中比较详细的介绍了这些命令的用法。以及怎样使用。在调用general.bat之前,应该先设定延迟变量。否则会出错。

以下为test.bat的代码:
  1. @echo off
  2. cls
  3. echo author: JTSYZX-yiwuyun 2014.12.08
  4. echo usage: call .\general.bat :Command File
  5. echo usage: call .\general.bat :Command SectionName [ChildName [ChildValue]]
  6. echo usage: Command: OpenFile,SaveToFile
  7. echo usage: Command: AddSectionAndChild,UpdateChildOfSection,DeleteChildOfSection,DeleteSection,GetChildValueOfSection
  8. set __file_yi=test.txt
  9. setlocal EnableDelayedExpansion
  10. setlocal ENABLEEXTENSIONS
  11. call .\general.bat :OpenFile !__file_yi!
  12. call .\general.bat :AddSectionAndChild name
  13. call .\general.bat :SaveToFile !__file_yi!
  14. type !__file_yi!
  15. echo AddSectionAndChild name
  16. pause
  17. call .\general.bat :AddSectionAndChild name author yiwuyun
  18. call .\general.bat :SaveToFile !__file_yi!
  19. type !__file_yi!
  20. echo AddSectionAndChild name author yiwuyun
  21. pause
  22. call .\general.bat :AddSectionAndChild name yi wuyun
  23. call .\general.bat :AddChildOfSection name fu futongzhen
  24. call .\general.bat :AddSectionAndChild office word true
  25. call .\general.bat :AddSectionAndChild office excel true
  26. call .\general.bat :SaveToFile !__file_yi!
  27. type !__file_yi!
  28. echo AddSectionAndChild name yi wuyun
  29. echo AddChildOfSection name fu futongzhen
  30. echo AddSectionAndChild office word true
  31. echo AddSectionAndChild office excel true
  32. pause
  33. call .\general.bat :UpdateChildOfSection office excel false
  34. call .\general.bat :SaveToFile !__file_yi!
  35. type !__file_yi!
  36. echo UpdateChildOfSection office excel false
  37. pause
  38. call .\general.bat :DeleteChildOfSection name yi
  39. call .\general.bat :SaveToFile !__file_yi!
  40. type !__file_yi!
  41. echo DeleteChildOfSection name yi
  42. pause
  43. call .\general.bat :DeleteSection office
  44. call .\general.bat :SaveToFile !__file_yi!
  45. type !__file_yi!
  46. echo DeleteSection office
  47. pause
  48. call .\general.bat :GetChildValueOfSection name author
  49. echo GetChildValueOfSection name author
  50. echo author:!childValueOfSection!
  51. pause
  52. endlocal
  53. exit /b
  54. @echo on
复制代码
以下为操作ini的主体代码。文件名为:general.bat
  1. @echo off
  2. set __yiwuyun.txt=___yiwuyun.txt
  3. call %1 %2 %3 %4
  4. exit /b
  5. @echo on
  6. :GetLineCount
  7. setlocal
  8. set /a n=0
  9. for /f  %%a in ('type %1') do (
  10. set /a n=!n!+1
  11. )
  12. set lineCount=!n!
  13. endlocal&set lineCount=%lineCount%
  14. exit /b 0
  15. :SaveToFile
  16. if not "%1"=="" (
  17.   set string=%1
  18. )
  19. if "!string!"=="" exit /b 1
  20. type nul>!string!
  21. for /l %%a in (1,1,!__sectionCount!) do (
  22.   if "!__section%%a.controlOutput!"=="0" (
  23.      echo [!__section%%a.name!]>>!string!
  24.      for /l %%b in (1,1,!__section%%a.childCount!) do (
  25.         if "!__section%%a.child%%b.controlOutput!"=="0" (
  26.            echo !__section%%a.child%%b.name!=!__section%%a.child%%b.value!>>!string!
  27.         )
  28.      )
  29.      echo;>>!string!
  30.   )
  31. )
  32. exit /b 0
  33. :OpenFile
  34. echo author: JTSYZX-yiwuyun 2014.12.08
  35. echo;
  36. echo;
  37. echo;
  38. if "%1"=="" (
  39.   echo need a file.
  40.   exit /b 1
  41. )
  42. if exist %1 (
  43.   call :GetSectionNAndCount %1
  44. ) else (
  45.   type nul>%1
  46.   call :GetSectionNAndCount %1
  47.   if exist %1 del %1
  48. )
  49. exit /b 0
  50. :GetSectionNAndCount
  51. setlocal
  52. set string=!string!
  53. set /a nChild=0
  54. set /a n=1
  55. set /a __sectionNumber=1
  56. for /f "tokens=1 delims=" %%a in ('type %1') do (
  57.   set string=%%a
  58.   call :IsSpaceLine
  59.   if "!isSpaceLine!"=="true" (
  60.     echo This is space line: !n!
  61.     echo This line will be deleted.
  62.   ) else (
  63.      call :IsLegalSection
  64.      if "!__section.validity!"=="true" (
  65.         if !nChild! gtr 0 set /a nChild=0
  66.         set __section!__sectionNumber!.name=!__section.name!
  67.         set /a __section!__sectionNumber!.lineNumber=!n!
  68.         set __section!__sectionNumber!.validity=true
  69.         set /a __section!__sectionNumber!.childCount=!nChild!
  70.         set /a nTemp=!__sectionNumber!
  71.         set /a __sectionNumber=!__sectionNumber!+1
  72.      ) else (
  73.         if !__sectionNumber! lss 2 (
  74.              echo SectionNumber lss 2
  75.              echo Section should be the first.
  76.              echo This line will be deleted.
  77.         ) else (
  78.            call :IsLegalEqualityExpression
  79.            if "!isLegalEqualityExpression!"=="true" (
  80.               set /a nChild=!nChild!+1
  81.               set __section!nTemp!.child!nChild!.name=!stringEqualityBefore!
  82.               set __section!nTemp!.child!nChild!.value=!stringEqualityAfter!
  83.               set __section!nTemp!.childCount=!nChild!
  84.            ) else (
  85.               echo This is line:!n! this is not legal line.
  86.               echo This line will be deleted.
  87.            )
  88.         )
  89.      )
  90.   )
  91.   set /a n=!n!+1   
  92. )
  93. set /a __sectionCount=!__sectionNumber!-1
  94. type nul>!__yiwuyun.txt!
  95. echo set /a __sectionCount=!__sectionCount!>>!__yiwuyun.txt!
  96. set /a controlOutput=0
  97. for /l %%a in (1,1,!__sectionCount!) do (
  98.    echo set __section%%a.name=!__section%%a.name!>>!__yiwuyun.txt!
  99.    echo set __section%%a.lineNumber=!__section%%a.lineNumber!>>!__yiwuyun.txt!
  100.    echo set __section%%a.validity=!__section%%a.validity!>>!__yiwuyun.txt!
  101.    echo set /a __section%%a.controlOutput=!controlOutput!>>!__yiwuyun.txt!
  102.    echo set /a __section%%a.childCount=!__section%%a.childCount!>>!__yiwuyun.txt!
  103.       for /l %%b in (1,1,!__section%%a.childCount!) do (
  104.          echo set __section%%a.child%%b.name=!__section%%a.child%%b.name!>>!__yiwuyun.txt!
  105.          echo set __section%%a.child%%b.value=!__section%%a.child%%b.value!>>!__yiwuyun.txt!
  106.          echo set /a __section%%a.child%%b.controlOutput=!controlOutput!>>!__yiwuyun.txt!
  107.       )
  108. )
  109. endlocal
  110. for /f "tokens=1 delims=" %%a in ('type !__yiwuyun.txt!') do (
  111.    rem echo %%a
  112.    %%a
  113. )
  114. if exist !__yiwuyun.txt! del !__yiwuyun.txt!
  115. call :MergeSameSection
  116. call :MergeSameChild
  117. exit /b 0
  118. :MergeSameSection
  119. setlocal
  120. for /l %%a in (1,1,!__sectionCount!) do (
  121.   if "!__section%%a.controlOutput!"=="0" (
  122.     set /a n=%%a+1
  123.     for /l %%b in (!n!,1,!__sectionCount!) do (
  124.        if /i "!__section%%a.name!"=="!__section%%b.name!" (
  125.              set /a __section%%b.controlOutput=%%a
  126.              rem echo __section%%b.controlOutput:!__section%%b.controlOutput!
  127.              set /a startNumber=!__section%%a.childCount!+1
  128.              set /a endNumber=!__section%%a.childCount!+!__section%%b.childCount!
  129.              for /l %%c in (!startNumber!,1,!endNumber!) do (
  130.                     set /a temp=%%c-!__section%%a.childCount!
  131.                     for /l %%d in (!temp!,1,!temp!) do (
  132.                       set __section%%a.child%%c.name=!__section%%b.child%%d.name!
  133.                       set __section%%a.child%%c.value=!__section%%b.child%%d.value!
  134.                       set __section%%a.child%%c.validity=!__section%%b.child%%d.validity!
  135.                       set /a __section%%a.child%%c.controlOutput=!__section%%b.child%%d.controlOutput!
  136.                     )
  137.              )
  138.              set /a __section%%a.childCount=!endNumber!
  139.        )
  140.     )
  141.   )
  142. )
  143. set /a n=1
  144. set /a m=1
  145. type nul>!__yiwuyun.txt!
  146. :start
  147. if "!__section%m%.controlOutput!"=="0" (
  148.    rem echo __section%m%.controlOutput:!__section%m%.controlOutput!
  149.    echo set __section%n%.name=!__section%m%.name!>>!__yiwuyun.txt!
  150.    echo set __section%n%.validity=!__section%m%.validity!>>!__yiwuyun.txt!
  151.    echo set /a __section%n%.controlOutput=!__section%m%.controlOutput!>>!__yiwuyun.txt!
  152.    echo set /a __section%n%.childCount=!__section%m%.childCount!>>!__yiwuyun.txt!
  153.       for /l %%b in (1,1,!__section%m%.childCount!) do (
  154.          echo set __section%n%.child%%b.name=!__section%m%.child%%b.name!>>!__yiwuyun.txt!
  155.          echo set __section%n%.child%%b.value=!__section%m%.child%%b.value!>>!__yiwuyun.txt!
  156.          echo set /a __section%n%.child%%b.controlOutput=!__section%m%.child%%b.controlOutput!>>!__yiwuyun.txt!
  157.       )
  158.    set /a n+=1
  159. )
  160. set /a m+=1
  161. if !m! leq !__sectionCount! goto start
  162. set /a oldSectionCount=!__sectionCount!
  163. set /a __sectionCount=!n!-1
  164. rem Clear redundant variable to free memory space.
  165. for /l %%a in (!n!,1,!oldSectionCount!) do (
  166.     echo set __section%%a.name=>>!__yiwuyun.txt!
  167.     echo set __section%%a.controlOutput=>>!__yiwuyun.txt!
  168.     echo set __section%%a.validity=>>!__yiwuyun.txt!
  169.     echo set __section%%a.lineNumber=>>!__yiwuyun.txt!
  170.     for /l %%b in (1,1,!__section%%a.childCount!) do (
  171.         echo set __section%%a.child%%b.name=>>!__yiwuyun.txt!
  172.         echo set __section%%a.child%%b.value=>>!__yiwuyun.txt!
  173.         echo set __section%%a.child%%b.controlOutput=>>!__yiwuyun.txt!
  174.    )
  175.    echo set __section%%a.childCount=>>!__yiwuyun.txt!
  176. )
  177. echo set /a __sectionCount=!__sectionCount!>>!__yiwuyun.txt!
  178. endlocal
  179. rem transfer to outter variable.
  180. for /f "tokens=1 delims=" %%a in ('type !__yiwuyun.txt!') do (
  181.    rem echo %%a
  182.    %%a
  183. )
  184. if exist !__yiwuyun.txt! del !__yiwuyun.txt!
  185. exit /b 0
  186. :MergeSameChild
  187. setlocal
  188. set /a n=0
  189. set /a m=0
  190. for /l %%a in (1,1,!__sectionCount!) do (
  191.    for /l %%b in (1,1,!__section%%a.childCount!) do (
  192.        set /a n=%%b+1
  193.        for /l %%c in (!n!,1,!__section%%a.childCount!) do (
  194.           if "!m!"=="0" (
  195.             if /i "!__section%%a.child%%b.name!"=="!__section%%a.child%%c.name!" (
  196.                set __section%%a.child%%b.controlOutput=%%c
  197.                set /a m=1
  198.             )
  199.           )
  200.        )
  201.        set /a m=0
  202.    )
  203. )
  204. set /a n=0
  205. type nul>!__yiwuyun.txt!
  206. echo set /a __sectionCount=!__sectionCount!>>!__yiwuyun.txt!
  207. for /l %%a in (1,1,!__sectionCount!) do (
  208.    echo set __section%%a.name=!__section%%a.name!>>!__yiwuyun.txt!
  209.    echo set __section%%a.validity=!__section%%a.validity!>>!__yiwuyun.txt!
  210.    echo set __section%%a.controlOutput=!__section%%a.controlOutput!>>!__yiwuyun.txt!
  211.    for /l %%b in (1,1,!__section%%a.childCount!) do (
  212.        if "!__section%%a.child%%b.controlOutput!"=="0" (
  213.           set /a n+=1
  214.           for /l %%c in (!n!,1,!n!) do (
  215.              echo set __section%%a.child%%c.name=!__section%%a.child%%b.name!>>!__yiwuyun.txt!
  216.              echo set __section%%a.child%%c.value=!__section%%a.child%%b.value!>>!__yiwuyun.txt!
  217.              echo set __section%%a.child%%c.controlOutput=!__section%%a.child%%b.controlOutput!>>!__yiwuyun.txt!
  218.           )
  219.        )
  220.    )
  221.    echo set /a __section%%a.childCount=!n!>>!__yiwuyun.txt!
  222.    set /a n+=1
  223.    for /l %%b in (!n!,1,!__section%%a.childCount!) do (
  224.        echo set __section%%a.child%%b.name=>>!__yiwuyun.txt!
  225.        echo set __section%%a.child%%b.value=>>!__yiwuyun.txt!
  226.        echo set __section%%a.child%%b.controlOutput=>>!__yiwuyun.txt!
  227.    )
  228.    set /a n=0
  229. )
  230. endlocal
  231. for /f "tokens=1 delims=" %%a in ('type !__yiwuyun.txt!') do (
  232.    rem echo %%a
  233.    %%a
  234. )
  235. if exist !__yiwuyun.txt! del !__yiwuyun.txt!
  236. exit /b 0
  237. :GetExistSection
  238. setlocal
  239. set string=!string!
  240. if not "%1"=="" (
  241.    set temp=%1
  242. ) else (
  243.    set temp=!string!
  244. )
  245. set existSection=false
  246. for /l %%a in (1,1,!__sectionCount!) do (
  247.   if /i "!temp!"=="!__section%%a.name!" (
  248.      if "!__section%%a.controlOutput!"=="0" (
  249.        set existSection=true
  250.        set __sectionSequence=%%a
  251.      )
  252.   )
  253. )
  254. endlocal&set existSection=%existSection%&set __sectionSequence=%__sectionSequence%
  255. exit /b 0
  256. :GetExistSectionAndChild
  257. setlocal
  258. set string=!string!
  259. set existSectionAndChild=false
  260. set __childSequence=0
  261. if not "%2"=="" (
  262.    set tempSection=%1
  263.    set tempChild=%2
  264. ) else if not "%1"=="" (
  265.    set tempSection=!string!
  266.    set tempChild=%1
  267. ) else (
  268.    echo need a section and a child.
  269.    set existSectionAndChild=false
  270.    goto end
  271. )
  272. call :GetExistSection %tempSection%
  273. if "!existSection!"=="false" (
  274.    goto end
  275. ) else (
  276.    for /l %%a in (1,1,!__section%__sectionSequence%.childCount!) do (
  277.       if /i "!tempChild!"=="!__section%__sectionSequence%.child%%a.name!" (
  278.           if "!__section%__sectionSequence%.child%%a.controlOutput!"=="0" (
  279.              set existSectionAndChild=true
  280.              set __childSequence=%%a
  281.           )
  282.       )
  283.    )
  284. )
  285. :end
  286. endlocal&set existSectionAndChild=%existSectionAndChild%&set __childSequence=%__childSequence%&set __sectionSequence=%__sectionSequence%
  287. exit /b 0
  288. :DeleteSection
  289. if "%1"=="" echo need section&exit /b 1
  290. call :GetExistSection %1
  291. if "!existSection!"=="false" echo no section found&exit /b 1
  292. set /a __section%__sectionSequence%.controlOutput=1
  293. exit /b 0
  294. :UpdateChildOfSection
  295. setlocal
  296. if "%~3"=="" (
  297.    echo need a section ,a child and a value.
  298.    exit /b 1
  299. )
  300. call :GetExistSectionAndChild %1 %2
  301. if "!existSectionAndChild!"=="false" exit /b 1
  302. endlocal&set __section%__sectionSequence%.child%__childSequence%.value=%3
  303. exit /b 0
  304. :AddChildOfSection
  305. setlocal
  306. if "%~3"=="" (
  307.    echo need a section, a child and a value.
  308.    exit /b 1
  309. )
  310. call :GetExistSectionAndChild %1 %2
  311. if "!existSectionAndChild!"=="true" exit /b 1
  312. call :GetExistSection %1
  313. if "!existSection!"=="false" exit /b 1
  314. set /a n=!__section%__sectionSequence%.childCount!+1
  315. endlocal&set __section%__sectionSequence%.child%n%.name=%2&set /a __section%__sectionSequence%.child%n%.controlOutput=0&set __section%__sectionSequence%.childCount=%n%&set __section%__sectionSequence%.child%n%.value=%3
  316. exit /b 0
  317. :AddSectionAndChild
  318. if "%1"=="" echo need a section&exit /b 1
  319. if "%~3"=="" (
  320. echo add section.
  321. call :GetExistSection %1
  322. if "!existSection!"=="true" echo %1 exist.&exit /b 1
  323. call :GetAddOneVariable !__sectionCount!
  324. set __section!addOneVariable!.name=%1
  325. set /a __section!addOneVariable!.childCount=0
  326. set __section!addOneVariable!.controlOutput=0
  327. set __sectionCount=!addOneVariable!
  328. ) else (
  329. call :GetExistSection %1
  330. if "!existSection!"=="true" (
  331.    call :AddChildOfSection %1 %2 %3
  332. ) else (
  333.     call :GetAddOneVariable !__sectionCount!
  334.     set __section!addOneVariable!.name=%1
  335.     set __section!addOneVariable!.child1.name=%2
  336.     set __section!addOneVariable!.child1.value=%3
  337.     set /a __section!addOneVariable!.child1.controlOutput=0
  338.     set /a __section!addOneVariable!.childCount=1
  339.     set /a __section!addOneVariable!.controlOutput=0
  340.     set __sectionCount=!addOneVariable!
  341. )
  342. )
  343. exit /b 0
  344. :GetAddOneVariable
  345. if "%1"=="" (
  346.   set /a addOneVariable=0
  347. ) else (
  348.   if "%2"=="" (
  349.     set /a addOneVariable=%1+1
  350.   ) else (
  351.     set /a addOneVariable=%1+%2
  352.   )
  353. )
  354. exit /b 0
  355. :GetDecreaseOneVariable
  356. if "%1"=="" (
  357.   set /a decreaseOneVariable=0
  358. ) else (
  359.   if "%2"=="" (
  360.     set /a decreaseOneVariable=%1-1
  361.   ) else (
  362.     set /a decreaseOneVariable=%1-%2
  363.   )
  364. )
  365. exit /b 0
  366. :DeleteChildOfSection
  367. if "%2"=="" (
  368.    echo need a section and a child.
  369.    exit /b 1
  370. )
  371. call :GetExistSectionAndChild %1 %2
  372. if "!existSectionAndChild!"=="false" (
  373.    echo not exist section and child.
  374.    exit /b 1
  375. )
  376. set /a __section%__sectionSequence%.child%__childSequence%.controlOutput=1
  377. exit /b 0
  378. :DeleteChildOfSection1
  379. setlocal
  380. if "%2"=="" (
  381.    echo need a section and a child.
  382.    exit /b 1
  383. )
  384. call :GetExistSectionAndChild %1 %2
  385. if "!existSectionAndChild!"=="false" (
  386.    echo not exist section and child.
  387.    exit /b 1
  388. )
  389. set /a m=!__section%__sectionSequence%.childCount!-1
  390. for /l %%a in (%__childSequence%,1,%m%) do (
  391.    set /a n=%%a+1
  392.    for /l %%b in (!n!,1,!n!) do (
  393.       set __section%__sectionSequence%.child%%a.name=!__section%__sectionSequence%.child%%b.name!
  394.       set __section%__sectionSequence%.child%%a.value=!__section%__sectionSequence%.child%%b.value!
  395.       set __section%__sectionSequence%.child%%a.controlOutput=!__section%__sectionSequence%.child%%b.controlOutput!
  396.    )
  397. )
  398. set /a __section%__sectionSequence%.childCount=%m%
  399. type nul>!__yiwuyun.txt!
  400. echo set __section%__sectionSequence%.child%n%.controlOutput=>>!__yiwuyun.txt!
  401. echo set __section%__sectionSequence%.child%n%.name=>>!__yiwuyun.txt!
  402. echo set __section%__sectionSequence%.child%n%.value=>>!__yiwuyun.txt!
  403. echo set /a __section%__sectionSequence%.childCount=!__section%__sectionSequence%.childCount!>>!__yiwuyun.txt!
  404. for /l %%a in (%__childSequence%,1,!__section%__sectionSequence%.childCount!) do (
  405.    echo set __section%__sectionSequence%.child%%a.name=!__section%__sectionSequence%.child%%a.name!>>!__yiwuyun.txt!
  406.    echo set __section%__sectionSequence%.child%%a.value=!__section%__sectionSequence%.child%%a.value!>>!__yiwuyun.txt!
  407.    echo set __section%__sectionSequence%.child%%a.controlOutput=!__section%__sectionSequence%.child%%a.controlOutput!>>!__yiwuyun.txt!
  408. )
  409. endlocal
  410. for /f "tokens=1 delims=" %%a in ('type !__yiwuyun.txt!') do (
  411.    rem echo %%a
  412.    %%a
  413. )
  414. if exist !__yiwuyun.txt! del !__yiwuyun.txt!
  415. exit /b 0
  416. :GetChildValueOfSection
  417. if "%2"=="" (
  418.    echo need a section and a child.
  419.    exit /b 1
  420. )
  421. call :GetExistSectionAndChild %1 %2
  422. if "!existSectionAndChild!"=="false" (
  423.    echo not exist section and child.
  424.    exit /b 1
  425. )
  426. set childValueOfSection=!__section%__sectionSequence%.child%__childSequence%.value!
  427. exit /b 0
  428. :IsSpaceLine
  429. call :GetStringTrimSpace
  430. if "!stringTrimSpace!"=="" (
  431.   set isSpaceLine=true
  432. ) else (
  433.   set isSpaceLine=false
  434. )
  435. exit /b 0
  436. :IsLegalEqualityExpression
  437. setlocal
  438. call :GetParseEqualityExpression
  439. if "!parseEqualityExpression!"=="true" (
  440.   if "!stringEqualityBefore!"=="" (
  441.     set isLegalEqualityExpression=false
  442.     goto end
  443.   ) else if "!stringEqualityAfter!"=="" (
  444.     set isLegalEqualityExpression=false
  445.     goto end
  446.   ) else (
  447.     set string=!stringEqualityBefore!
  448.     call :IsLegalString
  449.     if "!isLegalString!"=="fasle" (
  450.       set isLegalEqualityExpression=false
  451.       goto end
  452.     ) else (
  453.       set string=!stringEqualityBefore!
  454.       call :GetStringTrimSpace
  455.       set stringEqualityBefore=!stringTrimSpace!
  456.       set string=!stringEqualityAfter!
  457.       call :GetStringTrimSpace
  458.       set stringEqualityAfter=!stringTrimSpace!
  459.       if "!stringEqualityAfter!"=="" (
  460.          set isLegalEqualityExpression=false
  461.          goto end
  462.       ) else (
  463.          set isLegalEqualityExpression=true
  464.       )
  465.     )
  466.   )
  467. ) else (
  468.   set isLegalEqualityExpression=false
  469. )
  470. :end
  471. endlocal&set isLegalEqualityExpression=%isLegalEqualityExpression%&set stringEqualityBefore=%stringEqualityBefore%&set stringEqualityAfter=%stringEqualityAfter%
  472. exit /b 0
  473. :GetParseEqualityExpression
  474. setlocal
  475. set string=!string!
  476. call :GetStringMaxIndex
  477. for /l %%a in (0,1,!stringMaxIndex!) do (
  478.   if "!string:~%%a,1!"=="=" (
  479.      set stringEqualityBefore=!string:~0,%%a!
  480.      set stringEqualityAfter=!string:~%%a!
  481.      set stringEqualityAfter=!stringEqualityAfter:~1!
  482.      set parseEqualityExpression=true
  483.      goto end
  484.   )
  485. )
  486. set parseEqualityExpression=false
  487. :end
  488. endlocal&set parseEqualityExpression=%parseEqualityExpression%&set stringEqualityBefore=%stringEqualityBefore%&set stringEqualityAfter=%stringEqualityAfter%
  489. exit /b 0
  490. :GetStringMaxIndex
  491. call :GetStringLength
  492. set /a stringMaxIndex=!stringLength!-1
  493. exit /b 0
  494. :IsLegalSection
  495. setlocal
  496. call :GetStringTrimSpace
  497. set string=!stringTrimSpace!
  498. call :GetStringLength
  499. if !stringLength! lss 3 (
  500.    set __section.validity=false
  501.    goto end
  502. )
  503. if not "!string:~0,1!"=="[" (
  504.    set __section.validity=false
  505.    goto end
  506. )
  507. if not "!string:~-1,1!"=="]" (
  508.    set __section.validity=false
  509.    goto end
  510. )
  511. set string=!string:~1,-1!
  512. call :IsLegalString
  513. if not "!isLegalString!"=="true" (
  514.    set __section.validity=false
  515.    goto end
  516. ) else (
  517.    set __section.validity=true
  518.    call :GetStringTrimSpace
  519.    set __section.name=!stringTrimSpace!
  520. )
  521. :end
  522. endlocal&set __section.validity=%__section.validity%&set __section.name=%__section.name%
  523. exit /b 0
  524. :GetStringTrimSpace
  525. setlocal
  526. set string=!string!
  527. call :GetStringLeftTrimSpace
  528. set string=!stringLeftTrimSpace!
  529. call :GetStringRightTrimSpace
  530. set stringTrimSpace=!stringRightTrimSpace!
  531. endlocal&set stringTrimSpace=%stringTrimSpace%
  532. exit /b 0
  533. :GetStringLeftTrimSpace
  534. setlocal
  535. set string=!string!
  536. :startStringLeftTrimSpace
  537. if "!string:~0,1!"==" " (
  538.   set string=!string:~1!
  539. ) else (
  540.   goto endStringLeftTrimSpace
  541. )
  542. goto startStringLeftTrimSpace
  543. :endStringLeftTrimSpace
  544. set stringLeftTrimSpace=!string!
  545. endlocal&set stringLeftTrimSpace=%stringLeftTrimSpace%
  546. exit /b 0
  547. :GetStringRightTrimSpace
  548. setlocal
  549. set string=!string!
  550. :startStringRightTrimSpace
  551. if "!string:~-1,1!"==" " (
  552.   set string=!string:~0,-1!
  553. ) else (
  554.   goto endStringRightTrimSpace
  555. )
  556. goto startStringRightTrimSpace
  557. :endStringRightTrimSpace
  558. set stringRightTrimSpace=!string!
  559. endlocal&set stringRightTrimSpace=%stringRightTrimSpace%
  560. exit /b 0
  561. :GetStringTrimAllSpace
  562. setlocal
  563. set string=!string!
  564. call :GetStringTrimSpace
  565. set string=!stringTrimSpace!
  566. call :GetStringLength
  567. set /a n=!stringLength!
  568. set /a n=!n!-1
  569. for /l %%a in (0,1,!n!) do (
  570.    if "!string:~%%a,1!"==" " (     
  571.         call :GetStringDeletedChar %%a
  572.         set string= !stringDeletedChar!
  573.    )
  574. )
  575. call :GetStringTrimSpace
  576. set stringTrimAllSpace=!stringTrimSpace!
  577. endlocal&set stringTrimAllSpace=%stringTrimAllSpace%
  578. exit /b 0
  579. :GetStringTrimRedundanceSpace
  580. setlocal
  581. set string=!string!
  582. call :GetStringTrimSpace
  583. set string=!stringTrimSpace!
  584. call :GetStringLength
  585. set /a n=!stringLength!
  586. set /a n=!n!-1
  587. for /l %%a in (0,1,!n!) do (
  588.    if "!string:~%%a,1!"==" " (
  589.       if "!space!"=="true" (
  590.         call :GetStringDeletedChar %%a
  591.         set string= !stringDeletedChar!
  592.       )
  593.       set space=true
  594.    ) else (
  595.        set space=false
  596.    )
  597. )
  598. call :GetStringTrimSpace
  599. set stringTrimRedundanceSpace=!stringTrimSpace!
  600. endlocal&set stringTrimRedundanceSpace=%stringTrimRedundanceSpace%
  601. exit /b 0
  602. :GetStringDeletedChar
  603. setlocal
  604. set temp=!string!
  605. set temp1=!temp:~0,%1!
  606. if "%2"=="" (
  607.   set temp2=!temp:~%1!
  608. ) else (
  609.   set temp2=!temp:~%2!
  610. )
  611. set temp3=!temp2:~1!
  612. set string=!temp1!!temp3!
  613. set stringDeletedChar=!string!
  614. endlocal&set stringDeletedChar=%stringDeletedChar%
  615. exit /b 0
  616. :GetStringUpperCase
  617. setlocal
  618. set string=!string!
  619. call :GetStringLength
  620. set /a n=!stringLength!-1
  621. for /l %%a in (0,1,!n!) do (
  622.    set char=!string:~%%a,1!
  623.    call :IsLowerCase !char!
  624.    if "!isLowerCase!"=="true" (
  625.        call :GetUpperCase !char!
  626.        set char=!upperCase!
  627.        set temp1=!string:~0,%%a!
  628.        set temp2=!string:~%%a!
  629.        set temp3=!temp2:~1!
  630.        set string=!temp1!!char!!temp3!
  631.        )  
  632.    )
  633. )
  634. set stringUpperCase=!string!
  635. endlocal&set stringUpperCase=%stringUpperCase%
  636. exit /b 0
  637. :GetStringLowerCase
  638. setlocal
  639. set string=!string!
  640. call :GetStringLength
  641. set /a n=!stringLength!-1
  642. for /l %%a in (0,1,!n!) do (
  643.    set char=!string:~%%a,1!
  644.    call :IsUpperCase !char!
  645.    if "!isUpperCase!"=="true" (
  646.        call :GetLowerCase !char!
  647.        set char=!lowerCase!
  648.        set temp1=!string:~0,%%a!
  649.        set temp2=!string:~%%a!
  650.        set temp3=!temp2:~1!
  651.        set string=!temp1!!char!!temp3!
  652.        )  
  653.    )
  654. )
  655. set stringLowerCase=!string!
  656. endlocal&set stringLowerCase=%stringLowerCase%
  657. exit /b 0
  658. :GetUpperCase
  659. setlocal
  660. call :GetSequenceNumber %1
  661. set /a n=1
  662. for %%a in (A B C D E F G H I J K L M N O P Q R S T U V W X Y Z) do (
  663.    if !sequenceNumber! equ !n! (
  664.       set upperCase=%%a
  665.       goto end
  666.    ) else (
  667.       set /a n=!n!+1
  668.    )
  669. )
  670. :end
  671. endlocal&set upperCase=%upperCase%
  672. exit /b 0
  673. :GetLowerCase
  674. setlocal
  675. call :GetSequenceNumber %1
  676. set /a n=1
  677. for %%a in (a b c d e f g h i j k l m n o p q r s t u v w x y z) do (
  678.    if !sequenceNumber! equ !n! (
  679.       set lowerCase=%%a
  680.       goto end
  681.    ) else (
  682.       set /a n=!n!+1
  683.    )
  684. )
  685. :end
  686. endlocal&set lowerCase=%lowerCase%
  687. exit /b 0
  688. :GetSequenceNumber
  689. setlocal
  690. set /a n=0
  691. for %%a in (a b c d e f g h i j k l m n o p q r s t u v w x y z) do (
  692.   if "%%a"=="%1" (
  693.     set /a n=!n!+1
  694.     set sequenceNumber=!n!
  695.     goto end
  696.   )
  697.   set /a n=!n!+1
  698. )
  699. set /a n=0
  700. for %%a in (A B C D E F G H I J K L M N O P Q R S T U V W X Y Z) do (
  701.   if "%%a"=="%1" (
  702.     set /a n=!n!+1
  703.     set sequenceNumber=!n!
  704.     goto end
  705.   )
  706.   set /a n=!n!+1
  707. )
  708. :end
  709. endlocal&set sequenceNumber=%sequenceNumber%
  710. exit /b 0
  711. :IsLowerCase
  712. for %%a in (a b c d e f g h i j k l m n o p q r s t u v w x y z) do (
  713.    if "%1"=="%%a" (
  714.        set isLowerCase=true
  715.        goto endIsLowerCase
  716.    ) else (
  717.        set isLowerCase=false
  718.    )
  719. )
  720. :endIsLowerCase
  721. exit /b 0
  722. :IsUpperCase
  723. for %%a in (A B C D E F G H I J K L M N O P Q R S T U V W X Y Z) do (
  724.    if "%1"=="%%a" (
  725.        set isUpperCase=true
  726.        goto endIsUpperCase
  727.    ) else (
  728.        set isUpperCase=false
  729.    )
  730. )
  731. :endIsUpperCase
  732. exit /b 0
  733. :IsAlphabet
  734. call :IsUpperCase %1
  735. if "!isUpperCase!"=="true" (
  736.    set isAlphabet=true
  737.    goto endIsAlphabet
  738. ) else (
  739.    set isAlphabet=false
  740. )
  741. call :IsLowerCase %1
  742. if "!isLowerCase!"=="true" (
  743.    set isAlphabet=true
  744. ) else (
  745.    set isAlphabet=false
  746. )
  747. :endIsAlphabet
  748. exit /b 0
  749. :IsDigit
  750. for %%a in (0 1 2 3 4 5 6 7 8 9) do (
  751.    if "%%a"=="%1" (
  752.       set isDigit=true
  753.       goto endIsDigit
  754.    ) else (
  755.       set isDigit=false
  756.    )
  757. )
  758. :endIsDigit
  759. exit /b 0
  760. :IsSpecialChar
  761. for %%a in (_ . #) do (
  762.    if "%%a"=="%1" (
  763.       set isSpecialChar=true
  764.       goto endIsSpecialChar
  765.    ) else (
  766.       set isSpecialChar=false
  767.    )
  768. )
  769. :endIsSpecialChar
  770. exit /b 0
  771. :IsLegalString
  772. setlocal
  773. set string=!string!
  774. call :GetStringTrimSpace
  775. set string=!stringTrimSpace!
  776. call :IsLegalHeadChar !string:~0,1!
  777. if "!isLegalHeadChar!"=="true" (
  778.    set isLegalString=true
  779. ) else (
  780.    set isLegalString=false
  781.    goto endIsLegalString
  782. )
  783. call :GetStringLength
  784. set /a n=!stringLength!-1
  785. for /l %%a in (0,1,!n!) do (
  786.   call :IsLegalChar !string:~%%a,1!
  787.   if "!isLegalChar!"=="true" (
  788.      set isLegalString=true
  789.   ) else (
  790.      set isLegalString=false
  791.      goto endIsLegalString
  792.   )
  793. )
  794. :endIsLegalString
  795. endlocal&set isLegalString=%isLegalString%
  796. exit /b 0
  797. :IsLegalHeadChar
  798. if "%1"=="_" (
  799.    set isLegalHeadChar=true
  800.    goto endIsLegalHeadChar
  801. ) else (
  802.    set isLegalHeadChar=false
  803. )
  804. call :IsAlphabet %1
  805. if "!isAlphabet!"=="true" (
  806.    set isLegalHeadChar=true
  807. ) else (
  808.    set isLegalHeadChar=false
  809. )
  810. :endIsLegalHeadChar
  811. exit /b 0
  812. :IsLegalChar
  813. call :IsDigit %1
  814. if "!isDigit!"=="true" (
  815.   set isLegalChar=true
  816.   goto endIsLegalChar
  817. ) else (
  818.   set isLegalChar=false
  819. )
  820. call :IsAlphabet %1
  821. if "!isAlphabet!"=="true" (
  822.    set isLegalChar=true
  823.    goto endIsLegalChar
  824. ) else (
  825.    set isLegalChar=false
  826. )
  827. call :IsSpecialChar %1
  828. if "!isSpecialChar!"=="true" (
  829.    set isLegalChar=true
  830. ) else (
  831.    set isLegalChar=false
  832. )
  833. :endIsLegalChar
  834. exit /b 0
  835. :GetStringLength
  836. setlocal
  837. set string=!string!
  838. set /a n=0
  839. :startGetStringLength
  840. if not "!string!"=="" (
  841.   set string=!string:~1!
  842.   set /a n=!n!+1
  843.   goto startGetStringLength
  844. )
  845. set /a stringLength=!n!
  846. endlocal&set stringLength=%stringLength%
  847. exit /b 0
  848. :GetAmpersandPlaceNAndCount
  849. setlocal
  850. set string=!string!
  851. call :GetStringLength
  852. set /a nGetAmpersandPlaceNAndCount=!stringLength!-1
  853. set /a nAmpersand=1
  854. set /a ampersandPlace0=-1
  855. for /l %%a in (0,1,!nGetAmpersandPlaceNAndCount!) do (
  856.   if "!string:~%%a,1!"=="&" (
  857.      set /a ampersandPlace!nAmpersand!=%%a
  858.      set /a nAmpersand+=1
  859.   )
  860. )
  861. set /a ampersandPlace!nAmpersand!=!stringLength!
  862. set /a ampersandCount=!nAmpersand!-1
  863. type nul>!__yiwuyun.txt!
  864. for /l %%a in (0,1,!nAmpersand!) do (
  865.    echo set /a ampersandPlace%%a=!ampersandPlace%%a!>>!__yiwuyun.txt!
  866. )
  867. echo set /a ampersandCount=!ampersandCount!>>!__yiwuyun.txt!
  868. endlocal
  869. for /f "tokens=1 delims=" %%a in ('type !__yiwuyun.txt!') do (
  870.   rem echo %%a
  871.   %%a
  872. )
  873. if exist !__yiwuyun.txt! del !__yiwuyun.txt!
  874. exit /b 0
  875. :GetAmpersandCommandNAndCount
  876. setlocal
  877. set string=!string!
  878. call :GetAmpersandPlaceNAndCount
  879. set /a next=0
  880. set /a ampersandCommandCount=!ampersandCount!+1
  881. set /a n=0
  882. :startGetAmpersandCommandNAndCount
  883.   set /a beforePlace=!ampersandPlace%next%!
  884.   set /a first=!beforePlace!+1
  885.   set /a next=!next!+1
  886.   set /a afterPlace=!ampersandPlace%next%!
  887.   set /a second=!afterPlace!-!beforePlace!-1
  888.   set /a n=!n!+1
  889.   set ampersandCommand!n!=!string:~%first%,%second%!
  890. if !n! lss !ampersandCommandCount! goto startGetAmpersandCommandNAndCount
  891. type nul>!__yiwuyun.txt!
  892. for /l %%a in (1,1,!ampersandCommandCount!) do (
  893.    echo set ampersandCommand%%a=!ampersandCommand%%a!>>!__yiwuyun.txt!
  894. )
  895. echo set /a ampersandCommandCount=!ampersandCommandCount!>>!__yiwuyun.txt!
  896. endlocal
  897. for /f "tokens=1 delims=" %%a in ('type !__yiwuyun.txt!') do (
  898.   rem echo %%a
  899.   %%a
  900. )
  901. if exist !__yiwuyun.txt! del !__yiwuyun.txt!
  902. exit /b 0
复制代码
2

评分人数

够长!

TOP

看起来是对inf格式化的代码,这么长......LZ很有毅力

TOP

非第三方工具对ini文件处理的批很少。希望可以详细说明一下。
另希望有可以对ini内容操作 有增加,删除,读取,,修改的功能

TOP

能否写个使用文档放在顶楼?
近千行的代码,又没什么注释,别人可能不知道代码有哪些功能以及如何使用。
我帮忙写的代码不需要付钱。如果一定要给,请在微信群或QQ群发给大家吧。
【微信公众号、微信群、QQ群】http://bbs.bathome.net/thread-3473-1-1.html
【支持批处理之家,加入VIP会员!】http://bbs.bathome.net/thread-67716-1-1.html

TOP

返回列表