Page Date
2004-05-18
DD-Software
Kapitel zurück / previous Chapter
Main Index
 
Seite zurück / previous page
Backward
Seite vor / next page
Forward
 
Seitenanfang/TopOfPage
Top
Seitenende/EndOfPage
Bottom
MyWbtHelp current version

WinBatch Scripting - IniFile Managing



Seitenanfang/TopOfPage Seitenende/EndOfPage
Seitenanfang/TopOfPage Seitenende/EndOfPage

IniFile Section Managing

;------------------------------------------------------------------------------------------------------------------------------------------
; udfIniSectionCreate  (sSection, sIniFile)
; udfIniSectionDelete  (sSection, sIniFile)
;
; udfIniSectionRename1 (sSectionOld, sSectionNew, sIniFile)
; udfIniSectionRename  (sSectionOld, sSectionNew, sIniFile)
;
; udfIniSectionCopy    (sSection, sIniFileFrom, sIniFileTo)
; udfIniSectionMove    (sSection, sIniFileFrom, sIniFileTo)
; udfIniSectionMerge   (sSection, sIniFile1, sIniFile2, sIniFileMerge)
;
; udfIniSectionSort    (sSection, sIniFile)
;
; udfIniSectionAdjust1 (sIniFile)
; udfIniSectionAdjust  (sIniFile)
;------------------------------------------------------------------------------------------------------------------------------------------


;------------------------------------------------------------------------------------------------------------------------------------------
#DefineFunction udfIniSectionCreate (sSection, sIniFile)
IniWritePvt(sSection,"","",sIniFile)
IniDeletePvt(sSection,"",sIniFile)
IniWritePvt("","","",sIniFile)
Return (FileExist(sIniFile))
;..........................................................................................................................................
; This function udfIniSectionCreate creates a section header in inifile.
; If the section already exists nothing will be changed.
;
; Detlev Dalitz.20021127.
;..........................................................................................................................................
#EndFunction
;------------------------------------------------------------------------------------------------------------------------------------------


;------------------------------------------------------------------------------------------------------------------------------------------
#DefineFunction udfIniSectionDelete (sSection, sIniFile)
IniWritePvt("","","",sIniFile) ; Force flushing cache to disk.
TimeDelay(1)
IniDeletePvt(sSection,@WHOLESECTION,sIniFile)
IniWritePvt("","","",sIniFile)
Return (FileExist(sIniFile))
;..........................................................................................................................................
; This function udfIniSectionDelete deletes an entire section in inifile without permission.
;
; Detlev Dalitz.20021127.20030727.
;..........................................................................................................................................
#EndFunction
;------------------------------------------------------------------------------------------------------------------------------------------


;------------------------------------------------------------------------------------------------------------------------------------------
#DefineFunction udfIniSectionRename1 (sSectionOld, sSectionNew, sIniFile)
iFilesize = FileSize(sIniFile)
If !iFilesize Then Return (@FALSE) ; Quick way out.

IniWritePvt("","","",sIniFile) ; Force flushing cache to disk.
TimeDelay(1)

iBBsize = iFilesize + 2 + Max(0,(StrLen(sSectionNew)-StrLen(sSectionOld)))
hBB = BinaryAlloc(iBBsize)
BinaryPokeStr(hbb,0,@CRLF) ; A little helper, will be removed later.
If (iFilesize != BinaryReadEx(hBB,2,sIniFile,0,iFilesize)) Then Return (@FALSE) ; Quick way out.

; Left adjust all lines.
sSearch = StrCat(@CRLF," ")
sReplace = @CRLF
While BinaryReplace(hBB,sSearch,sReplace,@TRUE)
EndWhile

sBBTag = BinaryTagInit(hBB,StrCat(@LF,"[",sSectionOld),"]") ; Init section header to search.
sBBTag = BinaryTagFind(sBBTag) ; Find the section header.
iDone = @FALSE
If (sBBTag > "")
   sBBTag = BinaryTagRepl(sBBTag,StrCat(@LF,"[",sSectionNew,"]")) ; Rename section header.
   BinaryWriteEx(hBB,0,sIniFile,0,-1)
   iDone = (0 < BinaryWriteEx(hBB,2,sIniFile,0,BinaryEodGet(hBB)-2)) ; Overwrite inifile.
EndIf
BinaryFree(hBB)
Return (iDone && FileExist(sIniFile))
;..........................................................................................................................................
; This function udfIniSectionRename renames a given section header in inifile.
; This function requires, that lines of an inifile are delimited with @CRLF as eol marker.
;
; Detlev Dalitz.20021127.20030727.
;..........................................................................................................................................
#EndFunction
;------------------------------------------------------------------------------------------------------------------------------------------


