How to use variables in FileExit() function? Topic is solved

Get help with using AutoHotkey and its commands and hotkeys
User avatar
Nwb
Posts: 444
Joined: 29 Nov 2016, 08:56

How to use variables in FileExit() function?

09 Apr 2018, 12:04

Notice the examples

Code: Select all

bon := "lol"

y::
If FileExist("C:\Discord\logs\"bon".txt")
	Msgbox Yes
else
	Msgbox No
return

Code: Select all

bon := "C:\Discord\logs\lol.txt"
If FileExist(bon)
They do not work.

While this works.

Code: Select all

If FileExist("C:\Discord\logs\lol.txt")
it does?

How do I use variables inside FileExit's function?
Last edited by Nwb on 09 Apr 2018, 12:37, edited 1 time in total.
I am your average ahk newbie. Just.. a tat more cute. ;)
User avatar
Cuadrix
Posts: 204
Joined: 07 May 2017, 08:26

Re: How to use variables in FileExit() function?

09 Apr 2018, 12:22

You have to have a space in between the double quotation marks and the variable bon so that Autohotkey recognizes which of the text inside the ()-parentheses is meant to be a string;

Code: Select all

bon := "lol"

y::
If FileExist("C:\Discord\logs\" bon ".txt") ; Space between bon and ""
	MsgBox, Yes
else
	MsgBox, No
return
The second one works perfectly fine with me?

Code: Select all

bon := "C:\Discord\logs\lol.txt"
If FileExist(bon)
	MsgBox, Yes
Else
	MsgBox, No
----
User avatar
TheDewd
Posts: 1361
Joined: 19 Dec 2013, 11:16
Location: USA

Re: How to use variables in FileExit() function?

09 Apr 2018, 12:26

I was going to mention adding the spaces, but Cuadrix already did that.

I'll instead leave you with this: You can use ternary to make your code shorter

Code: Select all

bon := "lol"

y::
	Msgbox, % (FileExist("C:\Discord\logs\" bon ".txt") ? "Yes" : "No")
return
Image Bulldozer - Sokoban inspired game from 1994 recreated in AutoHotkey.
User avatar
Nwb
Posts: 444
Joined: 29 Nov 2016, 08:56

Re: How to use variables in FileExit() function?

09 Apr 2018, 12:29

Cuadrix wrote:You have to have a space in between the double quotation marks and the variable bon so that Autohotkey recognizes which of the text inside the ()-parentheses is meant to be a string;

Code: Select all

bon := "lol"

y::
If FileExist("C:\Discord\logs\" bon ".txt") ; Space between bon and ""
	MsgBox, Yes
else
	MsgBox, No
return
The second one works perfectly fine with me?

Code: Select all

bon := "C:\Discord\logs\lol.txt"
If FileExist(bon)
	MsgBox, Yes
Else
	MsgBox, No
No sir, I tried even your script but it doesn't work unless the full path is specified. The second one didn't work for me.

I get the Msgbox No even though the file exists. But when I replace it with the full path it says it Yes.


Something is wrong.
I am your average ahk newbie. Just.. a tat more cute. ;)
Odlanir
Posts: 554
Joined: 20 Oct 2016, 08:20

Re: How to use variables in FileExit() function?  Topic is solved

09 Apr 2018, 12:31

Out of curiosity, how can you say this works ?

Code: Select all

If FileExist(C:\Discord\logs\lol.txt)
it should give you this error message:

Code: Select all

 ==> The leftmost character above is illegal in an expression.
     Specifically: \Discord\logs\lol.txt
Anyway those three works:

Code: Select all

b1   := "lol"
bon1 := FileExist("C:\Discord\logs\" b1 ".txt")
b2   := "C:\Discord\logs\lol.txt" 
bon2 := FileExist(b2)
bon3 := FileExist("C:\Discord\logs\lol.txt")
Msgbox % bon1 "`n" bon2 "`n" bon3
____________________________________________________________________________
Windows 10 Pro 64 bit - Autohotkey v1.1.30.01 64-bit Unicode
User avatar
Nwb
Posts: 444
Joined: 29 Nov 2016, 08:56

Re: How to use variables in FileExit() function?

09 Apr 2018, 12:34

Edit:

The fault was that I unknowingly put the variable after a return. Thank you everybody who replied.
Last edited by Nwb on 09 Apr 2018, 12:43, edited 1 time in total.
I am your average ahk newbie. Just.. a tat more cute. ;)
User avatar
Nwb
Posts: 444
Joined: 29 Nov 2016, 08:56

Re: How to use variables in FileExit() function?

09 Apr 2018, 12:41

Odlanir wrote:Out of curiosity, how can you say this works ?

Code: Select all

If FileExist(C:\Discord\logs\lol.txt)
it should give you this error message:

Code: Select all

 ==> The leftmost character above is illegal in an expression.
     Specifically: \Discord\logs\lol.txt
Anyway those three works:

Code: Select all

b1   := "lol"
bon1 := FileExist("C:\Discord\logs\" b1 ".txt")
b2   := "C:\Discord\logs\lol.txt" 
bon2 := FileExist(b2)
bon3 := FileExist("C:\Discord\logs\lol.txt")
Msgbox % bon1 "`n" bon2 "`n" bon3
I did a typo. My bad.

Turns out the variable's value was never gotten to because there was a return before it.



Thanks to everybody that replied!
I am your average ahk newbie. Just.. a tat more cute. ;)
BoBo
Posts: 2519
Joined: 13 May 2014, 17:15

Re: How to use variables in FileExit() function?

09 Apr 2018, 14:21

The initial false function is the exact reason why to think about this: https://autohotkey.com/boards/viewtopic.php?f=5&t=46983

Return to “Ask For Help”

Who is online

Users browsing this forum: agrout, Bing [Bot], D4RQS1D3R, Google [Bot], TAC109 and 223 guests