FileCopy: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
mNo edit summary
No edit summary
Line 1: Line 1:
{{Useful Function}}
{{Server client function}}
__NOTOC__
__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.)}}
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)}}
{{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)}}
{{Note|This function has been implimented:[[http://code.google.com/p/mtasa-blue/source/detail?r=4584 here]]}}


==Syntax==
==Syntax==
<syntaxhighlight lang="lua"> bool fileCopy ( string filePath , string copiedFilePath ) </syntaxhighlight>
<syntaxhighlight lang="lua"> bool fileCopy ( string filePath , string copyToFilePath ) </syntaxhighlight>
===Required Arguments===
===Required Arguments===
* '''filePath''': The path of the file you want to copy.
* '''filePath''': The path of the file you want to copy.
* '''copiedFilePath''': The copied filepath.
* '''copyToFilePath''': Where to copy the specified file to.
 
===Returns===
===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.
===Code===
<syntaxhighlight lang="lua">
function fileCopy(curName,newName)
if fileExists(curName) then
local file = fileOpen(curName)
if not file then return false end
local file1 = fileCreate(newName)
if file1 then
local fileSize = fileGetSize(file)
if fileSize==0 then
fileClose(file1)
fileClose(file)
return true
end
fileWrite(file1,fileRead(file,fileSize))
fileClose(file1)
fileClose(file)
end
return true
end
return false
end
</syntaxhighlight>


==Example==
==Example==
Line 41: Line 18:
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)
    if res==getThisResource() then --make sure it's this resource
    local filePath = ":"..getResourceName(res).."/test.txt"
          local filePath = ":"..getResourceName(res).."/test.txt"
    fileCreate(filePath) --create the file in this resource and name it 'test.txt'.
          fileCreate(filePath) --create the file in this resource and name it 'test.txt'.
    if fileCopy(filePath, ":"..getResourceName(res).."/test1.txt") then
          if fileCopy(filePath,":"..getResourceName(res).."/test1.txt") then
        outputChatBox("File was successfully copied!", root, 0, 100, 0)
              outputChatBox("File was successfully copied!",0,100,0)
    else
          else
        outputChatBox("File was not successfully copied, probably because it doesn't exist.", root, 100, 0, 0)
              outputChatBox("Don't know how this happened but the file was not successfully copied!",100,0,0)
    end
          end
    end
end)
end)
</syntaxhighlight>
</syntaxhighlight>
Line 57: Line 32:
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)
    if res==getThisResource() then --make sure it's this resource
    local filePath = ":"..getResourceName(res).."/test.txt"
          local filePath = ":"..getResourceName(res).."/test.txt"
    fileCreate(filePath) --create the file in this resource and name it 'test.txt'.
          fileCreate(filePath) --create the file in this resource and name it 'test.txt'.
    if fileCopy(filePath,":"..getResourceName(res).."/test1.txt") then
          if fileCopy(filePath,":"..getResourceName(res).."/test1.txt") then
        outputChatBox("File was successfully copied!", 0, 100, 0)
              outputChatBox("File was successfully copied!",0,100,0)
    else
          else
        outputChatBox("File was not successfully copied, probably because it doesn't exist.", 100, 0, 0)
              outputChatBox("Don't know how this happened but the file was not successfully copied!",100,0,0)
    end
          end
    end
end)
end)
</syntaxhighlight>
</syntaxhighlight>
</section>
</section>
Created by: Jaysds1
 
{{Useful_Functions}}
{{Requirements|1.3.1|1.3.1}}
{{File functions}}

Revision as of 21:33, 3 September 2012


This function copies a file.

[[{{{image}}}|link=|]] Note: The file functions should not be used to implement configuration files. It is encouraged to use the XML functions for this instead.)
[[{{{image}}}|link=|]] 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 ) 

Required Arguments

  • filePath: The path of the file you want to copy.
  • copyToFilePath: Where to copy the specified file to.

Returns

Return true if the file was copied, else false if the 'filePath' doesn't exist.

Example

Click to collapse [-]
Server

This 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 [-]
Client

This 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)
Minimum server version 1.3.1
Minimum client version 1.3.1

Note: Using this feature requires the resource to have the above minimum version declared in the meta.xml <min_mta_version> section. e.g. <min_mta_version server="1.3.1" client="1.3.1" />