;------------------------------------------------------------------------------------------------------------------------------------------
#DefineFunction udfIniSectionRename (sSectionOld, sSectionNew, sIniFile)
iFilesize = FileSize(sIniFile)
If !iFilesize Then Return (@FALSE) ; Quick way out.

IniWritePvt("","","",sIniFile) ; Force flushing cache to disk.
TimeDelay(1)

sIni = StrCat(@LF,FileGet(sIniFile))

; Left adjust all lines.
sSearch = StrCat(@LF," ")
sReplace = @LF
While StrIndex(sIni,sSearch,0,@FWDSCAN)
   sIni = StrReplace(sIni,sSearch,sReplace)
EndWhile

; Rename section header.
sIni = StrReplace(sIni,StrCat(@LF,"[",sSectionOld,"]"),StrCat(@LF,"[",sSectionNew,"]"))

; Write inifile to disk.
FilePut(sIniFile,StrSub(sIni,2,-1))

Return (FileExist(sIniFile))
;..........................................................................................................................................
; This function udfIniSectionRename renames a given section header in inifile.
; This function requires, that lines of an inifile are delimited with @CRLF as eol marker.
;
; Detlev Dalitz.20030802.
;..........................................................................................................................................
#EndFunction
;------------------------------------------------------------------------------------------------------------------------------------------


;------------------------------------------------------------------------------------------------------------------------------------------
#DefineFunction udfIniSectionCopy (sSection, sIniFileFrom, sIniFileTo)
IniWritePvt("","","",sIniFileFrom) ; Force flushing cache to disk.
TimeDelay(1)
sKeyList = IniItemizePvt (sSection, sIniFileFrom)
iKeyCount = ItemCount(sKeyList,@TAB)
For iKey=1 To iKeyCount
   sKey = ItemExtract(iKey,sKeyList,@TAB)
   sData = IniReadPvt(sSection,sKey,"",sIniFileFrom)
   IniWritePvt(sSection,sKey,sData,sIniFileTo)
Next
IniWritePvt("","","",sIniFileTo)
Return (FileExist(sIniFileTo))
;..........................................................................................................................................
; This function udfIniSectionCopy copies a whole section from one inifile to another inifile.
; The receiving inifile will be created automatically.
;
; Caution:
; If the target inifile "sIniFileTo" already contains a section with the same name,
; this function works like an overwriting merger function!
;
; Detlev Dalitz.20021127.20030727.
;..........................................................................................................................................
#EndFunction
;------------------------------------------------------------------------------------------------------------------------------------------


;------------------------------------------------------------------------------------------------------------------------------------------
#DefineFunction udfIniSectionMove (sSection, sIniFileFrom, sIniFileTo)
If udfIniSectionCopy (sSection, sIniFileFrom, sIniFileTo)
   udfIniSectionDelete (sSection, sIniFileFrom)
EndIf
Return (FileExist(sIniFileTo))
;..........................................................................................................................................
; This function udfIniSectionMove moves a whole section from one inifile to another inifile.
; The receiving inifile will be created automatically.
; After copying the whole section from first inifile to second inifile,
; the whole section will be deleted from the first inifile.
;
; Caution:
; If the target inifile "sIniFileTo" already contains a section with the same name,
; this function works like an overwriting merger function!
;
; Detlev Dalitz.20021127.
;..........................................................................................................................................
#EndFunction
;------------------------------------------------------------------------------------------------------------------------------------------


;------------------------------------------------------------------------------------------------------------------------------------------
#DefineFunction udfIniSectionMerge (sSection, sIniFile1, sIniFile2, sIniFileMerge)
udfIniSectionCopy (sSection, sIniFile1, sIniFileMerge)
udfIniSectionCopy (sSection, sIniFile2, sIniFileMerge)
Return (FileExist(sIniFileMerge))
;..........................................................................................................................................
; This function udfIniSectionMerge merges equally named sections of two inifiles
; together into a third target inifile.
; The target inifile will be created automatically.
; The input inifiles remain untouched.
;
; Merging is done by simple copying sections from inifiles to target mergefile.
; The position of the inifiles in the function's parameter list determines,
; which inifile will get higher priority for overwrite pre-existing data:
; Data from second inifile "sIniFile2" overwrites data from first inifile "sIniFile1".
;
; Detlev Dalitz.20021127.
;..........................................................................................................................................
#EndFunction
;------------------------------------------------------------------------------------------------------------------------------------------


;------------------------------------------------------------------------------------------------------------------------------------------
#DefineFunction udfIniSectionSort (sSection, sIniFile)
iFilesize = FileSize(sIniFile)
If !iFilesize Then Return (@FALSE) ; Quick way out.

IniWritePvt("","","",sIniFile) ; Force flushing cache to disk.
TimeDelay(1)

