Code archives/File Utilities/Ini File Updater

This code has been declared by its author to be Public Domain code.

Download source code

Ini File Updater by Rob Farley
(Posted 23 years ago)
One command, Ini_File, this allows you to add to your ini file, append ini file values and get values back.

Usage:

To get a value out of an ini file:
value= ini_file(Filename,Command)
So if your ini file has a line
Particles=200
Then you would do something like
particles=ini_file("setup.ini","Particles")
This would then put the value to the varible particles.

Okay, now updated the value:
ini_file("setup.ini","Particles",50)
This will update the particles entry on the ini file to
Particles=50
On top of this if you do
ini_file("setup.ini","Particles",50,true
It will do the same but date and time stamp the change.

Further to this if you try to edit a value that is not present in the ini
ini_file("setup.ini","Fred",20)
This will add the entry of
Fred=20
If your value you want to amend or add is a string simply add a $ at the beginning of the value
ini_file("setup.ini","Fred","$Wilma")
This will then put " marks around the value, this is really useful if you're adding more of a message ie something with spaces in it
ini_file ("setup.ini","Gameovermes","$Game Over, You're dead and stuff")

I hope this all makes sense, give me a shout if you find it handy.

Cheers, Rob.
;ini file updater
; 2003 Mental Illusion
; http://www.mentalillusion.co.uk
; rob@mentalillusion.co.uk

; Usage:
; value of command = ini_file(Filename,Command)
;
; or
;
; ini_file(Filename,Command,New Value,[Date Stamp])


Function ini_file$(file$,cmd$,setting$="Null",date_stamp=False)

cmd=Lower(cmd)
found=False



filein = ReadFile(file$)

If setting<>"Null"
	fileout= WriteFile("temp.ini")
	If Left(setting,1)="$" Then setting=Chr(34)+Right(setting,Len(setting)-1)+Chr(34)
	If date_stamp Then setting=setting+" ; Updated "+CurrentDate()+" "+CurrentTime()
	EndIf

return_value$="NULL"

While Not Eof(filein)

temp$=ReadLine(filein)

If temp<>"" And Left(temp,1)<>";"
	command$=Lower(Left$(temp,Instr(temp,"=")-1)) ;extract command
	value$=Mid(temp,Instr(temp,"=")+1,(Instr(temp,";")-1)-(Instr(temp,"=")+1)) ;extract value
	If Left(value,1)=Chr(34)
		value=Mid(value,2,Instr(value,Chr(34),2)-2) ;extract a string if it is one
		Else
		If Instr(value," ")>0 Then value=Left(value,Instr(value," ")-1) ;trim spaces off value
		If Instr(value,Chr(9))>0 Then value=Left(value,Instr(value,Chr(9))-1) ;trim tabs off value
		EndIf

	If command=cmd
		found=True
		If setting="Null"
			return_value=value
			Else
			WriteLine(fileout,command+"="+setting)
			EndIf
		Else
		If setting<>"Null" Then WriteLine(fileout,temp)		
		EndIf
	Else
	If setting<>"Null" Then WriteLine(fileout,temp)
	EndIf

Wend

CloseFile (filein)

If setting="Null"
	Return return_value
	Else
	If found=False Then WriteLine(fileout,cmd+"="+setting)
	CloseFile (fileout)
	DeleteFile file
	CopyFile "temp.ini",file
	DeleteFile "temp.ini"
	EndIf

End Function