本帖最后由 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的代码:- @echo off
- cls
- echo author: JTSYZX-yiwuyun 2014.12.08
- echo usage: call .\general.bat :Command File
- echo usage: call .\general.bat :Command SectionName [ChildName [ChildValue]]
- echo usage: Command: OpenFile,SaveToFile
- echo usage: Command: AddSectionAndChild,UpdateChildOfSection,DeleteChildOfSection,DeleteSection,GetChildValueOfSection
- set __file_yi=test.txt
- setlocal EnableDelayedExpansion
- setlocal ENABLEEXTENSIONS
- call .\general.bat :OpenFile !__file_yi!
- call .\general.bat :AddSectionAndChild name
- call .\general.bat :SaveToFile !__file_yi!
- type !__file_yi!
- echo AddSectionAndChild name
- pause
- call .\general.bat :AddSectionAndChild name author yiwuyun
- call .\general.bat :SaveToFile !__file_yi!
- type !__file_yi!
- echo AddSectionAndChild name author yiwuyun
- pause
- call .\general.bat :AddSectionAndChild name yi wuyun
- call .\general.bat :AddChildOfSection name fu futongzhen
- call .\general.bat :AddSectionAndChild office word true
- call .\general.bat :AddSectionAndChild office excel true
- call .\general.bat :SaveToFile !__file_yi!
- type !__file_yi!
- echo AddSectionAndChild name yi wuyun
- echo AddChildOfSection name fu futongzhen
- echo AddSectionAndChild office word true
- echo AddSectionAndChild office excel true
- pause
- call .\general.bat :UpdateChildOfSection office excel false
- call .\general.bat :SaveToFile !__file_yi!
- type !__file_yi!
- echo UpdateChildOfSection office excel false
- pause
- call .\general.bat :DeleteChildOfSection name yi
- call .\general.bat :SaveToFile !__file_yi!
- type !__file_yi!
- echo DeleteChildOfSection name yi
- pause
- call .\general.bat :DeleteSection office
- call .\general.bat :SaveToFile !__file_yi!
- type !__file_yi!
- echo DeleteSection office
- pause
- call .\general.bat :GetChildValueOfSection name author
- echo GetChildValueOfSection name author
- echo author:!childValueOfSection!
- pause
- endlocal
- exit /b
- @echo on
复制代码 以下为操作ini的主体代码。文件名为:general.bat- @echo off
- set __yiwuyun.txt=___yiwuyun.txt
- call %1 %2 %3 %4
- exit /b
- @echo on
-
-
- :GetLineCount
- setlocal
- set /a n=0
- for /f %%a in ('type %1') do (
- set /a n=!n!+1
- )
- set lineCount=!n!
- endlocal&set lineCount=%lineCount%
- exit /b 0
-
-
- :SaveToFile
- if not "%1"=="" (
- set string=%1
- )
- if "!string!"=="" exit /b 1
- type nul>!string!
- for /l %%a in (1,1,!__sectionCount!) do (
- if "!__section%%a.controlOutput!"=="0" (
- echo [!__section%%a.name!]>>!string!
- for /l %%b in (1,1,!__section%%a.childCount!) do (
- if "!__section%%a.child%%b.controlOutput!"=="0" (
- echo !__section%%a.child%%b.name!=!__section%%a.child%%b.value!>>!string!
- )
- )
- echo;>>!string!
- )
- )
- exit /b 0
-
-
- :OpenFile
- echo author: JTSYZX-yiwuyun 2014.12.08
- echo;
- echo;
- echo;
- if "%1"=="" (
- echo need a file.
- exit /b 1
- )
- if exist %1 (
- call :GetSectionNAndCount %1
- ) else (
- type nul>%1
- call :GetSectionNAndCount %1
- if exist %1 del %1
- )
- exit /b 0
-
- :GetSectionNAndCount
- setlocal
- set string=!string!
- set /a nChild=0
- set /a n=1
- set /a __sectionNumber=1
- for /f "tokens=1 delims=" %%a in ('type %1') do (
- set string=%%a
- call :IsSpaceLine
- if "!isSpaceLine!"=="true" (
- echo This is space line: !n!
- echo This line will be deleted.
- ) else (
- call :IsLegalSection
- if "!__section.validity!"=="true" (
- if !nChild! gtr 0 set /a nChild=0
- set __section!__sectionNumber!.name=!__section.name!
- set /a __section!__sectionNumber!.lineNumber=!n!
- set __section!__sectionNumber!.validity=true
- set /a __section!__sectionNumber!.childCount=!nChild!
- set /a nTemp=!__sectionNumber!
- set /a __sectionNumber=!__sectionNumber!+1
- ) else (
- if !__sectionNumber! lss 2 (
- echo SectionNumber lss 2
- echo Section should be the first.
- echo This line will be deleted.
- ) else (
- call :IsLegalEqualityExpression
- if "!isLegalEqualityExpression!"=="true" (
- set /a nChild=!nChild!+1
- set __section!nTemp!.child!nChild!.name=!stringEqualityBefore!
- set __section!nTemp!.child!nChild!.value=!stringEqualityAfter!
- set __section!nTemp!.childCount=!nChild!
- ) else (
- echo This is line:!n! this is not legal line.
- echo This line will be deleted.
- )
- )
- )
- )
- set /a n=!n!+1
- )
- set /a __sectionCount=!__sectionNumber!-1
- type nul>!__yiwuyun.txt!
- echo set /a __sectionCount=!__sectionCount!>>!__yiwuyun.txt!
- set /a controlOutput=0
- for /l %%a in (1,1,!__sectionCount!) do (
- echo set __section%%a.name=!__section%%a.name!>>!__yiwuyun.txt!
- echo set __section%%a.lineNumber=!__section%%a.lineNumber!>>!__yiwuyun.txt!
- echo set __section%%a.validity=!__section%%a.validity!>>!__yiwuyun.txt!
- echo set /a __section%%a.controlOutput=!controlOutput!>>!__yiwuyun.txt!
- echo set /a __section%%a.childCount=!__section%%a.childCount!>>!__yiwuyun.txt!
- for /l %%b in (1,1,!__section%%a.childCount!) do (
- echo set __section%%a.child%%b.name=!__section%%a.child%%b.name!>>!__yiwuyun.txt!
- echo set __section%%a.child%%b.value=!__section%%a.child%%b.value!>>!__yiwuyun.txt!
- echo set /a __section%%a.child%%b.controlOutput=!controlOutput!>>!__yiwuyun.txt!
- )
- )
- endlocal
- for /f "tokens=1 delims=" %%a in ('type !__yiwuyun.txt!') do (
- rem echo %%a
- %%a
- )
- if exist !__yiwuyun.txt! del !__yiwuyun.txt!
- call :MergeSameSection
- call :MergeSameChild
- exit /b 0
-
- :MergeSameSection
- setlocal
- for /l %%a in (1,1,!__sectionCount!) do (
- if "!__section%%a.controlOutput!"=="0" (
- set /a n=%%a+1
- for /l %%b in (!n!,1,!__sectionCount!) do (
- if /i "!__section%%a.name!"=="!__section%%b.name!" (
- set /a __section%%b.controlOutput=%%a
- rem echo __section%%b.controlOutput:!__section%%b.controlOutput!
- set /a startNumber=!__section%%a.childCount!+1
- set /a endNumber=!__section%%a.childCount!+!__section%%b.childCount!
- for /l %%c in (!startNumber!,1,!endNumber!) do (
- set /a temp=%%c-!__section%%a.childCount!
- for /l %%d in (!temp!,1,!temp!) do (
- set __section%%a.child%%c.name=!__section%%b.child%%d.name!
- set __section%%a.child%%c.value=!__section%%b.child%%d.value!
- set __section%%a.child%%c.validity=!__section%%b.child%%d.validity!
- set /a __section%%a.child%%c.controlOutput=!__section%%b.child%%d.controlOutput!
- )
- )
- set /a __section%%a.childCount=!endNumber!
- )
- )
- )
- )
- set /a n=1
- set /a m=1
- type nul>!__yiwuyun.txt!
- :start
- if "!__section%m%.controlOutput!"=="0" (
- rem echo __section%m%.controlOutput:!__section%m%.controlOutput!
- echo set __section%n%.name=!__section%m%.name!>>!__yiwuyun.txt!
- echo set __section%n%.validity=!__section%m%.validity!>>!__yiwuyun.txt!
- echo set /a __section%n%.controlOutput=!__section%m%.controlOutput!>>!__yiwuyun.txt!
- echo set /a __section%n%.childCount=!__section%m%.childCount!>>!__yiwuyun.txt!
- for /l %%b in (1,1,!__section%m%.childCount!) do (
- echo set __section%n%.child%%b.name=!__section%m%.child%%b.name!>>!__yiwuyun.txt!
- echo set __section%n%.child%%b.value=!__section%m%.child%%b.value!>>!__yiwuyun.txt!
- echo set /a __section%n%.child%%b.controlOutput=!__section%m%.child%%b.controlOutput!>>!__yiwuyun.txt!
- )
- set /a n+=1
- )
- set /a m+=1
- if !m! leq !__sectionCount! goto start
- set /a oldSectionCount=!__sectionCount!
- set /a __sectionCount=!n!-1
- rem Clear redundant variable to free memory space.
- for /l %%a in (!n!,1,!oldSectionCount!) do (
- echo set __section%%a.name=>>!__yiwuyun.txt!
- echo set __section%%a.controlOutput=>>!__yiwuyun.txt!
- echo set __section%%a.validity=>>!__yiwuyun.txt!
- echo set __section%%a.lineNumber=>>!__yiwuyun.txt!
- for /l %%b in (1,1,!__section%%a.childCount!) do (
- echo set __section%%a.child%%b.name=>>!__yiwuyun.txt!
- echo set __section%%a.child%%b.value=>>!__yiwuyun.txt!
- echo set __section%%a.child%%b.controlOutput=>>!__yiwuyun.txt!
- )
- echo set __section%%a.childCount=>>!__yiwuyun.txt!
- )
-
- echo set /a __sectionCount=!__sectionCount!>>!__yiwuyun.txt!
- endlocal
-
- rem transfer to outter variable.
- for /f "tokens=1 delims=" %%a in ('type !__yiwuyun.txt!') do (
- rem echo %%a
- %%a
- )
- if exist !__yiwuyun.txt! del !__yiwuyun.txt!
- exit /b 0
-
- :MergeSameChild
- setlocal
- set /a n=0
- set /a m=0
- for /l %%a in (1,1,!__sectionCount!) do (
- for /l %%b in (1,1,!__section%%a.childCount!) do (
- set /a n=%%b+1
- for /l %%c in (!n!,1,!__section%%a.childCount!) do (
- if "!m!"=="0" (
- if /i "!__section%%a.child%%b.name!"=="!__section%%a.child%%c.name!" (
- set __section%%a.child%%b.controlOutput=%%c
- set /a m=1
- )
- )
- )
- set /a m=0
- )
- )
- set /a n=0
- type nul>!__yiwuyun.txt!
- echo set /a __sectionCount=!__sectionCount!>>!__yiwuyun.txt!
- for /l %%a in (1,1,!__sectionCount!) do (
- echo set __section%%a.name=!__section%%a.name!>>!__yiwuyun.txt!
- echo set __section%%a.validity=!__section%%a.validity!>>!__yiwuyun.txt!
- echo set __section%%a.controlOutput=!__section%%a.controlOutput!>>!__yiwuyun.txt!
- for /l %%b in (1,1,!__section%%a.childCount!) do (
- if "!__section%%a.child%%b.controlOutput!"=="0" (
- set /a n+=1
- for /l %%c in (!n!,1,!n!) do (
- echo set __section%%a.child%%c.name=!__section%%a.child%%b.name!>>!__yiwuyun.txt!
- echo set __section%%a.child%%c.value=!__section%%a.child%%b.value!>>!__yiwuyun.txt!
- echo set __section%%a.child%%c.controlOutput=!__section%%a.child%%b.controlOutput!>>!__yiwuyun.txt!
- )
- )
- )
- echo set /a __section%%a.childCount=!n!>>!__yiwuyun.txt!
- set /a n+=1
- for /l %%b in (!n!,1,!__section%%a.childCount!) do (
- echo set __section%%a.child%%b.name=>>!__yiwuyun.txt!
- echo set __section%%a.child%%b.value=>>!__yiwuyun.txt!
- echo set __section%%a.child%%b.controlOutput=>>!__yiwuyun.txt!
- )
- set /a n=0
- )
- endlocal
- for /f "tokens=1 delims=" %%a in ('type !__yiwuyun.txt!') do (
- rem echo %%a
- %%a
- )
- if exist !__yiwuyun.txt! del !__yiwuyun.txt!
- exit /b 0
-
-
- :GetExistSection
- setlocal
- set string=!string!
- if not "%1"=="" (
- set temp=%1
- ) else (
- set temp=!string!
- )
- set existSection=false
- for /l %%a in (1,1,!__sectionCount!) do (
- if /i "!temp!"=="!__section%%a.name!" (
- if "!__section%%a.controlOutput!"=="0" (
- set existSection=true
- set __sectionSequence=%%a
- )
- )
- )
- endlocal&set existSection=%existSection%&set __sectionSequence=%__sectionSequence%
- exit /b 0
-
- :GetExistSectionAndChild
- setlocal
- set string=!string!
- set existSectionAndChild=false
- set __childSequence=0
- if not "%2"=="" (
- set tempSection=%1
- set tempChild=%2
- ) else if not "%1"=="" (
- set tempSection=!string!
- set tempChild=%1
- ) else (
- echo need a section and a child.
- set existSectionAndChild=false
- goto end
- )
- call :GetExistSection %tempSection%
- if "!existSection!"=="false" (
- goto end
- ) else (
- for /l %%a in (1,1,!__section%__sectionSequence%.childCount!) do (
- if /i "!tempChild!"=="!__section%__sectionSequence%.child%%a.name!" (
- if "!__section%__sectionSequence%.child%%a.controlOutput!"=="0" (
- set existSectionAndChild=true
- set __childSequence=%%a
- )
- )
- )
- )
- :end
- endlocal&set existSectionAndChild=%existSectionAndChild%&set __childSequence=%__childSequence%&set __sectionSequence=%__sectionSequence%
- exit /b 0
-
-
- :DeleteSection
- if "%1"=="" echo need section&exit /b 1
- call :GetExistSection %1
- if "!existSection!"=="false" echo no section found&exit /b 1
- set /a __section%__sectionSequence%.controlOutput=1
- exit /b 0
-
-
- :UpdateChildOfSection
- setlocal
- if "%~3"=="" (
- echo need a section ,a child and a value.
- exit /b 1
- )
- call :GetExistSectionAndChild %1 %2
- if "!existSectionAndChild!"=="false" exit /b 1
- endlocal&set __section%__sectionSequence%.child%__childSequence%.value=%3
- exit /b 0
-
- :AddChildOfSection
- setlocal
- if "%~3"=="" (
- echo need a section, a child and a value.
- exit /b 1
- )
- call :GetExistSectionAndChild %1 %2
- if "!existSectionAndChild!"=="true" exit /b 1
- call :GetExistSection %1
- if "!existSection!"=="false" exit /b 1
- set /a n=!__section%__sectionSequence%.childCount!+1
- 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
- exit /b 0
-
-
-
- :AddSectionAndChild
- if "%1"=="" echo need a section&exit /b 1
- if "%~3"=="" (
- echo add section.
- call :GetExistSection %1
- if "!existSection!"=="true" echo %1 exist.&exit /b 1
- call :GetAddOneVariable !__sectionCount!
- set __section!addOneVariable!.name=%1
- set /a __section!addOneVariable!.childCount=0
- set __section!addOneVariable!.controlOutput=0
- set __sectionCount=!addOneVariable!
- ) else (
- call :GetExistSection %1
- if "!existSection!"=="true" (
- call :AddChildOfSection %1 %2 %3
- ) else (
- call :GetAddOneVariable !__sectionCount!
- set __section!addOneVariable!.name=%1
- set __section!addOneVariable!.child1.name=%2
- set __section!addOneVariable!.child1.value=%3
- set /a __section!addOneVariable!.child1.controlOutput=0
- set /a __section!addOneVariable!.childCount=1
- set /a __section!addOneVariable!.controlOutput=0
- set __sectionCount=!addOneVariable!
- )
- )
- exit /b 0
-
-
- :GetAddOneVariable
- if "%1"=="" (
- set /a addOneVariable=0
- ) else (
- if "%2"=="" (
- set /a addOneVariable=%1+1
- ) else (
- set /a addOneVariable=%1+%2
- )
- )
- exit /b 0
-
- :GetDecreaseOneVariable
- if "%1"=="" (
- set /a decreaseOneVariable=0
- ) else (
- if "%2"=="" (
- set /a decreaseOneVariable=%1-1
- ) else (
- set /a decreaseOneVariable=%1-%2
- )
- )
- exit /b 0
-
-
- :DeleteChildOfSection
- if "%2"=="" (
- echo need a section and a child.
- exit /b 1
- )
- call :GetExistSectionAndChild %1 %2
- if "!existSectionAndChild!"=="false" (
- echo not exist section and child.
- exit /b 1
- )
- set /a __section%__sectionSequence%.child%__childSequence%.controlOutput=1
- exit /b 0
-
-
- :DeleteChildOfSection1
- setlocal
- if "%2"=="" (
- echo need a section and a child.
- exit /b 1
- )
- call :GetExistSectionAndChild %1 %2
- if "!existSectionAndChild!"=="false" (
- echo not exist section and child.
- exit /b 1
- )
- set /a m=!__section%__sectionSequence%.childCount!-1
- for /l %%a in (%__childSequence%,1,%m%) do (
- set /a n=%%a+1
- for /l %%b in (!n!,1,!n!) do (
- set __section%__sectionSequence%.child%%a.name=!__section%__sectionSequence%.child%%b.name!
- set __section%__sectionSequence%.child%%a.value=!__section%__sectionSequence%.child%%b.value!
- set __section%__sectionSequence%.child%%a.controlOutput=!__section%__sectionSequence%.child%%b.controlOutput!
- )
- )
- set /a __section%__sectionSequence%.childCount=%m%
- type nul>!__yiwuyun.txt!
- echo set __section%__sectionSequence%.child%n%.controlOutput=>>!__yiwuyun.txt!
- echo set __section%__sectionSequence%.child%n%.name=>>!__yiwuyun.txt!
- echo set __section%__sectionSequence%.child%n%.value=>>!__yiwuyun.txt!
- echo set /a __section%__sectionSequence%.childCount=!__section%__sectionSequence%.childCount!>>!__yiwuyun.txt!
- for /l %%a in (%__childSequence%,1,!__section%__sectionSequence%.childCount!) do (
- echo set __section%__sectionSequence%.child%%a.name=!__section%__sectionSequence%.child%%a.name!>>!__yiwuyun.txt!
- echo set __section%__sectionSequence%.child%%a.value=!__section%__sectionSequence%.child%%a.value!>>!__yiwuyun.txt!
- echo set __section%__sectionSequence%.child%%a.controlOutput=!__section%__sectionSequence%.child%%a.controlOutput!>>!__yiwuyun.txt!
- )
- endlocal
- for /f "tokens=1 delims=" %%a in ('type !__yiwuyun.txt!') do (
- rem echo %%a
- %%a
- )
- if exist !__yiwuyun.txt! del !__yiwuyun.txt!
- exit /b 0
-
-
- :GetChildValueOfSection
- if "%2"=="" (
- echo need a section and a child.
- exit /b 1
- )
- call :GetExistSectionAndChild %1 %2
- if "!existSectionAndChild!"=="false" (
- echo not exist section and child.
- exit /b 1
- )
- set childValueOfSection=!__section%__sectionSequence%.child%__childSequence%.value!
- exit /b 0
-
-
-
- :IsSpaceLine
- call :GetStringTrimSpace
- if "!stringTrimSpace!"=="" (
- set isSpaceLine=true
- ) else (
- set isSpaceLine=false
- )
- exit /b 0
-
-
-
- :IsLegalEqualityExpression
- setlocal
- call :GetParseEqualityExpression
- if "!parseEqualityExpression!"=="true" (
- if "!stringEqualityBefore!"=="" (
- set isLegalEqualityExpression=false
- goto end
- ) else if "!stringEqualityAfter!"=="" (
- set isLegalEqualityExpression=false
- goto end
- ) else (
- set string=!stringEqualityBefore!
- call :IsLegalString
- if "!isLegalString!"=="fasle" (
- set isLegalEqualityExpression=false
- goto end
- ) else (
- set string=!stringEqualityBefore!
- call :GetStringTrimSpace
- set stringEqualityBefore=!stringTrimSpace!
-
- set string=!stringEqualityAfter!
- call :GetStringTrimSpace
- set stringEqualityAfter=!stringTrimSpace!
- if "!stringEqualityAfter!"=="" (
- set isLegalEqualityExpression=false
- goto end
- ) else (
- set isLegalEqualityExpression=true
- )
- )
- )
- ) else (
- set isLegalEqualityExpression=false
- )
- :end
- endlocal&set isLegalEqualityExpression=%isLegalEqualityExpression%&set stringEqualityBefore=%stringEqualityBefore%&set stringEqualityAfter=%stringEqualityAfter%
- exit /b 0
-
-
- :GetParseEqualityExpression
- setlocal
- set string=!string!
- call :GetStringMaxIndex
- for /l %%a in (0,1,!stringMaxIndex!) do (
- if "!string:~%%a,1!"=="=" (
- set stringEqualityBefore=!string:~0,%%a!
- set stringEqualityAfter=!string:~%%a!
- set stringEqualityAfter=!stringEqualityAfter:~1!
- set parseEqualityExpression=true
- goto end
- )
- )
- set parseEqualityExpression=false
- :end
- endlocal&set parseEqualityExpression=%parseEqualityExpression%&set stringEqualityBefore=%stringEqualityBefore%&set stringEqualityAfter=%stringEqualityAfter%
- exit /b 0
-
-
- :GetStringMaxIndex
- call :GetStringLength
- set /a stringMaxIndex=!stringLength!-1
- exit /b 0
-
-
-
- :IsLegalSection
- setlocal
- call :GetStringTrimSpace
- set string=!stringTrimSpace!
- call :GetStringLength
- if !stringLength! lss 3 (
- set __section.validity=false
- goto end
- )
- if not "!string:~0,1!"=="[" (
- set __section.validity=false
- goto end
- )
- if not "!string:~-1,1!"=="]" (
- set __section.validity=false
- goto end
- )
- set string=!string:~1,-1!
- call :IsLegalString
- if not "!isLegalString!"=="true" (
- set __section.validity=false
- goto end
- ) else (
- set __section.validity=true
- call :GetStringTrimSpace
- set __section.name=!stringTrimSpace!
- )
- :end
- endlocal&set __section.validity=%__section.validity%&set __section.name=%__section.name%
- exit /b 0
-
-
-
- :GetStringTrimSpace
- setlocal
- set string=!string!
- call :GetStringLeftTrimSpace
- set string=!stringLeftTrimSpace!
- call :GetStringRightTrimSpace
- set stringTrimSpace=!stringRightTrimSpace!
- endlocal&set stringTrimSpace=%stringTrimSpace%
- exit /b 0
-
-
-
- :GetStringLeftTrimSpace
- setlocal
- set string=!string!
- :startStringLeftTrimSpace
- if "!string:~0,1!"==" " (
- set string=!string:~1!
- ) else (
- goto endStringLeftTrimSpace
- )
- goto startStringLeftTrimSpace
- :endStringLeftTrimSpace
- set stringLeftTrimSpace=!string!
- endlocal&set stringLeftTrimSpace=%stringLeftTrimSpace%
- exit /b 0
-
-
-
- :GetStringRightTrimSpace
- setlocal
- set string=!string!
- :startStringRightTrimSpace
- if "!string:~-1,1!"==" " (
- set string=!string:~0,-1!
- ) else (
- goto endStringRightTrimSpace
- )
- goto startStringRightTrimSpace
- :endStringRightTrimSpace
- set stringRightTrimSpace=!string!
- endlocal&set stringRightTrimSpace=%stringRightTrimSpace%
- exit /b 0
-
-
-
- :GetStringTrimAllSpace
- setlocal
- set string=!string!
- call :GetStringTrimSpace
- set string=!stringTrimSpace!
- call :GetStringLength
- set /a n=!stringLength!
- set /a n=!n!-1
- for /l %%a in (0,1,!n!) do (
- if "!string:~%%a,1!"==" " (
- call :GetStringDeletedChar %%a
- set string= !stringDeletedChar!
- )
- )
- call :GetStringTrimSpace
- set stringTrimAllSpace=!stringTrimSpace!
- endlocal&set stringTrimAllSpace=%stringTrimAllSpace%
- exit /b 0
-
-
- :GetStringTrimRedundanceSpace
- setlocal
- set string=!string!
- call :GetStringTrimSpace
- set string=!stringTrimSpace!
- call :GetStringLength
- set /a n=!stringLength!
- set /a n=!n!-1
- for /l %%a in (0,1,!n!) do (
- if "!string:~%%a,1!"==" " (
- if "!space!"=="true" (
- call :GetStringDeletedChar %%a
- set string= !stringDeletedChar!
- )
- set space=true
- ) else (
- set space=false
- )
- )
- call :GetStringTrimSpace
- set stringTrimRedundanceSpace=!stringTrimSpace!
- endlocal&set stringTrimRedundanceSpace=%stringTrimRedundanceSpace%
- exit /b 0
-
- :GetStringDeletedChar
- setlocal
- set temp=!string!
- set temp1=!temp:~0,%1!
- if "%2"=="" (
- set temp2=!temp:~%1!
- ) else (
- set temp2=!temp:~%2!
- )
- set temp3=!temp2:~1!
- set string=!temp1!!temp3!
- set stringDeletedChar=!string!
- endlocal&set stringDeletedChar=%stringDeletedChar%
- exit /b 0
-
-
-
- :GetStringUpperCase
- setlocal
- set string=!string!
- call :GetStringLength
- set /a n=!stringLength!-1
- for /l %%a in (0,1,!n!) do (
- set char=!string:~%%a,1!
- call :IsLowerCase !char!
- if "!isLowerCase!"=="true" (
- call :GetUpperCase !char!
- set char=!upperCase!
- set temp1=!string:~0,%%a!
- set temp2=!string:~%%a!
- set temp3=!temp2:~1!
- set string=!temp1!!char!!temp3!
- )
- )
- )
- set stringUpperCase=!string!
- endlocal&set stringUpperCase=%stringUpperCase%
- exit /b 0
-
-
- :GetStringLowerCase
- setlocal
- set string=!string!
- call :GetStringLength
- set /a n=!stringLength!-1
- for /l %%a in (0,1,!n!) do (
- set char=!string:~%%a,1!
- call :IsUpperCase !char!
- if "!isUpperCase!"=="true" (
- call :GetLowerCase !char!
- set char=!lowerCase!
- set temp1=!string:~0,%%a!
- set temp2=!string:~%%a!
- set temp3=!temp2:~1!
- set string=!temp1!!char!!temp3!
- )
- )
- )
- set stringLowerCase=!string!
- endlocal&set stringLowerCase=%stringLowerCase%
- exit /b 0
-
-
- :GetUpperCase
- setlocal
- call :GetSequenceNumber %1
- set /a n=1
- 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 (
- if !sequenceNumber! equ !n! (
- set upperCase=%%a
- goto end
- ) else (
- set /a n=!n!+1
- )
- )
- :end
- endlocal&set upperCase=%upperCase%
- exit /b 0
-
-
- :GetLowerCase
- setlocal
- call :GetSequenceNumber %1
- set /a n=1
- 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 (
- if !sequenceNumber! equ !n! (
- set lowerCase=%%a
- goto end
- ) else (
- set /a n=!n!+1
- )
- )
- :end
- endlocal&set lowerCase=%lowerCase%
- exit /b 0
-
-
-
- :GetSequenceNumber
- setlocal
- set /a n=0
- 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 (
- if "%%a"=="%1" (
- set /a n=!n!+1
- set sequenceNumber=!n!
- goto end
- )
- set /a n=!n!+1
- )
- set /a n=0
- 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 (
- if "%%a"=="%1" (
- set /a n=!n!+1
- set sequenceNumber=!n!
- goto end
- )
- set /a n=!n!+1
- )
- :end
- endlocal&set sequenceNumber=%sequenceNumber%
- exit /b 0
-
-
- :IsLowerCase
- 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 (
- if "%1"=="%%a" (
- set isLowerCase=true
- goto endIsLowerCase
- ) else (
- set isLowerCase=false
- )
- )
- :endIsLowerCase
- exit /b 0
-
- :IsUpperCase
- 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 (
- if "%1"=="%%a" (
- set isUpperCase=true
- goto endIsUpperCase
- ) else (
- set isUpperCase=false
- )
- )
- :endIsUpperCase
- exit /b 0
-
- :IsAlphabet
- call :IsUpperCase %1
- if "!isUpperCase!"=="true" (
- set isAlphabet=true
- goto endIsAlphabet
- ) else (
- set isAlphabet=false
- )
- call :IsLowerCase %1
- if "!isLowerCase!"=="true" (
- set isAlphabet=true
- ) else (
- set isAlphabet=false
- )
- :endIsAlphabet
- exit /b 0
-
- :IsDigit
- for %%a in (0 1 2 3 4 5 6 7 8 9) do (
- if "%%a"=="%1" (
- set isDigit=true
- goto endIsDigit
- ) else (
- set isDigit=false
- )
- )
- :endIsDigit
- exit /b 0
-
-
- :IsSpecialChar
- for %%a in (_ . #) do (
- if "%%a"=="%1" (
- set isSpecialChar=true
- goto endIsSpecialChar
- ) else (
- set isSpecialChar=false
- )
- )
- :endIsSpecialChar
- exit /b 0
-
-
- :IsLegalString
- setlocal
- set string=!string!
- call :GetStringTrimSpace
- set string=!stringTrimSpace!
- call :IsLegalHeadChar !string:~0,1!
- if "!isLegalHeadChar!"=="true" (
- set isLegalString=true
- ) else (
- set isLegalString=false
- goto endIsLegalString
- )
- call :GetStringLength
- set /a n=!stringLength!-1
- for /l %%a in (0,1,!n!) do (
- call :IsLegalChar !string:~%%a,1!
- if "!isLegalChar!"=="true" (
- set isLegalString=true
- ) else (
- set isLegalString=false
- goto endIsLegalString
- )
- )
- :endIsLegalString
- endlocal&set isLegalString=%isLegalString%
- exit /b 0
-
-
- :IsLegalHeadChar
- if "%1"=="_" (
- set isLegalHeadChar=true
- goto endIsLegalHeadChar
- ) else (
- set isLegalHeadChar=false
- )
- call :IsAlphabet %1
- if "!isAlphabet!"=="true" (
- set isLegalHeadChar=true
- ) else (
- set isLegalHeadChar=false
- )
- :endIsLegalHeadChar
- exit /b 0
-
- :IsLegalChar
- call :IsDigit %1
- if "!isDigit!"=="true" (
- set isLegalChar=true
- goto endIsLegalChar
- ) else (
- set isLegalChar=false
- )
-
- call :IsAlphabet %1
- if "!isAlphabet!"=="true" (
- set isLegalChar=true
- goto endIsLegalChar
- ) else (
- set isLegalChar=false
- )
- call :IsSpecialChar %1
- if "!isSpecialChar!"=="true" (
- set isLegalChar=true
- ) else (
- set isLegalChar=false
- )
- :endIsLegalChar
- exit /b 0
-
-
-
- :GetStringLength
- setlocal
- set string=!string!
- set /a n=0
- :startGetStringLength
- if not "!string!"=="" (
- set string=!string:~1!
- set /a n=!n!+1
- goto startGetStringLength
- )
- set /a stringLength=!n!
- endlocal&set stringLength=%stringLength%
- exit /b 0
-
-
- :GetAmpersandPlaceNAndCount
- setlocal
- set string=!string!
- call :GetStringLength
- set /a nGetAmpersandPlaceNAndCount=!stringLength!-1
- set /a nAmpersand=1
- set /a ampersandPlace0=-1
- for /l %%a in (0,1,!nGetAmpersandPlaceNAndCount!) do (
- if "!string:~%%a,1!"=="&" (
- set /a ampersandPlace!nAmpersand!=%%a
- set /a nAmpersand+=1
- )
- )
- set /a ampersandPlace!nAmpersand!=!stringLength!
- set /a ampersandCount=!nAmpersand!-1
- type nul>!__yiwuyun.txt!
- for /l %%a in (0,1,!nAmpersand!) do (
- echo set /a ampersandPlace%%a=!ampersandPlace%%a!>>!__yiwuyun.txt!
- )
- echo set /a ampersandCount=!ampersandCount!>>!__yiwuyun.txt!
- endlocal
- for /f "tokens=1 delims=" %%a in ('type !__yiwuyun.txt!') do (
- rem echo %%a
- %%a
- )
- if exist !__yiwuyun.txt! del !__yiwuyun.txt!
- exit /b 0
-
-
- :GetAmpersandCommandNAndCount
- setlocal
- set string=!string!
- call :GetAmpersandPlaceNAndCount
- set /a next=0
- set /a ampersandCommandCount=!ampersandCount!+1
- set /a n=0
- :startGetAmpersandCommandNAndCount
- set /a beforePlace=!ampersandPlace%next%!
- set /a first=!beforePlace!+1
- set /a next=!next!+1
- set /a afterPlace=!ampersandPlace%next%!
- set /a second=!afterPlace!-!beforePlace!-1
- set /a n=!n!+1
- set ampersandCommand!n!=!string:~%first%,%second%!
- if !n! lss !ampersandCommandCount! goto startGetAmpersandCommandNAndCount
- type nul>!__yiwuyun.txt!
- for /l %%a in (1,1,!ampersandCommandCount!) do (
- echo set ampersandCommand%%a=!ampersandCommand%%a!>>!__yiwuyun.txt!
- )
- echo set /a ampersandCommandCount=!ampersandCommandCount!>>!__yiwuyun.txt!
- endlocal
- for /f "tokens=1 delims=" %%a in ('type !__yiwuyun.txt!') do (
- rem echo %%a
- %%a
- )
- if exist !__yiwuyun.txt! del !__yiwuyun.txt!
- exit /b 0
复制代码
|