 |
AutoHotkey Community Let's help each other out
|
| View previous topic :: View next topic |
| Author |
Message |
bpmb
Joined: 13 Sep 2009 Posts: 30
|
Posted: Wed Sep 23, 2009 9:02 pm Post subject: if FileExist? |
|
|
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 |
|
 |
Z_Gecko Guest
|
Posted: Wed Sep 23, 2009 9:08 pm Post subject: |
|
|
| 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
|
Posted: Wed Sep 23, 2009 9:20 pm Post subject: |
|
|
| 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 |
|
 |
Z_Gecko Guest
|
Posted: Wed Sep 23, 2009 9:23 pm Post subject: |
|
|
| how? (means show the code) |
|
| Back to top |
|
 |
HotKeyIt
Joined: 18 Jun 2008 Posts: 4652 Location: AHK Forum
|
Posted: Wed Sep 23, 2009 9:24 pm Post subject: |
|
|
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  |
|
| Back to top |
|
 |
bpmb
Joined: 13 Sep 2009 Posts: 30
|
Posted: Wed Sep 23, 2009 9:34 pm Post subject: |
|
|
| 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 |
|
 |
randallf
Joined: 06 Jul 2009 Posts: 678
|
Posted: Wed Sep 23, 2009 9:40 pm Post subject: Re: if FileExist? |
|
|
| 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 |
|
 |
Z_Gecko Guest
|
Posted: Wed Sep 23, 2009 9:58 pm Post subject: |
|
|
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
|
Posted: Thu Sep 24, 2009 4:49 am Post subject: |
|
|
| 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  |
|
| Back to top |
|
 |
[VxE]
Joined: 07 Oct 2006 Posts: 3254 Location: Simi Valley, CA
|
|
| Back to top |
|
 |
bpmb
Joined: 13 Sep 2009 Posts: 30
|
Posted: Thu Sep 24, 2009 9:27 pm Post subject: |
|
|
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 |
|
 |
|
|
You can post new topics in this forum You can reply to topics in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|