FileCopy: Difference between revisions
Jump to navigation
Jump to search
(Created page with "{{Useful Function}} This function copies a file. ==Syntax== <syntaxhighlight lang="lua"> bool fileCopy ( string filePath , string copiedFilePath ) </syntaxhighlight> ===Required Arguments=== * '''filePath'...") |
Fernando187 (talk | contribs) (Remove obsolete Requirements section) |
||
(15 intermediate revisions by 7 users not shown) | |||
Line 1: | Line 1: | ||
{{ | {{Server client function}} | ||
This function copies a file. | __NOTOC__ | ||
This function copies a file.{{Note|The file functions should not be used to implement configuration files. It is encouraged to use the XML functions for this instead.}} | |||
{{Tip|If you do not want to share the content of the created file with other servers, prepend the file path with @ (See [[filepath]] for more information)}} | |||
==Syntax== | ==Syntax== | ||
<syntaxhighlight lang="lua"> bool fileCopy ( string filePath , string | <syntaxhighlight lang="lua"> | ||
bool fileCopy ( string filePath, string copyToFilePath [, bool overwrite = false ] ) | |||
</syntaxhighlight> | |||
{{OOP|This function is a static function underneath the File class.|[[File]].copy}} | |||
===Required Arguments=== | ===Required Arguments=== | ||
* '''filePath''': The path of the file you want to copy. | * '''filePath''': The path of the file you want to copy. | ||
* ''' | * '''copyToFilePath''': Where to copy the specified file to. | ||
===Returns | |||
===Optional Arguments=== | |||
* '''overwrite''': If set to true it will overwrite a file that already exists at copyToFilePath. | |||
==Returns== | |||
Return true if the file was copied, else false if the 'filePath' doesn't exist. | Return true if the file was copied, else false if the 'filePath' doesn't exist. | ||
==Example== | ==Example== | ||
Line 37: | Line 24: | ||
This example copies a file called 'test.txt' and called it 'test1.txt'. | This example copies a file called 'test.txt' and called it 'test1.txt'. | ||
<syntaxhighlight lang="lua"> | <syntaxhighlight lang="lua"> | ||
addEventHandler("onResourceStart",resourceRoot,function(res) | addEventHandler("onResourceStart", resourceRoot, function(res) | ||
local filePath = ":"..getResourceName(res).."/test.txt" | |||
fileCreate(filePath) --create the file in this resource and name it 'test.txt'. | |||
if fileCopy(filePath, ":"..getResourceName(res).."/test1.txt") then | |||
outputChatBox("File was successfully copied!", root, 0, 100, 0) | |||
else | |||
outputChatBox("File was not successfully copied, probably because it doesn't exist.", root, 100, 0, 0) | |||
end | |||
end) | end) | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Line 53: | Line 38: | ||
This example copies a file called 'test.txt' and called it 'test1.txt'. | This example copies a file called 'test.txt' and called it 'test1.txt'. | ||
<syntaxhighlight lang="lua"> | <syntaxhighlight lang="lua"> | ||
addEventHandler("onClientResourceStart",resourceRoot,function(res) | addEventHandler("onClientResourceStart", resourceRoot, function(res) | ||
local filePath = ":"..getResourceName(res).."/test.txt" | |||
fileCreate(filePath) --create the file in this resource and name it 'test.txt'. | |||
if fileCopy(filePath,":"..getResourceName(res).."/test1.txt") then | |||
outputChatBox("File was successfully copied!", 0, 100, 0) | |||
else | |||
outputChatBox("File was not successfully copied, probably because it doesn't exist.", 100, 0, 0) | |||
end | |||
end) | end) | ||
</syntaxhighlight> | </syntaxhighlight> | ||
</section> | </section> | ||
==See Also== | |||
{{File functions}} | |||
[[pt-br:fileCopy]] |
Latest revision as of 17:07, 7 November 2024
This function copies a file.
Tip: If you do not want to share the content of the created file with other servers, prepend the file path with @ (See filepath for more information) |
Syntax
bool fileCopy ( string filePath, string copyToFilePath [, bool overwrite = false ] )
OOP Syntax Help! I don't understand this!
- Note: This function is a static function underneath the File class.
- Method: File.copy(...)
Required Arguments
- filePath: The path of the file you want to copy.
- copyToFilePath: Where to copy the specified file to.
Optional Arguments
- overwrite: If set to true it will overwrite a file that already exists at copyToFilePath.
Returns
Return true if the file was copied, else false if the 'filePath' doesn't exist.
Example
Click to collapse [-]
ServerThis example copies a file called 'test.txt' and called it 'test1.txt'.
addEventHandler("onResourceStart", resourceRoot, function(res) local filePath = ":"..getResourceName(res).."/test.txt" fileCreate(filePath) --create the file in this resource and name it 'test.txt'. if fileCopy(filePath, ":"..getResourceName(res).."/test1.txt") then outputChatBox("File was successfully copied!", root, 0, 100, 0) else outputChatBox("File was not successfully copied, probably because it doesn't exist.", root, 100, 0, 0) end end)
Click to collapse [-]
ClientThis example copies a file called 'test.txt' and called it 'test1.txt'.
addEventHandler("onClientResourceStart", resourceRoot, function(res) local filePath = ":"..getResourceName(res).."/test.txt" fileCreate(filePath) --create the file in this resource and name it 'test.txt'. if fileCopy(filePath,":"..getResourceName(res).."/test1.txt") then outputChatBox("File was successfully copied!", 0, 100, 0) else outputChatBox("File was not successfully copied, probably because it doesn't exist.", 100, 0, 0) end end)