Page 1 of 1

How to use variables in FileExit() function?

Posted: 09 Apr 2018, 12:04
by Nwb
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?

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

Posted: 09 Apr 2018, 12:22
by Cuadrix
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

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

Posted: 09 Apr 2018, 12:26
by TheDewd
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

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

Posted: 09 Apr 2018, 12:29
by Nwb
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.

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

Posted: 09 Apr 2018, 12:31
by Odlanir
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

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

Posted: 09 Apr 2018, 12:34
by Nwb
Edit:

The fault was that I unknowingly put the variable after a return. Thank you everybody who replied.

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

Posted: 09 Apr 2018, 12:41
by Nwb
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!

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

Posted: 09 Apr 2018, 14:21
by BoBo
The initial false function is the exact reason why to think about this: https://autohotkey.com/boards/viewtopic.php?f=5&t=46983