Copy a file from one directory to another leaving

In this category you can exchange your programming questions and solutions.
Post Reply
Darren
Posts: 63
Joined: Wed May 11, 2011 11:55 am

Copy a file from one directory to another leaving

Post: # 949Post Darren
Fri Aug 24, 2012 7:10 am

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!!!

Marco
Site Admin
Posts: 246
Joined: Sat Sep 15, 2018 8:41 pm

Copy a file from one directory to another leaving

Post: # 950Post Marco
Fri Aug 24, 2012 8:55 pm

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

SAURAV ROY
Posts: 5
Joined: Tue Oct 02, 2012 11:35 am

Copy a file from one directory to another leaving

Post: # 951Post SAURAV ROY
Tue Oct 02, 2012 12:46 pm

Hello Admin, I`ve tested the second option code. It shows the error message:

"error 8 line 5 `COPYFROMSTREAM` not declared/unknown"

:o

Marco
Site Admin
Posts: 246
Joined: Sat Sep 15, 2018 8:41 pm

Copy a file from one directory to another leaving

Post: # 952Post Marco
Wed Oct 03, 2012 3:30 pm

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

SAURAV ROY
Posts: 5
Joined: Tue Oct 02, 2012 11:35 am

Copy a file from one directory to another leaving

Post: # 953Post SAURAV ROY
Wed Oct 03, 2012 4:52 pm

Yes, this is working smoothly now!  :)
thank you Admin

SAURAV ROY
Posts: 5
Joined: Tue Oct 02, 2012 11:35 am

Copy a file from one directory to another leaving

Post: # 954Post SAURAV ROY
Wed Oct 03, 2012 6:00 pm

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

Post Reply