FileExists: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
m (Portuguese version indexed) |
||
(3 intermediate revisions by 3 users not shown) | |||
Line 1: | Line 1: | ||
__NOTOC__ | __NOTOC__ | ||
{{Server function}} | {{Server client function}} | ||
This functions checks whether a specified file exists inside a resource. | This functions checks whether a specified file exists inside a resource. | ||
==Syntax== | ==Syntax== | ||
<syntaxhighlight lang="lua"> | <syntaxhighlight lang="lua"> | ||
bool fileExists ( string filePath ) | bool fileExists ( string filePath ) | ||
</syntaxhighlight> | </syntaxhighlight> | ||
{{OOP|This function is a static function underneath the File class.|[[File]].exists}} | |||
===Required Arguments=== | ===Required Arguments=== | ||
*'''filePath:''' The [[filepath]] of the file, whose existence is going to be checked, in the following format: '''":resourceName/path"'''. 'resourceName' is the name of the resource the file is checked to be in, and 'path' is the path from the root directory of the resource to the file. | *'''filePath:''' The [[filepath]] of the file, whose existence is going to be checked, in the following format: '''":resourceName/path"'''. 'resourceName' is the name of the resource the file is checked to be in, and 'path' is the path from the root directory of the resource to the file. | ||
:For example, if you want to check whether a file named 'myfile.txt' exists in the resource 'mapcreator', it can be done from another resource this way: ''fileExists(":mapcreator/myfile.txt")''. | :For example, if you want to check whether a file named 'myfile.txt' exists in the resource 'mapcreator', it can be done from another resource this way: ''fileExists(":mapcreator/myfile.txt")''. | ||
:If the file, whose existence is going to be checked, is in the current resource, only the file path is necessary, e.g. ''fileExists("myfile.txt")''. | :If the file, whose existence is going to be checked, is in the current resource, only the file path is necessary, e.g. ''fileExists("myfile.txt")''. Note that you must use forward slashes '/' for the folders, backslashes '\' will return false. | ||
===Returns=== | ===Returns=== | ||
Line 62: | Line 45: | ||
==See Also== | ==See Also== | ||
{{File functions}} | {{File functions}} | ||
[[pt-br:fileExists]] |
Latest revision as of 17:59, 20 December 2023
This functions checks whether a specified file exists inside a resource.
Syntax
bool fileExists ( string filePath )
OOP Syntax Help! I don't understand this!
- Note: This function is a static function underneath the File class.
- Method: File.exists(...)
Required Arguments
- filePath: The filepath of the file, whose existence is going to be checked, in the following format: ":resourceName/path". 'resourceName' is the name of the resource the file is checked to be in, and 'path' is the path from the root directory of the resource to the file.
- For example, if you want to check whether a file named 'myfile.txt' exists in the resource 'mapcreator', it can be done from another resource this way: fileExists(":mapcreator/myfile.txt").
- If the file, whose existence is going to be checked, is in the current resource, only the file path is necessary, e.g. fileExists("myfile.txt"). Note that you must use forward slashes '/' for the folders, backslashes '\' will return false.
Returns
Returns true if the file exists, false otherwise.
Example
This example checks if a file exists in a resource directory
function checkExistingFile(player,cmd,filename,resourcename) if not filename then -- if the player didn't include a filename outputChatBox("ERROR: Syntax '/checkfile filename resourcename(optional)'.",player) -- display error return false -- stop function end if not resourcename then -- if the player didn't specify the resource he wants to check, use current resource resourcename = getResourceName(resource) --every resource has a predefined global variable called resource that contains the resource pointer for that resource, in other words, the value that getThisResource() function returns. else if not getResourceFromName(resourcename) then -- if a resource with that name doesn't exist, output error and stop function outputChatBox("ERROR: Resource "..resourcename.." doesn't exist.",player) -- output error message return false -- stop the function here end end -- as it hasn't stopped anywhere, we have both correct resourcename and filename local exists = fileExists((":%s/%s"):format(resourcename,filename)) -- using shorter format of string.format, see StringLibraryTutorial in lua wiki for that if exists then outputChatBox(("The file %q in resource %q exists"):format(filename,resourcename)) else outputChatBox(("The file %q in resource %q doesn't exist"):format(filename,resourcename)) end end addCommandHandler("exists",checkExistingFile)