AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

if FileExist?

 
Reply to topic    AutoHotkey Community Forum Index -> Ask for Help
View previous topic :: View next topic  
Author Message
bpmb



Joined: 13 Sep 2009
Posts: 30

PostPosted: Wed Sep 23, 2009 9:02 pm    Post subject: if FileExist? Reply with quote

Hi there!

I can't get my head around this one, maybe it's my english but I don't seem to be able to understand IF. I tried the following to show a gui depending on whether a file exists:

Code:
If FileExist("%A_AppData%\Folder\config.ini") = "%A_AppData%\Folder\config.ini"
{
IniRead, BrowseSource, %A_AppData%\Folder\config.ini, Sourcefolder, key
IniRead, BrowseDest, %A_AppData%\Folder\config.ini, Destinationfolder, key
MsgBox %BrowseSource%`t%BrowseDest%
Gui, Add, Text,, Do you want to backup your default folders?
Gui, Add, Button, , Yes
Gui, Add, Button, , No
Gui, Show
}
else
{
IfNotExist %A_AppData%\Folder
FileCreateDir, %A_AppData%\Folder
IfNotExist %A_AppData%\Folder\config.ini
FileAppend, , %A_AppData%\Folder\config.ini
Gui, Destroy
Gui, Add, Text,, Select the source folder:
Gui, Add, Edit, r1 w300 vSourceEdit,
Gui, Add, Button, gSource, Select
Gui, Add, Text,, Select the destination folder:
Gui, Add, Edit, r1 w300 vDestEdit,
Gui, Add, Button, gDest, Select
Gui, Add, Button, x+160, OK
Gui, Show, w320 h200
}
Return


Somehow when I run this and the ini file is already there it still shows me the Gui created after ELSE? I also tried setting the FileExist() = 1 thinking it would only return True or False (which I also tried)..

It's been a very long day, maybe I'll crack it tomorrow but suggestions are always welcome!
Back to top
View user's profile Send private message
Z_Gecko
Guest





PostPosted: Wed Sep 23, 2009 9:08 pm    Post subject: Reply with quote

Help wrote:
FileExist(FilePattern): Returns a blank value (empty string) if FilePattern does not exist (FilePattern is assumed to be in A_WorkingDir if an absolute path isn't specified). Otherwise, it returns the attribute string (a subset of "RASHNDOCT") of the first matching file or folder. If the file has no attributes (rare), "X" is returned.
Back to top
bpmb



Joined: 13 Sep 2009
Posts: 30

PostPosted: Wed Sep 23, 2009 9:20 pm    Post subject: Reply with quote

hmmm OK, so it returns an Atrib value.. I now tried using IfExist instead but still can't get it to work..?
Back to top
View user's profile Send private message
Z_Gecko
Guest





PostPosted: Wed Sep 23, 2009 9:23 pm    Post subject: Reply with quote

how? (means show the code)
Back to top
HotKeyIt



Joined: 18 Jun 2008
Posts: 4652
Location: AHK Forum

PostPosted: Wed Sep 23, 2009 9:24 pm    Post subject: Reply with quote

Try:
Code:
If (FileExist("%A_AppData%\Folder\config.ini") = A_AppData . "\Folder\config.ini"){
   IniRead, BrowseSource, %A_AppData%\Folder\config.ini, Sourcefolder, key
   IniRead, BrowseDest, %A_AppData%\Folder\config.ini, Destinationfolder, key
   MsgBox %BrowseSource%`t%BrowseDest%
   Gui, Add, Text,, Do you want to backup your default folders?
   Gui, Add, Button, , Yes
   Gui, Add, Button, , No
   Gui, Show
} else {
   If (FileExist(A_AppData "\Folder\config.ini")!="D"){
      FileCreateDir, %A_AppData%\Folder
      FileAppend, , %A_AppData%\Folder\config.ini
   }
   Gui, Destroy
   Gui, Add, Text,, Select the source folder:
   Gui, Add, Edit, r1 w300 vSourceEdit,
   Gui, Add, Button, gSource, Select
   Gui, Add, Text,, Select the destination folder:
   Gui, Add, Edit, r1 w300 vDestEdit,
   Gui, Add, Button, gDest, Select
   Gui, Add, Button, x+160, OK
   Gui, Show, w320 h200
}
Return

_________________
AHK_H (2alpha) AHF TT _Struct WatchDir Yaml _Input ObjTree RapidHotkey DynaRun Wink
Back to top
View user's profile Send private message
bpmb



Joined: 13 Sep 2009
Posts: 30

PostPosted: Wed Sep 23, 2009 9:34 pm    Post subject: Reply with quote

Thanks, I tried you code but it still shows me the second GUI created after ELSE ? I can't see what's going wrong?
Back to top
View user's profile Send private message
randallf



Joined: 06 Jul 2009
Posts: 678

PostPosted: Wed Sep 23, 2009 9:40 pm    Post subject: Re: if FileExist? Reply with quote

bpmb wrote:

Somehow when I run this and the ini file is already there it still shows me the Gui created after ELSE?


If the code you are showing us is a subroutine that is part of a larger script then you are not destroying the GUI before you show it again,

i.e. the second part of the GUI adds those elements and then when it comes back around to GUI, SHOW in the first part it simply displays what has been built.