sKeyList = IniItemizePvt(sSection,sIniFile)
sKeyList = ItemSort(sKeyList,@TAB)
iKeyCount = ItemCount(sKeyList,@TAB)

; Create a temporary section with unique name.
sSectionTemp = StrCat(sSection,GetTickCount())
For iKey=1 To iKeyCount
   sKey = ItemExtract(iKey,sKeyList,@TAB)
   sData = IniReadPvt(sSection,sKey,"",sIniFile)
   IniWritePvt(sSectionTemp,sKey,sData,sIniFile)
Next
IniWritePvt("","","",sIniFile) ; Flush the inifile to disk.

; Delete original section.
IniDeletePvt(sSection,@WHOLESECTION,sIniFile)
IniWritePvt("","","",sIniFile) ; Flush the inifile to disk.

; Rename temporary section.
iDone = udfIniSectionRename(sSectionTemp,sSection,sIniFile)

Return (iDone && FileExist(sIniFile))
;..........................................................................................................................................
; This function udfIniSectionSort sorts a sections's keywords in ascending order
; for better human readability.
;
; Note: In Win9x operating systems the maximum size of an inifile is limited to 64kB.
; Make sure that the above function can work successful within this limit.
;
; Detlev Dalitz.20021127.20030727.
;..........................................................................................................................................
#EndFunction
;------------------------------------------------------------------------------------------------------------------------------------------


;------------------------------------------------------------------------------------------------------------------------------------------
#DefineFunction udfIniSectionAdjust1 (sIniFile)
iFilesize = FileSize(sIniFile)
If !iFilesize Then Return (@FALSE) ; Quick way out.

IniWritePvt("","","",sIniFile) ; Force flushing cache to disk.
TimeDelay(1)

sSectionList = IniItemizePvt("",sIniFile)
sSectionCount = ItemCount(sSectionList,@TAB)

iBBsize = iFilesize + 2 + (2*sSectionCount)
hBB = BinaryAlloc(iBBsize)
BinaryPokeStr(hbb,0,@CRLF) ; A little helper.
If (iFilesize != BinaryReadEx(hBB,2,sIniFile,0,iFilesize)) Then Return (@FALSE) ; Quick way out.

; Left adjust all lines.
sSearch = StrCat(@CRLF," ")
sReplace = @CRLF
While BinaryReplace(hBB,sSearch,sReplace,@TRUE)
EndWhile

; Insert blank line before section headers.
sSearch = StrCat(@CRLF,"[")
sReplace = StrCat(@CRLF,@CRLF,"[")
BinaryReplace(hBB,sSearch,sReplace,@TRUE)

; Force adjust to one blank line.
sSearch = StrCat(@CRLF,@CRLF,@CRLF,"[")
sReplace = StrCat(@CRLF,@CRLF,"[")
While BinaryReplace(hBB,sSearch,sReplace,@TRUE)
EndWhile

; Overwrite inifile.
BinaryWriteEx(hBB,0,sIniFile,0,-1)
iDone = (0 < BinaryWriteEx(hBB,4,sIniFile,0,BinaryEodGet(hBB)-4))

BinaryFree(hBB)
Return (iDone && FileExist(sIniFile))
;..........................................................................................................................................
; This function udfIniSectionAdjust adjusts the space between sections
; to a single blank line for better human readability.
;
; Detlev Dalitz.20021127.20030727.
;..........................................................................................................................................
#EndFunction
;------------------------------------------------------------------------------------------------------------------------------------------


;------------------------------------------------------------------------------------------------------------------------------------------
#DefineFunction udfIniSectionAdjust (sIniFile)
iFilesize = FileSize(sIniFile)
If !iFilesize Then Return (@FALSE) ; Quick way out.

IniWritePvt("","","",sIniFile) ; Force flushing cache to disk.
TimeDelay(1)

sSectionList = IniItemizePvt("",sIniFile)
sSectionCount = ItemCount(sSectionList,@TAB)

sIni = StrCat(@LF,FileGet(sIniFile))

; Left adjust all lines.
sSearch = StrCat(@LF," ")
sReplace = @LF
While StrIndex(sIni,sSearch,0,@FWDSCAN)
   sIni = StrReplace(sIni,sSearch,sReplace)
EndWhile

; Insert blank line before section headers.
sSearch = StrCat(@CRLF,"[")
sReplace = StrCat(@CRLF,@CRLF,"[")
sIni = StrReplace(sIni,sSearch,sReplace)

; Force adjust to one blank line.
sSearch = StrCat(@CRLF,@CRLF,@CRLF,"[")
sReplace = StrCat(@CRLF,@CRLF,"[")
While StrIndex(sIni,sSearch,0,@FWDSCAN)
   sIni = StrReplace(sIni,sSearch,sReplace)
EndWhile

; Write inifile to disk.
FilePut(sIniFile,StrSub(sIni,2,-1))

