The CopyFile function copies an existing file to a new file, with the option of returning an error if the new filename already exists; the FileCopy function copies an existing file to a new file without checking the existence of the destination file.
CopyFile "source" [Over [To]] "dest" [, subname[,ident%]]
FileCopy "source" [To] "dest" [, subname [, ident%]]
"dest", "source" | : file names paths. |
subname | : the name of linked procedure. |
ident% | : 32-bit Integer |
CopyFile and FileCopy copy an existing file "source" to the destination file "dest" in 32KB blocks. By default, CopyFile checks first to see if the destination file exists, but this check can be over-ridden by the inclusion of the Over or Over To clauses; FileCopy carries out no such check and will overwrite the destination file if it exists.
To use long filenames (in excess of MAX_PATH length of 260), add "\\?\" before the source and destination filenames.
When specified, subname is the name of a Sub procedure that is invoked after each copied block (32KB). The Sub takes two parameters:
Sub copyfile(bytes_copied, ident)
bytes_copied | : 32-bit Integer. |
ident | : 32-bit Integer |
The bytes_copied argument specifies the number of bytes copied at that moment; for file sizes in excess of _MaxInt, see the third example below. The ident variable identifies the CopyFile/FileCopy command (through the value of ident%), allowing the procedure to be used for more than one type of copy operation and, thus, allowing some element of customsation; it is also useful if it is planned to carry out more than one copy operation at any one time. Finally, note that if the program is ended by End or Stop in the midst of copying a file, the copyfile Sub is not always halted at the same time and may continue working afterwards.
Open "c:\test.dat" for Output As # 1 : Close # 1
Try
CopyFile "c:\test.dat" To "c:\backup.dat" // Will cause error if c:\backup.dat exists
Catch
If Exist("c:\backup.dat") Then Kill "c:\backup.dat" // Override safety feature (if needed)
CopyFile "c:\test.dat" To "c:\backup.dat"
EndCatch
CopyFile "c:\test.dat" Over To "c:\backup.dat" // 'Over' prevents an error if c:\backup.dat exists
FileCopy "c:\test.dat" To "c:\backup.dat" // 'Over' prevents an error if c:\backup.dat exists
Kill "c:\test.dat" : Kill "c:\backup.dat" // Tidy up line
or
Dim a(200000) As Int32
BSave "c:\test.dat", V:a(0), 800004
Ocx Label lbl = "Save Progress", 10, 10, 180, 14 : lbl.BackColor = RGB(255, 255, 255)
Ocx ProgressBar prg = "", 10, 25, 200, 30
// If c:\backup.dat exists, CopyFile will raise an error message
If Exist("c:\backup.dat")
FileCopy "c:\test.dat" To "c:\backup.dat", check_it, 1
Else
CopyFile "c:\test.dat" To "c:\backup.dat", check_it, 2
EndIf
prg.Value = 100
Ocx Command cmd = "Close", 60, 65, 100, 22
Do : Sleep : Until Me Is Nothing
Kill "c:\test.dat" : Kill "c:\backup.dat" // Tidy up line
Sub check_it(bytes_copied%, ident%)
If ident% = 1
lbl.Caption = "Save Progress using FileCopy:"
Else If ident% = 2
lbl.Caption = "Save Progress using CopyFile:"
EndIf
prg.Value = 100 * (bytes_copied% / 800004)
Pause 1 // Included purely to lengthen the time the program runs to allow you to see the effects of this Sub
EndSub
Sub cmd_Click
Me.Close
EndSub
When the file size is greater than _MaxInt, the following workaround can be used:
Dim a(200000) As Int32
BSave "c:\test.dat", V:a(0), 800004
// If c:\backup.dat exists, CopyFile will raise an error message
If Exist("c:\backup.dat") Then Kill "c:\backup.dat"
check_it(0, -1) // Set bytes_count to zero
CopyFile "c:\test.dat" To "c:\backup.dat", check_it, 1
check_it(0, -1) // Set bytes_count to zero
FileCopy "c:\test.dat" To "c:\backup.dat", check_it, 2
Ocx Command cmd = "Close", 60, 65, 100, 22
Do : Sleep : Until Me Is Nothing
Kill "c:\test.dat" : Kill "c:\backup.dat" // Tidy up line
Sub check_it(bytes_copied%, ident%)
Static bytes_count As Large
If ident% = -1
bytes_count = 0
Else
bytes_count = bytes_count + (32 * 1024)
Print AT(1, ident%); "Bytes copied: "; bytes_count; " "
Pause 1 // Included purely to lengthen the time the program runs to allow you to see the effects of this Sub
EndIf
EndSub
Sub cmd_Click
Me.Close
EndSub
CopyFile and FileCopy can take ':Files' resource file as an argument.
CopyFile is not a GFA-BASIC 32 implementation of the API function CopyFileEx(), because it is available only from NT onwards.
{Created by Sjouke Hamstra; Last updated: 15/01/2016 by James Gaite}