FileExists: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
(Removed from category "Needs example")
Line 4: Line 4:


==Syntax==
==Syntax==
<section name="Server" class="server" show="true">
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
bool fileExists ( string filePath )
bool fileExists ( string filePath )
Line 12: Line 13:
: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")''.
</section>
{{New feature|3.0110|1.1|
<section name="Client" class="client" show="true">
<syntaxhighlight lang="lua">
bool fileExists ( string filePath [, string accessType = "public" ] )
</syntaxhighlight>
===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")''.
===Optional Arguments===
*'''accessType :''' This setting determines whether to check the public or private version of the file at '''filePath'''
** "public" will check the file that is shared by all servers.
** "private" will check the file that only the current server is allowed to access. Note: It is only possible to check a private file if it was previously saved as ''"private"'' by the current server.
</section>
}}


===Returns===
===Returns===

Revision as of 13:44, 1 June 2011

This functions checks whether a specified file exists inside a resource.

Syntax

Click to collapse [-]
Server
bool fileExists ( string filePath )

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").


Click to collapse [-]
Client
bool fileExists ( string filePath [, string accessType = "public" ] )

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").

Optional Arguments

  • accessType : This setting determines whether to check the public or private version of the file at filePath
    • "public" will check the file that is shared by all servers.
    • "private" will check the file that only the current server is allowed to access. Note: It is only possible to check a private file if it was previously saved as "private" by the current server.

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)

See Also