Return (FileExist(sIniFile))
;..........................................................................................................................................
; This function udfIniSectionAdjust adjusts the space between sections
; to a single blank line for better human readability.
;
; Detlev Dalitz.20030802.
;..........................................................................................................................................
#EndFunction
;------------------------------------------------------------------------------------------------------------------------------------------



; --- test ---
sBrowserExe = StrCat(DirHome(),"browser.exe")

:test1
sIniFile = "D:\temp\TestA.ini"
sSection = "test"

iResult = udfIniSectionCreate (sSection, sIniFile)
RunWait(sBrowserExe,sIniFile)

iResult = udfIniSectionDelete (sSection, sIniFile)
RunWait(sBrowserExe,sIniFile)

iResult = udfIniSectionCreate (sSection, sIniFile)
RunWait(sBrowserExe,sIniFile)

sSectionOld = "test"
sSectionNew = "renamed"
iResult = udfIniSectionRename (sSectionOld, sSectionNew, sIniFile)
RunWait(sBrowserExe,sIniFile)

sSectionOld = "renamed"
sSectionNew = "test"
iResult = udfIniSectionRename (sSectionOld, sSectionNew, sIniFile)
RunWait(sBrowserExe,sIniFile)


:test2
sIniFileFrom = "D:\temp\TestA.ini"
sIniFileTo   = "D:\temp\TestB.ini"
sSection = "test"

FileDelete(sIniFileFrom)
IniWritePvt("A","key","data",sIniFileFrom)
IniWritePvt(sSection,"key1","data1",sIniFileFrom)
IniWritePvt(sSection,"key2","data2",sIniFileFrom)
IniWritePvt(sSection,"key3","data3",sIniFileFrom)
IniWritePvt("","","",sIniFileFrom)
RunWait(sBrowserExe,sIniFileFrom)

FileDelete(sIniFileTo)
iResult = udfIniSectionCopy (sSection, sIniFileFrom, sIniFileTo)
RunWait(sBrowserExe,sIniFileTo)


FileDelete(sIniFileTo)
iResult = udfIniSectionMove (sSection, sIniFileFrom, sIniFileTo)
RunWait(sBrowserExe,sIniFileFrom)
RunWait(sBrowserExe,sIniFileTo)


:test3
sIniFileA = "D:\temp\TestA.ini"
sIniFileB = "D:\temp\TestB.ini"
sIniFileMerge = "D:\temp\TestC.ini"
sSection = "test"

FileDelete(sIniFileA)
IniWritePvt("A","key","data",sIniFileA)
IniWritePvt(sSection,"key11","data1",sIniFileA)
IniWritePvt(sSection,"key22","data2",sIniFileA)
IniWritePvt(sSection,"key33","data3",sIniFileA)
IniWritePvt("","","",sIniFileA)
RunWait(sBrowserExe,sIniFileA)

FileDelete(sIniFileB)
IniWritePvt("B","key","data",sIniFileB)
IniWritePvt(sSection,"key11","data11",sIniFileB)
IniWritePvt(sSection,"key99","data99",sIniFileB)
IniWritePvt(sSection,"key88","data88",sIniFileB)
IniWritePvt("","","",sIniFileB)
RunWait(sBrowserExe,sIniFileB)

FileDelete(sIniFileMerge)
iResult = udfIniSectionMerge (sSection, sIniFileA, sIniFileB, sIniFileMerge)
RunWait(sBrowserExe,sIniFileMerge)

FileDelete(sIniFileMerge)
iResult = udfIniSectionMerge (sSection, sIniFileB, sIniFileA, sIniFileMerge)
RunWait(sBrowserExe,sIniFileMerge)


:test4
sIniFile = "D:\temp\TestA.ini"

iResult = udfIniSectionAdjust (sIniFile)
RunWait(sBrowserExe,sIniFile)


:test5
sIniFile = "D:\temp\TestA.ini"
sSection = "test"

FileDelete(sIniFile)
IniWritePvt("A","key","data",sIniFile)
IniWritePvt(sSection,"key33","data3",sIniFile)
IniWritePvt(sSection,"key11","data1",sIniFile)
IniWritePvt(sSection,"key22","data2",sIniFile)
IniWritePvt("","","",sIniFile)
RunWait(sBrowserExe,sIniFile)

iResult = udfIniSectionSort (sSection, sIniFile)
RunWait(sBrowserExe,sIniFile)

Exit
;------------------------------------------------------------------------------------------------------------------------------------------
;*EOF*





Page Date
2004-05-18
DD-Software
Kapitel zurück / previous Chapter
Main Index
 
Seite zurück / previous page
Backward
Seite vor / next page
Forward
 
Seitenanfang/TopOfPage
Top
Seitenende/EndOfPage
Bottom
MyWbtHelp current version