WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

System INI and INI File Topics

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

UDF to Simplify Editing of INI files

Keywords: 	 INI files editing UDF

Question:

I'm using IniWritePvt inside a loop to string together several INI files. The last remaining thing I'd like to accomplish is to add a blank line between the end of one section and the beginning of the next for readability.

Answer:

Here is some code to simplify editing INI files:



#definefunction modIniFile(ofile)
   fs = filesize(ofile)
   bb = binaryalloc(fs+100)
   binaryread(bb, ofile)
   binaryreplace(bb, "[", strcat(@crlf, "["), @true)
   binarywrite(bb, ofile)
   binaryfree(bb)
   return
#endfunction
and here's a few other helpful functions I came up with when dealing with INI files...
#definefunction copyINIsection(ifile, section, ofile)

   srchstr = strcat("[", section, "]")

   fsi = filesize(ifile)
   binbuf1 = binaryalloc(fsi)
   binaryread(binbuf1, ifile)

   ;   look for the first horizontal rule we find...
   spos = 0
   spos = binaryindexex(binbuf1, 0, srchstr, @fwdscan, @true)

if spos > 0
;   now find the next left bracket
   epos = binaryindexex(binbuf1, spos+1, "[", @fwdscan, @true)
   if epos == -1 then epos = fsi

   region = binarypeekstr(binbuf1, spos, epos-spos)

   reglen = strlen(region)

   fso = filesize(ofile)
   bbo = binaryalloc(fso+reglen)
   binaryread(bbo, ofile)   
   binarypokestr(bbo, fso, region)

   binbuf1 = binaryfree(binbuf1)
   binarywrite(bbo, ofile)
   bbo = binaryfree(bbo)
endif

   return
#endfunction

#definefunction mergeINIsection(inputfile1, inputfile2, section, outputfile)

   datastr = ""

;   get the data from the first file

   srchstr = strcat("[", section, "]")

for x = 1 to 2
   region = ""
   if x == 1
      inputfile = inputfile1
   else
      inputfile = inputfile2
   endif

   fsi = filesize(inputfile)
   binbuf1 = binaryalloc(fsi)
   binaryread(binbuf1, inputfile)

   spos = 0
   ;   look for the first searchstr find...
   spos = binaryindexex(binbuf1, 0, srchstr, @fwdscan, @true)

if spos > 0
;   now find the next left bracket
   epos = binaryindexex(binbuf1, spos+1, "[", @fwdscan, @true)
;   message(inputfile, epos)
   if epos == -1 then epos = fsi

   region = binarypeekstr(BINBUF1, spos, epos-spos)
   region = strreplace(region, @crlf, @tab)
   region = itemsort(region, @tab)
   binbuf1 = binaryfree(binbuf1)

   for y = 1 to itemcount(region, @tab)
      thisitem = itemextract(y, region, @tab)
      if strindexnc(thisitem, "[", 1, @fwdscan) == 0 && strtrim(thisitem) <> "" && itemlocate(thisitem, datastr, @tab) == 0
         datastr = iteminsert(thisitem, -1, datastr, @tab)
      endif
   next
endif
next
   datastr = itemsort(datastr, @tab)
   for x = 1 to itemcount(datastr, @tab)
      thisline = itemextract(x, datastr, @tab)
      thiskey  = itemextract(1, thisline, "=")
      thisdata = itemextract(2, thisline, "=")
      iniwritepvt(section, thiskey, thisdata, outputfile)
   next
   return
#endfunction

More INI File Manipulation:

Question:

How to use "IniReadPvt" to obtain the list of variaibles under one label name in an ini file.

Example:-

An ini file named Filelist.ini and the content of it are :-

[FileList]
File1=Autoexec.bat
File2=Config.sys
[FileList] is a known fact where File1 and File2 are unknown.

How to use IniReadPvt to obtain File1 and File2 and their values under [FileList]

Answer:

#DefineFunction IniToList(IniFile)
   Sections=IniItemizePvt("",inifile)
   SectionsCount = itemcount(Sections, @tab)
   IniFileContent = ""
   For x=1 To SectionsCount
      ThisSection   = ItemExtract(x, Sections, @TAB)
      IniFileContent = strcat(IniFileContent, @tab, "[", ThisSection, "]")
      Keywords      = IniItemizePvt(ThisSection, inifile)
      KeywordsCount = itemcount(Keywords, @tab)
      For y=1 To KeywordsCount
         ThisKeyword= ItemExtract(y, Keywords, @TAB)
         ThisValue  = IniReadPvt(ThisSection, ThisKeyword, "???", inifile)
         IniFileContent = strcat(IniFileContent, @tab, ThisKeyword, "=", ThisValue)
      next
      IniFileContent = strcat(IniFileContent, @tab)
   Next
   RETURN itemremove(1, IniFileContent, @tab)
#EndFunction

inifile="win.ini"
IniFileContent = IniToList(IniFile)
A = AskItemList(IniFile, IniFileContent, @tab, @unsorted, @single)

exit

Article ID:   W15713
File Created: 2003:05:28:10:23:20
Last Updated: 2003:05:28:10:23:20