Jump to content

writing and reading *.ini Files IniWrite() , IniRead


DssLexius
 Share

Recommended Posts

Friends, my Question may be very stupid but this is very important for me.

the problem is that i dont want my file to run again from the same Directory but on changing Location it will run.

For this i have read the HelpFile for Functions mentioned in my Thread title but i could not understand them correctly.

before running my program will create a *.ini file in @DocumentsDir with the path of it and next time before running will check if the Path Exists or not. if there will be the same path where from the current exe is running then it will give a message and close itself otherwise will run correctly.

i guess the ini file will be like this

[Path]
Path=C:\myProg.exe

if the ini file will have something like this then my script will not run from

C:\

but on copying it or moving it to Drive

D:\New Folder\My AutoIt Stuff\

it will work.

can anyone give me some example on how can i do it? any help will be very appreciated and thanked.

Edited by DssLexius
Link to comment
Share on other sites

Friends, my Question may be very stupid but this is very important for me.

the problem is that i dont want my file to run again from the same Directory but on changing Location it will run.

For this i have read the HelpFile for Functions mentioned in my Thread title but i could not understand them correctly.

before running my program will create a *.ini file in @DocumentsDir with the path of it and next time before running will check if the Path Exists or not. if there will be the same path where from the current exe is running then it will give a message and close itself otherwise will run correctly.

i guess the ini file will be like this

[Path]
Path=C:\myProg.exe

if the ini file will have something like this then my script will not run from

C:\

but on copying it or moving it to Drive

D:\New Folder\My AutoIt Stuff\

it will work.

can anyone give me some example on how can i do it? any help will be very appreciated and thanked.

Something like this near the start of your script should work, if I have understood your requirements correctly.

;Check the path used last time script was run
$LastRunPath = IniRead( @DocumentsCommonDir & "\Myini.ini", "LastPath", "Path", "" )
If @ScriptFullPath = $LastRunPath Then
    MsgBox(0,"My Title", "Script was run from" & @CRLF & $LastRunPath & @CRLF & "Please change location")
    Exit
Else
    ;Update the path in the ini file for next time 
    IniWrite( @DocumentsCommonDir & "\Myini.ini", "LastPath", "Path", @ScriptFullPath )
EndIf

; run the rest of your code

"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to build bigger and better idiots. So far, the universe is winning."- Rick Cook

Link to comment
Share on other sites

Something like this near the start of your script should work, if I have understood your requirements correctly.

;Check the path used last time script was run
$LastRunPath = IniRead( @DocumentsCommonDir & "\Myini.ini", "LastPath", "Path", "" )
If @ScriptFullPath = $LastRunPath Then
    MsgBox(0,"My Title", "Script was run from" & @CRLF & $LastRunPath & @CRLF & "Please change location")
    Exit
Else
    ;Update the path in the ini file for next time 
    IniWrite( @DocumentsCommonDir & "\Myini.ini", "LastPath", "Path", @ScriptFullPath )
EndIf

; run the rest of your code
Thanks a lot for it but it only saves one path each time (LastPath).

i want that after one location it should never Run again from same location.

it works but after changing file location when you place it back to previous location then it runs but i dont want that it should run again from same location. thanks for your help.

Link to comment
Share on other sites

Thanks a lot for it but it only saves one path each time (LastPath).

i want that after one location it should never Run again from same location.

it works but after changing file location when you place it back to previous location then it runs but i dont want that it should run again from same location. thanks for your help.

When you want to use an INI file, use

- one section

- write a value for every program start. Name should be some kind of timestamp, value = CurrentDirectoriesPath

- on Program Start you do a IniReadSection() rather then an IniRead()

You might also have a look at RegRead() and RegWrite(), which will not be as obvious as an INI file. Be aware, that you will need to create a key e.g. HKLM\Software\Yourname\ where any authenticated user will have to have write access to.

Or drop a "myprog-done.txt" file in each directory that's done.

Be aware that all these "security" precautions aren't really safe. With a little brains your users can bypass such things very easyly.

Regards, Rudi.

Earth is flat, pigs can fly, and Nuclear Power is SAFE!

Link to comment
Share on other sites

Thanks a lot for it but it only saves one path each time (LastPath).

i want that after one location it should never Run again from same location.

it works but after changing file location when you place it back to previous location then it runs but i dont want that it should run again from same location. thanks for your help.

This should work for multiple locations. It uses a simple text file to store the paths, because there are size limits on ini file sections that may cause you problems if you want to store a lot of paths.

Not tested

;Get list of paths already run from
$RunPaths = FileRead(@DocumentsCommonDir & "\MyPathsUsed.txt")
    ;Check if path has already been run
    If StringInStr($RunPaths, @ScriptFullPath) <> 0 Then
        MsgBox(0, "My Title", "Script was run from" & @CRLF & @ScriptFullPath & @CRLF & "Please change location")
        Exit
    Else
        ;Add the path used to the file for next time
        FileWriteLine(@DocumentsCommonDir & "\MyPathsUsed.txt", @ScriptFullPath)
    EndIf


;Run Script code

Edit: Removed extraneous EndIf from end of code

Edited by Bowmore

"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to build bigger and better idiots. So far, the universe is winning."- Rick Cook

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...