AutoHotkey Community

It is currently May 26th, 2012, 9:51 pm

All times are UTC [ DST ]




Post new topic Reply to topic  [ 11 posts ] 
Author Message
 Post subject: if FileExist?
PostPosted: September 23rd, 2009, 10:02 pm 
Offline

Joined: September 13th, 2009, 2:34 pm
Posts: 30
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!


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 23rd, 2009, 10:08 pm 
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.


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: September 23rd, 2009, 10:20 pm 
Offline

Joined: September 13th, 2009, 2:34 pm
Posts: 30
hmmm OK, so it returns an Atrib value.. I now tried using IfExist instead but still can't get it to work..?


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 23rd, 2009, 10:23 pm 
how? (means show the code)


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: September 23rd, 2009, 10:24 pm 
Offline

Joined: June 18th, 2008, 8:36 am
Posts: 4923
Location: AHK Forum
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:


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 23rd, 2009, 10:34 pm 
Offline

Joined: September 13th, 2009, 2:34 pm
Posts: 30
Thanks, I tried you code but it still shows me the second GUI created after ELSE ? I can't see what's going wrong?


Report this post
Top
 Profile  
Reply with quote  
 Post subject: Re: if FileExist?
PostPosted: September 23rd, 2009, 10:40 pm 
Offline

Joined: July 6th, 2009, 9:58 pm
Posts: 678
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


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 23rd, 2009, 10:58 pm 
You are still not using the right syntax for FileExist()

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


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: September 24th, 2009, 5:49 am 
Offline

Joined: June 18th, 2008, 8:36 am
Posts: 4923
Location: AHK Forum
:oops:
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:


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 24th, 2009, 9:40 pm 
Offline
User avatar

Joined: October 7th, 2006, 8:45 am
Posts: 3330
Location: Simi Valley, CA
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!


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 24th, 2009, 10:27 pm 
Offline

Joined: September 13th, 2009, 2:34 pm
Posts: 30
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!


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 11 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: BrandonHotkey, dra, Exabot [Bot], JSLover, patgenn123, rbrtryn and 48 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group