Jump to content
Sign in to follow this  
Ashok4u

Reading properties file

Recommended Posts

Ashok4u

I have a requirement to read a properties file(key, value pair) and while reading need to ignore comments.

Please provide sample code.

Thanks.

Ashok.

Share this post


Link to post
Share on other sites
Ashok4u

Here is my sample code. i dont no whether it's right approach.

$fileVariableName = FileOpen("Sample.txt", 0)

If $fileVariableName = -1 Then

MsgBox(0,"Failure","Failed to open file")

Exit

EndIf

While 1

$lineVariableName = FileReadLine($fileVariableName)

If @error = -1 Then ExitLoop

$result = StringInStr($lineVariableName, "=")

$len = StringLen($lineVariableName)

$left = StringLeft($lineVariableName, $result - 1)

If StringLen($left) > 0 Then

ConsoleWrite("" &$left )

$leftlen = StringLen($left)

$right = StringRight($lineVariableName, $len - $leftlen - 1)

ConsoleWrite(" -------- " &$right & @CRLF)

EndIf

WEnd

FileClose($fileVariableName)

Sample.txt:

abc=2

asdfasdf=192.11.13.4

asdfasdf=dc=vsp

aasssss=symavaya.dr.com

ttttttt=1

#

# Specifies the password for the login account. The login provides access to

# limited set of administration pages.

aaaaaaaaaaaa=7010a653770c9344

#

# Specifies the password for the login account. The login provides access to

# limited set of administration pages.

bbbbbbbbbbbbbbbbbbbbbb=7010a653770c9344

Share this post


Link to post
Share on other sites
nekkutta

is it an Ini file you are working with?, if it is, why not just use the File, Ini... functions?

here is a nicer way to handle just key=value pairs

$sRawData = FileRead("filename")
$asData = StringSplit($sRawData , @CRLF , 3)
For $i in $asData
    If StringLeft($i,1) = '#' Or $i = '' Or Not StringInStr($i,'=') Then ContinueLoop
    $key = StringLeft($i,StringInStr($i, '=') - 1)
    $value = StringTrimLeft($i,StringInStr($i, '='))
    ;do what ever you want with each key and value here
Next

[size="2"] "Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." - Brian Kernighan[/size]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.