Notifications
Clear all

[Closed] Is there a better way to do this?

struct StatStruct (otherOpen0)
Stat = StatStruct()

                                                check_version = openFile ("c:\\proyect\\proyectconfig.ini") mode:"r"
                                                seek check_version 0
                                                skipToString check_version "project="
                                                            
                                                if ((not (eof check_version))) do
                                                    (
                                                     Stat = (readline check_version) as string
                                                     seek check_version 0
                                                    )
                                                messagebox stat

i am wonder if are a better way to do this, i am searching for the name of the proyect on the file “proyectconfig.ini”

10 Replies

If the INI file has correct [Categories], then you can use getIniSetting(). Otherwise, there are several ways to do what you did, and none is necessarily better.

If YOU are creating the .ini file and have control over its structure, then I suggest using the INI settings methods. If the file was made by another application, then you are stuck with it…

5 Replies
(@ecarter)
Joined: 11 months ago

Posts: 0

the ini file is created on other program, i can’t modify that file

(@panayot)
Joined: 11 months ago

Posts: 0

as i see its not standard ini file, so… may try this:

file = openFile "c:\\proyect\\proyectconfig.ini" mode:"r"
vers = (filterString (readLine file) "=")[2]
close file
messageBox vers 
(@ecarter)
Joined: 11 months ago

Posts: 0

thanks i try to implement this, thanks for your help

(@bobo)
Joined: 11 months ago

Posts: 0

Then your code is pretty much what you need. A bunch of observations:
*You don’t need to do seek() to 0 because when you open the file, it is at 0 already, and after you find the entry you don’t have to waste time returning to the beginning again.
*But be sure to call close() on the file after you are done with it, otherwise it could get locked.
*There is no need to convert the result to string since reading a line from a file returns a string anyway.

Other than that, if it is working, don’t touch it.

If you intend to find more data than just the project name, you could generalize the code to loop through all lines, filter by “=” string and check the resulting array’s first element for the property name and store the second element as the value in a struct or whatever. That would be a bit slower though since your code jumps to the right line faster with the seek() than a for loop would…

(@ecarter)
Joined: 11 months ago

Posts: 0

thanks for the help, and yes, the code work, but i like to try to optimize

 eek

What Bobo said,

[b][b][color=white][b]

[size=5][color=white][color=white][font=Verdana]Accessing INI File Keys[/color][/b]

[left]An INI File is a pure ASCII Text file typically used to store settings of User Interface states or any arbitrary information. The data is organized in Categories containing any number of Keys and corresponding Values. [/left]

[left]MAXScript provides a very error-tolerant set of functions to manage INI Files. For example, setting a new category and key in an non-existent INI file will automatically generate a new file without the need for any additional management function calls. Getting a non-existent key value from an existing file or even from a non-existent file will return an empty string without causing an error and so on.[/left]

[color=white]The following methods provide read and write access to Categories, Keys and Values in INI files:[/color]

[left]getINISetting <filename_string> <section_string> <key_string>[/left]

[left]Reads an INI setting from the specified file. Within the file, the section identified by <section_string> is located, and then the key specified by the <key_string> is located. The value assigned to this key is then returned as a string. [/left]

[left]If the specified file, section, or key is not found, an empty string “” is returned.[/left]

[left]EXAMPLE[/left]

[left]GetINISetting “c:/3dsmax6/3dsmax.ini” “Directories” “Scenes”

[/left]
[left]In 3ds Max 6 and higher, if only the .ini file is specified, the method will return an array of the sections in the .ini file. [/left]

[left]In 3ds Max 6 and higher, if only the .ini file and section are specified, the method will return an array of the keys in the section.[/left]

[left][/left]

[left]setINISetting <filename_string> <section_string> <key_string> <key_value_string>[/left]

[left]Sets an INI setting in the specified file. Returns a value of true if the key value was written to the file, false if the key was not written. Within the file, the section identified by <section_string> is located, and then the key specified by the <key_string> is located. The <key_value_string> is then assigned to this key. [/left]

[left]EXAMPLE[/left]

[left][/left]

[left]setINISetting “c:/3dsmax6/3dsmax.ini” “Directories” “Scenes” “c:/3dsmax/scenes”[/left]

[left]If the specified file, section, or key is not found, a new file, section, or key is created.[/left]

[left]If the specified file is read-only, or the file is open in MAXScript, the key value is not written to the file. [/left]

[left]The method returns true when writing has been successful, false when writing has failed.[/left]

[left][/left]

[left]delIniSetting <filename_string> <section_string> <key_string> [/left]

[left]Deletes the specified key from the supplied section of the given INI file.[/left]

[left]delIniSetting <filename_string> <section_string> [/left]

[left]Deletes the section and all keys in the section of the given INI file.[/left]

[left][/left]

[left]hasINISetting <filename_string> <section_string> [<key_string>][/left]

[left]Returns true if the INI setting exists in the specified file. [/left]

[left]This method is available in 3ds Max 2008 and higher.[/left]

[left]Within the file, the section identified by <section_string> is located, and then the key specified by the <key_string> is tested for. [/left]

[left]If <key_string> is not specified, the section specified by <section_string> is tested for.[/left]

[left][/left]

[left]Related Methods:[/left]

[left][b]getMAXIniFile/b[/left]

[left]In 3ds Max 6 and higher, returns the current 3dsmax.ini file as a string. For some products that store the ini settings in the registry, this method returns undefined.[/left]

[/b][/font][/color][/color][/size][/b]</H1>

Had a strange encounter here.

settings = for i = 1 to 30 collect i
setINISetting Path “UI” “Settings” (settings as string)

the key written to file:
settings = #(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, …)

Do i really have to build the string first if the count of an array is over 20?
Or am i doing this the wrong way?

Seems strange it writes what it shown in the listener instead of the actual full array.

should anyone wonder:
with PrintAllElements on settings as string

1 Reply
(@bobo)
Joined: 11 months ago

Posts: 0

Glad you found your answer!
And you can call execute() when reading that string back to turn it into an array again.