IF that is the case (pardon the pun) then do this: (maybe)

Code:
If (FileExist("%A_AppData%\Folder\config.ini") = A_AppData . "\Folder\config.ini"){
   IniRead, BrowseSource, %A_AppData%\Folder\config.ini, Sourcefolder, key
   IniRead, BrowseDest, %A_AppData%\Folder\config.ini, Destinationfolder, key
   MsgBox %BrowseSource%`t%BrowseDest%

   Gui, Destroy

   Gui, Add, Text,, Do you want to backup your default folders?
   Gui, Add, Button, , Yes
   Gui, Add, Button, , No
   Gui, Show
} else {
   If (FileExist(A_AppData "\Folder\config.ini")!="D"){
      FileCreateDir, %A_AppData%\Folder
      FileAppend, , %A_AppData%\Folder\config.ini
   }
   Gui, Destroy
   Gui, Add, Text,, Select the source folder:
   Gui, Add, Edit, r1 w300 vSourceEdit,
   Gui, Add, Button, gSource, Select
   Gui, Add, Text,, Select the destination folder:
   Gui, Add, Edit, r1 w300 vDestEdit,
   Gui, Add, Button, gDest, Select
   Gui, Add, Button, x+160, OK
   Gui, Show, w320 h200
}
Return


You also should just be using msgbox...

replace this

Code:
   Gui, Destroy
   Gui, Add, Text,, Do you want to backup your default folders?
   Gui, Add, Button, , Yes
   Gui, Add, Button, , No
   Gui, Show


with this

Code:
msgbox, 1,, Do you want to backup your default folders?


you can then use

Code:
IfMsgBox Yes
{
msgbox you pressed yes!
}
ELSE
{
msgbox you pressed no!
}

Etc
Back to top
View user's profile Send private message
Z_Gecko
Guest





PostPosted: Wed Sep 23, 2009 9:58 pm    Post subject: Reply with quote

You are still not using the right syntax for FileExist()

using IfExist might be easier, e.g.
Code:
IfExist, %A_AppData%\Folder\config.ini
Back to top
HotKeyIt



Joined: 18 Jun 2008
Posts: 4652
Location: AHK Forum

PostPosted: Thu Sep 24, 2009 4:49 am    Post subject: Reply with quote

Embarassed
Code:
If FileExist("%A_AppData%\Folder\config.ini"){
   IniRead, BrowseSource, %A_AppData%\Folder\config.ini, Sourcefolder, key
   IniRead, BrowseDest, %A_AppData%\Folder\config.ini, Destinationfolder, key
   MsgBox %BrowseSource%`t%BrowseDest%
   Gui, Add, Text,, Do you want to backup your default folders?
   Gui, Add, Button, , Yes
   Gui, Add, Button, , No
   Gui, Show
} else {
   If (FileExist(A_AppData "\Folder\config.ini")!="D"){
      FileCreateDir, %A_AppData%\Folder
      FileAppend, , %A_AppData%\Folder\config.ini
   }
   Gui, Destroy
   Gui, Add, Text,, Select the source folder:
   Gui, Add, Edit, r1 w300 vSourceEdit,
   Gui, Add, Button, gSource, Select
   Gui, Add, Text,, Select the destination folder:
   Gui, Add, Edit, r1 w300 vDestEdit,
   Gui, Add, Button, gDest, Select
   Gui, Add, Button, x+160, OK
   Gui, Show, w320 h200
}
Return

_________________
AHK_H (2alpha) AHF TT _Struct WatchDir Yaml _Input ObjTree RapidHotkey DynaRun Wink
Back to top
View user's profile Send private message
[VxE]



Joined: 07 Oct 2006
Posts: 3254
Location: Simi Valley, CA

PostPosted: Thu Sep 24, 2009 8:40 pm    Post subject: Reply with quote

Code:
If FileExist("%A_AppData%\Folder\config.ini"){

should be
Code:
If FileExist( A_AppData "\Folder\config.ini") {

_________________
Ternary (a ? b : c) guide     TSV Table Manipulation Library
Post code inside [code][/code] tags!
Back to top
View user's profile Send private message
bpmb



Joined: 13 Sep 2009
Posts: 30

PostPosted: Thu Sep 24, 2009 9:27 pm    Post subject: Reply with quote

Thanks! Always good to know how it should be used.. I solved it in the meantime by using the code below:

Code:
IfExist, %A_AppData%\Folder\config.ini
{
   Gui, Add, Text,, Do you want to backup your default folders?
   Gui, Add, Text,,
   Gui, Add, Button, Default, Yes
   Gui, Add, Button, x+70, No
   Gui, Show
}
ELSE
{
   Gui, Add, Text,, Select the source folder:
   Gui, Add, Edit, r1 w300 vSourceEdit,
   Gui, Add, Button, gSource, Select
   Gui, Add, Text,, Select the destination folder:
   Gui, Add, Edit, r1 w300 vDestEdit,
   Gui, Add, Button, gDest, Select
   Gui, Add, Button, x+160, Go
   Gui, Show, w320 h200
}
Return


The actual scripts are triggered by the buttons.. I tried something similair before but I must've typed it wrong..

Thanks everyone!
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    AutoHotkey Community Forum Index -> Ask for Help All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group