Did you know that you can
interact with INI files with ColdFusion?
This tutorial will demonstrate how to write and
read from INI files.
Now you may be saying, why would I want to
interact with INI files, when I can use a database? Well, the answer is
simple... If you are running software that runs off of an INI (i.e. an FTP
server) you can easily interact with the FTP servers INI files to easily modify
users information. In this example we will be using fictional INI file
information. Let's begin:
The first thing I will show you is what an INI
file looks like, let's imagine that this INI file is called settings.ini and is
located in the following path (C:\windows\settings.ini):
[settings]
user=John
password=Hello
path=C:\John\
The example above is a simple INI file, some
tend to be a bit more complex, but this is just so you get the idea of how it's
done ;)
If you wanted to read the values, you would use
GetProfileString(INIPATH, SECTION, ENTRY).
The easiest way to do this is as follows:
<cfoutput>
Username :
#GetProfileString("C:\windows\settings.ini", "settings",
"user")#<BR>
Password :
#GetProfileString("C:\windows\settings.ini", "settings",
"password")#<BR>
FTP Path :
#GetProfileString("C:\windows\settings.ini", "settings",
"path")#<BR>
</cfoutput>
The will display as follows:
Username : John
Password : Hello
FTP Path : C:\John\
There, wasn't that easy? The next step is to
actually write to the INI file and therefore make is useful :)
The write to an INI file, you will use the SetProfileString(inipath, section, entry, value).
Let's see an example of this:
<cfscript>
user = SetProfileString("C:\windows\settings.ini",
"settings", "user", "#FORM.NEWUSERNAME#")
Pass = SetProfileString("C:\windows\settings.ini",
"settings", "user", "#FORM.NEWPASSWORD#")
Path = SetProfileString("C:\windows\settings.ini",
"settings", "user", "#FORM.NEWPATH#")
</cfscript>
That will allow you to easily integrate your
ColdFusion applications with other software titles that are INI driven! The best
example as I mentioned before are FTP servers (a few you can try Serv-U, G 6 and
there are a few more!)
Well, you now understand the concept of GetProfileString()
& SetProfileString().....
Questions? Comments? Email Me....
Date added: Fri. October 4, 2002
Posted by: Pablo Varando | Views: 17855 | Tested Platforms: CF5 | Difficulty: Intermediate
Other
Functions
|