Difference between revisions of "File.copy"

From MiniScript Wiki
Jump to navigation Jump to search
m (Added some known limitations on error returning.)
 
Line 34: Line 34:
 
<ms>if file.exists("myfile.txt") then
 
<ms>if file.exists("myfile.txt") then
  
   ret = file.copy("myfile.txt", "../mycopy.txt")
+
   err = file.copy("myfile.txt", "../mycopy.txt")
  
   if ret == null and file.exists("../mycopy.txt") then
+
   if err == null and file.exists("../mycopy.txt") then
 
     print "Copy successful!"
 
     print "Copy successful!"
 
   else
 
   else
     print "Copy failed: " + ret
+
     print "Copy failed: " + err
 
   end if
 
   end if
  

Latest revision as of 18:17, 12 January 2022

In Mini Micro and command-line MiniScript, the file.copy function copies a file on the system.

Arguments

Parameter Name Meaning
sourceFilePath absolute or relative path to the source file
targetFilePath absolute or relative path of the target file


Return Meaning
null File was copied successfully, or the source file was not found. (Use file.exists to check if the source file is present first.)
"Error: target file already exists" Target file already exists. file.copy won't override existing files.
"Error: target disk not found" targetFilePath cannot be navigated to, so no copy can be made.

Current limitations

  1. If you attempt to copy a file into a path, the file is not copied and no error is returned. EG: file.copy "myfile.txt", "/usr2"
  2. If you attempt to copy a directory, or to a directory, with no file names, it returns null with no error. EG: file.copy "/usr2", "/usr2/copy"

Example

This copies a file from the current working directory, to a directory just above it:

if file.exists("myfile.txt") then

  err = file.copy("myfile.txt", "../mycopy.txt")

  if err == null and file.exists("../mycopy.txt") then
    print "Copy successful!"
  else
    print "Copy failed: " + err
  end if

end if