Filenames - quotation marks or no quotation marks?

Get help with using AutoHotkey and its commands and hotkeys
yogalD
Posts: 3
Joined: 25 Apr 2016, 06:36

Filenames - quotation marks or no quotation marks?

25 Apr 2016, 06:40

A very annoying syntax issue I'm having with AHK right now:
I can't find any way to determine if a file is empty, so I wrote a function to do so.

Code: Select all

CheckEmpty(file) {
	FileReadLine,temp, file, 1
	if (temp="") {
		return 1
	} else {
		return 0
	}
}
msgbox  % "File empty status: " . CheckEmpty("D:\common\preferences\prefs.cs")
The only problem is, calling the CheckEmpty function requires me to have quotation marks. However, the function does not let me use quotation marks in the file name (otherwise FileReadLine returns nothing). If I don't have quotation marks in the CheckEmpty function call it says, "The leftmost character above is illegal in an expression."
What on earth am I supposed to do about this? It's pretty ridiculous!
Last edited by yogalD on 25 Apr 2016, 09:07, edited 1 time in total.
HotKeyIt
Posts: 2130
Joined: 29 Sep 2013, 18:35
Contact:

Re: Filenames - quotation marks or no quotation marks?

25 Apr 2016, 07:38

Either FileReadLine,temp, %file%, 1 or FileReadLine,temp,% file, 1.
Btw. I would rather use FileGetSize, size, %file%.

Code: Select all

CheckEmpty(file) {
	FileGetSize, size, %file%
	return !!size
}
msgbox  % "File empty status: " . CheckEmpty("D:\common\preferences\prefs.cs")
yogalD
Posts: 3
Joined: 25 Apr 2016, 06:36

Re: Filenames - quotation marks or no quotation marks?

25 Apr 2016, 09:07

Thanks, it still doesn't make any logical sense to me, but hey it works.

Also I did realise of course that the first line being empty doesn't mean the whole file is empty but it was what I needed at the time.
User avatar
boiler
Posts: 5295
Joined: 21 Dec 2014, 02:44

Re: Filenames - quotation marks or no quotation marks?

25 Apr 2016, 09:58

When you're passing a literal string (rather than a variable that contains the string) as an argument in a function call, you use expression syntax, which requires it to be in quotes. If you were to use the literal string in a command like FileReadLine, then it requires you to use "traditional" syntax rather than expression syntax, unless you force it to use expression syntax with a % followed by a space. I know it makes things confusing while learning the language.

Example using a literal string with traditional syntax:
FileReadLine,temp, D:\common\preferences\prefs.cs, 1

Example using a literal string with forced expression syntax:
FileReadLine,temp, % "D:\common\preferences\prefs.cs", 1

Similarly, you can use traditional or expression syntax if you use a variable instead of a literal string as HotKeyIt showed. So the above examples using a variable become:

Traditional:
FileReadLine,temp, %file%, 1

Forced expression:
FileReadLine,temp, % file, 1 (subtle but important difference)

In fact, the assignment of the variable itself can be done either way, affecting both the assignment operator and the way the value is shown.

Traditional:
file = D:\common\preferences\prefs.cs

Expression:
file := " D:\common\preferences\prefs.cs"
yogalD
Posts: 3
Joined: 25 Apr 2016, 06:36

Re: Filenames - quotation marks or no quotation marks?

26 Apr 2016, 08:10

Perhaps everything should be forced to expression syntax, it's too inconsistent having two different types. Oh well.

Return to “Ask For Help”

Who is online

Users browsing this forum: Google [Bot], PebblyBeachBum, zcw1030 and 50 guests