Sign in to follow this  
Followers 0
pvr1

Newbie - Check 2 files and create 2 options

14 posts in this topic

#1 ·  Posted (edited)

Hi there, complete Newbie here! Any help most appreciated.

What I am trying to do is check an .ini file for an entry and a directory for a file. If both cases exist then to call a certain operation with a GUI if both do not exist then to call the GUI to do something else. Basically it is a search for previous user settings. I have tried many things but can't seem to get anything to work and am far too embarassed to post any of the code I tried here!

I have a settings file that with previous use gets written to

Global $SettingsFile = @ScriptDir & '.\settings.ini'

I want to look in the .ini file to see if there is a an entry for location

IniRead($SettingsFile, "config", "location", "NotFound")

and look for another file

FileExists(@ScriptDir & ".\configbackup\config.xml")

if there is an entry for "location" in the .ini AND "config.xml" exists then I ask the GUI to do one thing, if BOTH do not exist then ask the GUI to do another. I guess (but am not sure) that it owuld be easier to wrap this up in a function and then just call the function which I have tried as

Func check_previous_settings()

but all the attempts I have made at writing the function do not work :-( I am sure that I have missed something really simple but complete newbie! I have searched the forums etc but am not sure what to call what I am doing so hard to find an answer....

many thanks for any help or advice

Edited by pvr1

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

I'd do something like this:

$Location = IniRead($SettingsFile, "config", "location", "NotFound")

If $Location <> "NotFound" And FileExists(@ScriptDir & ".\configbackup\config.xml") Then
     ;Location and file exist do something
Else
     ;otherwise do something else
Endif

Edit: Just wanted to mention one other thing, there's no reason to put the "." in front of the first back slash after the @ScriptDir macro. The dot just tells Windows to look in current directory and if you run your script from another folder, it may not execute correctly.

Edited by BrewManNH

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

I'd do something like this:

$Location = IniRead($SettingsFile, "config", "location", "NotFound")

If $Location <> "NotFound" And FileExists(@ScriptDir & ".\configbackup\config.xml") Then
     ;Location and file exist do something
Else
     ;otherwise do something else
Endif

Edit: Just wanted to mention one other thing, there's no reason to put the "." in front of the first back slash after the @ScriptDir macro. The dot just tells Windows to look in current directory and if you run your script from another folder, it may not execute correctly.

Many thanks for your help and the tip on the "." not being needed. Something fo rme is still not working right.

I have this

;set location of file for previous user settings
Global $SettingsFile = @ScriptDir & '\settings.ini'

;check for previous config.xml location
$ConfigLocation = IniRead($SettingsFile, "config", "location", "NotFound")

I have set up this as a function

Func _check_previous_settings()
    If $ConfigLocation <> "NotFound" And FileExists(@ScriptDir & "\configbackup\config.xml") Then
        ;call etc
    Else
        ;call etc
    EndIf
EndFunc   ;==>_check_previous_settings

and then I call the function in the script

_check_previous_settings()

In testing if the "config.xml" exists and this info is in the settings.ini

[config]

location=

even though there is no information within the location= the script executes as if there was.

many thanks

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Hi pvr1,

if you take a look at the help file from IniRead:

Return Value

Success: Returns the requested key value.

Failure: Returns the default string if requested key not found.

... you will notice that it returns the default value only if the KEY is not found. Here an empty value is returned ( "" )

Maybe you want to try sth like this:

$ConfigLocation = IniRead($SettingsFile, "config", "location", "NotFound")
If $ConfigLocation = "" Then $ConfigLocation = "NotFound"
Edited by Hannes123

Regards,Hannes[spoiler]If you can't convince them, confuse them![/spoiler]

Share this post


Link to post
Share on other sites

Hi pvr1,

if you take a look at the help file from IniRead:

... you will notice that it returns the default value only if the KEY is not found. Here an empty value is returned ( "" )

Maybe you want to try sth like this:

$ConfigLocation = IniRead($SettingsFile, "config", "location", "NotFound")
If $ConfigLocation = "" Then $ConfigLocation = "NotFound"

Hi Hannes, the thing is I am quite embarassed because I did read the help file but I still did not understand the concept of the default value. I did not see what it means. I think now I am beginning to understand it......

thank you very much for your time.

Share this post


Link to post
Share on other sites

Hi pvr1,

well, sometimes if I don't understand the helpfile 100% I just go ahead and try the example at the bottom. Or, in this case with my(your) example data. That is usually the time I understand what is going on.

And ... You're welcome. :)


Regards,Hannes[spoiler]If you can't convince them, confuse them![/spoiler]

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

Hi, I do not know if I should post a new post or use this same thread with more questions I have!

With many thanks for previous help (this is a VERY helpful forum, with nice people) I now have exactly working what I was trying to 1st do:

;set location of data directory and return values
Global $DataDirectory = IniRead($SettingsFile, "config", "location", "NotFound")
If $DataDirectory = "" Then $DataDirectory = "NotFound"

;check at startup to see if previous settings are saved
Func _check_previous_settings_setGUI()
    If $DataDirectory <> "NotFound" And FileExists(@ScriptDir & "\configbackup\config.xml") Then
        _previous_GUI()
        _sh_setup_load_buttons()
    Else
        _setup_GUI()
        _sh_cancel_next_buttons()
    EndIf
EndFunc   ;==>_check_previous_settings_setGUI

then I can just call the function

_check_previous_settings_setGUI()

Now I am much further into the code and I have hit another problem!

I want to add a further value to the $DataDirectory which means if there is a value and run more script

so thinking that "*" means there is content for $DataDirectory I now have

;set location of data directory and return values
Global $DataDirectory = IniRead($SettingsFile, "config", "location", "NotFound")
If $DataDirectory = "" Then $DataDirectory = "NotFound"
If $DataDirectory = "*" Then $DataDirectory = "Located"

would this be right thing to do?

Next I want to run a check whether there is a value in the $DataDirectory, if there is not one then to locate the Data Directory and write this to the $SettingsFile, then to check that we now have a value for the $DataDirectory. Then if that check is successful to call a funtion named _success1 but if still there is no value for the $DataDirectory to return an error.

So far I have this, it seems to work and write the location to the settings but it does not seem to be checking that there is now a value for $DataDirectory and calling _success1. Have I got something very wrong? I bet I have!

;check for saved location of config.xml, if none, find location and save to settings. Report success or error
Func _loc_config_save_setts()
    If $DataDirectory = "NotFound" And FileExists(@AppDataCommonDir & "\NPVR\config.xml") Then
        IniWrite($SettingsFile, "config", "location", @AppDataCommonDir & "\NPVR\")
    ElseIf FileExists("C:\Users\Public\NPVR\config.xml") Then
        IniWrite($SettingsFile, "config", "location", "C:\Users\Public\NPVR\")
    ElseIf $DataDirectory = "Located" And FileExists($DataDirectory) Then
        _success1()
    ElseIf $DataDirectory = "NotFound" Then
        SetError(1)
    EndIf
EndFunc   ;==>_loc_config_save_setts
and am I using the SetError syntax correctly?

Many thanks in advance for any help and guidance.

Edited by pvr1

Share this post


Link to post
Share on other sites

Hi, I do not know if I should post a new post or use this same thread with more questions I have!

With many thanks for previous help (this is a VERY helpful forum, with nice people) I now have exactly working what I was trying to 1st do:

;set location of data directory and return values
Global $DataDirectory = IniRead($SettingsFile, "config", "location", "NotFound")
If $DataDirectory = "" Then $DataDirectory = "NotFound"

;check at startup to see if previous settings are saved
Func _check_previous_settings_setGUI()
    If $DataDirectory <> "NotFound" And FileExists(@ScriptDir & "\configbackup\config.xml") Then
        _previous_GUI()
        _sh_setup_load_buttons()
    Else
        _setup_GUI()
        _sh_cancel_next_buttons()
    EndIf
EndFunc   ;==>_check_previous_settings_setGUI

then I can just call the function

_check_previous_settings_setGUI()

Now I am much further into the code and I have hit another problem!

I want to add a further value to the $DataDirectory which means if there is a value and run more script

so thinking that "*" means there is content for $DataDirectory I now have

;set location of data directory and return values
Global $DataDirectory = IniRead($SettingsFile, "config", "location", "NotFound")
If $DataDirectory = "" Then $DataDirectory = "NotFound"
If $DataDirectory = "*" Then $DataDirectory = "Located"

would this be right thing to do?

Next I want to run a check whether there is a value in the $DataDirectory, if there is not one then to locate the Data Directory and write this to the $SettingsFile, then to check that we now have a value for the $DataDirectory. Then if that check is successful to call a funtion named _success1 but if still there is no value for the $DataDirectory to return an error.

So far I have this, it seems to work and write the location to the settings but it does not seem to be checking that there is now a value for $DataDirectory and calling _success1. Have I got something very wrong? I bet I have!

;check for saved location of config.xml, if none, find location and save to settings. Report success or error
Func _loc_config_save_setts()
    If $DataDirectory = "NotFound" And FileExists(@AppDataCommonDir & "\NPVR\config.xml") Then
        IniWrite($SettingsFile, "config", "location", @AppDataCommonDir & "\NPVR\")
    ElseIf FileExists("C:\Users\Public\NPVR\config.xml") Then
        IniWrite($SettingsFile, "config", "location", "C:\Users\Public\NPVR\")
    ElseIf $DataDirectory = "Located" And FileExists($DataDirectory) Then
        _success1()
    ElseIf $DataDirectory = "NotFound" Then
        SetError(1)
    EndIf
EndFunc   ;==>_loc_config_save_setts
and am I using the SetError syntax correctly?

Many thanks in advance for any help and guidance.

Actually I have this wrong because if there is already a value for the $DataDirectory I do not want to write it again.....

Share this post


Link to post
Share on other sites

Hi I think it is how I am trying to find if there is any content in the key in the ini file.

I have now tried this a different way with

;set location of data directory and return values
Global $DataDirLoc = IniRead($SettingsFile, "config", "location", "NotFound")
If $DataDirLoc = "" Then $DataDirLoc = "NotFound"
;### Tidy Error: next line creates a negative tablevel.
Else $DataDirLoc = "Located"
;### Tidy Error: next line creates a negative tablevel.
;### Tidy Error: next line creates a negative tablevel for the line after it.
EndIf

But I do not understand what I am doing wrong! I want to be able to do 2 things on whetehr there is an antry in th eini key or not. The "NotFound" as suggested is working great but I do not see how to set the alternative!

Many thanks and apologies from a Newbie!

Share this post


Link to post
Share on other sites

Hi pvr1,

try to format the lines correctly:

Global $DataDirLoc = IniRead($SettingsFile, "config", "location", "NotFound")
If $DataDirLoc = "" Then 
    $DataDirLoc = "NotFound"
Else 
    $DataDirLoc = "Located"
EndIf

You can use the simple If ... Then "Do something" only if there is no else.


Regards,Hannes[spoiler]If you can't convince them, confuse them![/spoiler]

Share this post


Link to post
Share on other sites

Hi pvr1,

try to format the lines correctly:

Global $DataDirLoc = IniRead($SettingsFile, "config", "location", "NotFound")
If $DataDirLoc = "" Then 
    $DataDirLoc = "NotFound"
Else 
    $DataDirLoc = "Located"
EndIf

You can use the simple If ... Then "Do something" only if there is no else.

Thank you so much Hannes, I see what I did now. Too many hours staring at the screen and I go cross-eyed and can't see for looking!

However as soon as I use this code(I even tried to put in a function to see if it made a difference) I get a false return.

What I mean is without the extra

Else

$DataDirLoc = "Located"

everything works OK but as soon as I put this in I do not get the correct result, if I delete the settings file completely the script does not read the $DataDirLoc = "NotFound" as 'not found' but treats it is $DataDirLoc = "Located"

this is the function that checks it

Func _check_previous_settings_setGUI()
    If $DataDirLoc <> "NotFound" And FileExists(@ScriptDir & "\configbackup\config.xml") Then
        _previous_GUI()
        _sh_setup_load_buttons_GUI()
    Else
        _setup_GUI()
        _sh_cancel_next_buttons_GUI()
    EndIf
EndFunc   ;==>_check_previous_settings_setGUI

With no settings file present it should go to _setup_GUI but instead it goes to _previous_GUI

many thanks

Share this post


Link to post
Share on other sites

Global $DataDirLoc = IniRead("settings.ini", "config", "location", "NotFound")

If $DataDirLoc = "" Then ;Key empty
    $DataDirLoc = "NotFound"
ElseIf $DataDirLoc <> "NotFound" Then ;Key found and filled
    $DataDirLoc = "Located"
Else ;Key or INI file is found
    $DataDirLoc = $DataDirLoc
EndIf

ConsoleWrite($DataDirLoc & @CRLF)

You can leave out the lines 7 and 8 to simplyfy your code. They are just there so you might understand the code better. :)


Regards,Hannes[spoiler]If you can't convince them, confuse them![/spoiler]

Share this post


Link to post
Share on other sites

Global $DataDirLoc = IniRead("settings.ini", "config", "location", "NotFound")

If $DataDirLoc = "" Then ;Key empty
    $DataDirLoc = "NotFound"
ElseIf $DataDirLoc <> "NotFound" Then ;Key found and filled
    $DataDirLoc = "Located"
Else ;Key or INI file is found
    $DataDirLoc = $DataDirLoc
EndIf

ConsoleWrite($DataDirLoc & @CRLF)

You can leave out the lines 7 and 8 to simplyfy your code. They are just there so you might understand the code better. :)

Hi again, I thank Hannes for all the help so far. This really is a very friendly forum with helpful people. And AutoIT is just brilliant for a Newbie!

I am now much further into my script, I have everything working that wasn't before and have used Hannes suggestion to check for Key content in my .ini file.

I now have found a new problem and ask for help again.

I have done much googling and forum reading but can't seem to find an answer.

What I am trying to do here is if there is not a saved location for the file I am looking for in my settings file, check default locations and if found write that to my settings .ini.

After this finally check that there is now a location in the settings file and then call another function. If it does not exist still (means the file was not found) then call an error so that user can do something else.

Currently it is in 2 functions. The problem is it actually works but the 2nd function runs very fast after the first. If the location already exists it works fine but if the first function has to write to file the 2nd function seems to check too fast and so does not work. So I appear to need some kind of delay between the 2 functions to give the first function time to write before the 2nd function checks.

I tried with sleep but this was no good as it is sleep. There does not appear to be a wait command. I also tried putting a 3 second timer between the 2 but this did not seem to work either. Would anyone have suggestions? I am also not sure how to create an error correctly and then be able to call from it.

;if no saved config.xml location found try to find location and save to settings ini
Func _loc_config_save_setts()
    If $DataDirLoc = "NotFound" And FileExists(@AppDataCommonDir & "\NPVR\config.xml") Then
        IniWrite($SettingsFile, "configloc", "location", @AppDataCommonDir & "\NPVR\")
    ElseIf FileExists("C:\Users\Public\NPVR\config.xml") Then
        IniWrite($SettingsFile, "configloc", "location", "C:\Users\Public\NPVR\")
    EndIf
EndFunc   ;==>_loc_config_save_setts

;check saved config.xml location now exists in settings ini, call success or error
Func _check_settings_loc_exists()
    If $DataDirLoc = "Located" And FileExists($ConfigLocation & "\config.xml") Then
        _locate_success_GUI()
        ;ElseIf write error
    EndIf
EndFunc   ;==>_check_settings_loc_exists

Share this post


Link to post
Share on other sites

Hi again!

with many thnaks to help here on the forum and loads of reading and looking at other scripts etc I have nearly finished my 1st script. However I would be really grateful if someone could point me in the right direction at finding out what I am missing/have got wrong with my script.

It is the same problem I have from the post above.

If I have previous settings saved for a file path in my settings.ini the script reads them fine and continues with the next functions. If the original file location cannot be found the user is prompted to locate the file and then this is saved to the .ini. All of this works fine. But the next function I call after the user has selcted the file path 'hangs' on checking the location.

If I close the script and run it again the file path is read fine and all is OK......

It is like the file path cannot be read once it is saved because the next function begins to look for it before it is saved? Or is may be something else I have totally missed!

The 2 functions in question are:

;opens file select dialog
Func _user_locate_config()
    $UserLocateCfg = FileOpenDialog($userlocatecfg_txt, @HomeDrive, "XML (*.xml)", 1 + 2, "config.xml")
    If @error Then
        MsgBox(4096, "", "No configuration file chosen")
    ElseIf $DataDirLoc = "NotFound" Then
        $UserLocateCfg = StringReplace($UserLocateCfg, "|", @CRLF)
        MsgBox(4096, "", "Configuration file location " & $UserLocateCfg)
        IniWrite($SettingsFile, "configloc", "location", $UserLocateCfg & @CRLF)
        _location_from_user_GUI()
        _check_settings_loc_exists()
    EndIf
EndFunc   ;==>_user_locate_config

;check saved config.xml location now exists in settings ini, call success or error
Func _check_settings_loc_exists()
    If $DataDirLoc = "Located" And FileExists($ConfigLocation) Then
        _config_locate_success_GUI()
        _rename_backup()
    EndIf
EndFunc   ;==>_check_settings_loc_exists

If anyone could help I would be most grateful. If other bits of sctipt are needed to look at just ask :)

With many thanks

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  
Followers 0