Page 1 of 1
Copy a file from one directory to another leaving
Posted: Fri Aug 24, 2012 7:10 am
by Darren
Can anybody tell me how I can copy one file, in a certain directory to another directory, leaving the original file alone.
For example,
Say I had a file called "C:TestFred.exe", how can I copy this file to a directory called "C:New Folder", leaving the original file where it is.
(This is similar to the "Rename" function)
I need a command something like "FileCopy" as with visual basic 6
Perhaps this command "FileCopy" could be added to the next release?
Many thanks in advance!!!
Copy a file from one directory to another leaving
Posted: Fri Aug 24, 2012 8:55 pm
by Marco
hi, here`s option one:
Declare CopyFileA as "CopyFileA" of "kernel32"
sourceFileName As String
targetFileName As String
bFailIfExists As integer
result as integer
end declare
function copyfile(source as string,target as string,overwrite as boolean) as integer
if overwrite=false then copyfilea.bFailIfExists=true
if overwrite=true then copyfilea.bFailIfExists=false
copyfilea.sourcefilename=source
copyfilea.targetfilename=target
copyfilea.execute
result=copyfilea.result
end function
`this wil copy a file, the result of the function is true if it succeeds.
`if overwrite is false then the function does not succeed if the file exists.
dim copyresult as boolean
copyresult=copyfile("casetest.bas","casetestcopy.bas",false)
if copyresult=true then showmessage "copy done"
if copyresult=false then showmessage "copy failed"
here`s option two:
dim target as filestream
dim source as filestream
source.open("sourcename.ext")
target.open("targetname.ext")
target.copyfromstream(source)
target.close
source.close
this one i`ve not tested but will work i guess.
hope this will help you, best regards
Copy a file from one directory to another leaving
Posted: Tue Oct 02, 2012 12:46 pm
by SAURAV ROY
Hello Admin, I`ve tested the second option code. It shows the error message:
"error 8 line 5 `COPYFROMSTREAM` not declared/unknown"
:o
Copy a file from one directory to another leaving
Posted: Wed Oct 03, 2012 3:30 pm
by Marco
Sorry mistake of me you must use copyfrom like this:
dim target as filestream
dim source as filestream
source.open("readme.txt")
target.open("copyfromreadme.txt")
target.copyfrom(source)
target.close
source.close
showmessage "done"
this must work smoothly. Best regards
Copy a file from one directory to another leaving
Posted: Wed Oct 03, 2012 4:52 pm
by SAURAV ROY
Yes, this is working smoothly now! :)
thank you Admin
Copy a file from one directory to another leaving
Posted: Wed Oct 03, 2012 6:00 pm
by SAURAV ROY
My CopyFile Code :)
----------------------------------------------------------
const NULL=chr$(0)
call CopyFile("D:Temp
eadme.txt","D: empabcd.txt")
call CopyFile("D:Temp
eadme.txt","D: emp1234.txt")
call CopyFile("D:Temp
eadme.txt","D: empwriteme.txt",false)
call CopyFile("D:Temp
eadme.txt","D: empduplicate.txt",true)
FUNCTION CopyFile(CopyFrom as string,CopyTo as string,OverWrite as boolean) as boolean
? if OverWrite=NULL then OverWrite=false
?
? dim target as filestream
? dim source as filestream
?
? if CopyFrom<>"" and CopyTo<>"" then
? ? if fileexists(CopyFrom)=1 then
? ?? ?if fileexists(CopyTo)=0 then
? ?? ?? source.open(CopyFrom)
? ?? ?? target.open(CopyTo)
? ?? ?? target.copyfrom(source)
? ?? ?? target.close
? ?? ?? source.close
? ?? ?? result=true
? ?? ?else
? ?? ?? if OverWrite=true then
? ?? ?? ? if fileexists(CopyTo)=1 then kill(CopyTo)
? ?? ?? ? source.open(CopyFrom)
? ?? ?? ? target.open(CopyTo)
? ?? ?? ? target.copyfrom(source)
? ?? ?? ? target.close
? ?? ?? ? source.close
? ?? ?? ? result=true
? ?? ?? else
? ?? ?? ? showmessage "Source file already exists!"
? ?? ?? ? result=false
? ?? ?? end if
? ?? ?end if
? ? else
? ?? ?showmessage "Source file does not exist!"
? ?? ?result=false
? ? end if
? else
? ? showmessage "Parameter missing!"
? ? result=false
? end if
END FUNCTION