CopyFile

BlitzMax Forums/BlitzMax Programming/CopyFile

Garfield(Posted 2007) [#1]
I have read all the posts about CopyFile()
But it still wont work!
My BMax is a new 1.24 installation on an Intel Mac and I canīt find a solution for this simple function to work!

Whats the magic behind that?
Is CopyStream really the same?
Is it save to copy the whole stream?
Is it as fast as a simple copyfunction?

Someone wrote a solution with RenameFile(src$,dst$) to copy files -- but the same behavior........ I guess that canīt work..

Edit:

I see the code in the filesystem.bmx for the function is really copystream

Function CopyFile( src$,dst$ )
Local in:TStream=ReadStream( src ),ok
If in
Local out:TStream=WriteStream( dst )
If out
Try
CopyStream in,out
ok=True
Catch ex:TStreamWriteException
End Try
out.Close
EndIf
in.Close
EndIf
Return ok
End Function


but why it wont work??


grable(Posted 2007) [#2]
It probably isnt as fast as a native one, but its very portable. So it should work.

are you sure you have proper permissions to access source/destination ?

You could try your native version: save this as "copyfile.m"
int CopyFileEx( char* src, char* dst) {
	NSString* source = [NSString cString:src];
	NSString* destination = [NSString cString:dst];
	NSFileManager* fm = [NSFileManager defaultManager];
	if([fm fileExistsAtPath:source]) {
		if( [fm copyPath:source toPath:destination handler:nil] == YES) return 1;
	}
	return 0;
}

And test it:
Import "copyfile.m"
Extern
	Function CopyFileEx:Int( src$z, dst$z)
EndExtern


Note: i havent tested this, just looked over the docs.


tonyg(Posted 2007) [#3]
Provide example code and let us know what goes wrong.
Anyway :
Create test1.txt which includes text
Hello World!
save it in the same directory as the following bmx code :
Local a : String = "test1.txt"
Local b : String = "test2.txt" 
CopyFile(a,b)

What happens?


Garfield(Posted 2007) [#4]
OK tonyg,

test2.txt is created

this will exactly do what it say.

I think my problem are these permissions under osx, espacially my prog will copy a file from the /Volumes/No Name/ fat32 formatted partition for XP on my MAC!

So first I will try to copy the files from the HFS Partion, then I will lok for the code from grable.


Garfield(Posted 2007) [#5]
I got it !

I thought for the dst$ declination is only the path needed, because the filename is alrady known ;)

Now src and dst has full path and name of the file ( in this case both the same, I need it for backup to another partition) and everything is fine !!!

The error sittinīin front of the computer......

OK , the next problem is, how can I create a progressbar for the copy process?

I copy files and a folder, the folder contains approx 600MB data

my system needs apprx 1 minute to copy this and in this time I saw only the spinning ball (OS-X, I guess with XP the "sand glass" watch)

is there at least one chance to get a progress bar??


grable(Posted 2007) [#6]
This is a simple way of making a progressbar possible.
Its just CopyFile() and CopyStream() slapped together with a callback, note the size of the buffer (4096) will dictate how often the callback gets called.
CopyFileProgress( "infile", "outfile", progress)

Function progress( pos:Int,size:Int)
	Print pos +" < " + size
EndFunction

Function CopyFileProgress:Int( src:String,dst:String, callback(pos:Int,size:Int))
	If Not callback Then Return CopyFile( src,dst)
	Local in:TStream = ReadStream( src), ok:Int
	If in Then
		Local out:TStream = WriteStream( dst )
		If out Then
			Local buf:Byte[4096]
			Try
				While Not in.Eof()
					callback( in.Pos(), in.Size())
					out.WriteBytes buf,in.Read( buf,buf.Length)
				Wend
				callback( in.Pos(), in.Size())
				ok = True
			Catch ex:TStreamWriteException
			EndTry
			out.Close()
		EndIf
		in.Close()
	EndIf
	Return ok
EndFunction



Garfield(Posted 2007) [#7]
Thanks a lot grable, I will try this later.

This is maybe a little comlicated for that what I want.
My prog is for a easy daily Backup of my business-programm in my office.
Itīs only copy some files and a folder.
So for an easier and simplier progress-bar I will copy the contents of the folder file for file and show the activity, itīs only for my mind to see what happens...


Garfield(Posted 2007) [#8]
Hello again, itīs eastertime and space for me to work on my project.

Sorry grable, I dont understand this code...

Please axplain it if You have time.

A Function in a function (progress)?
CopyFileProgress( "infile", "outfile", progress)

and another function in a function??
callback(pos:Int,size:Int)

I havent any idea how this work

I have done it with all files copy within the folder, but the gui wont updatet and I see the copied files only if the job has done, after 2 minutes ( approx 800 files, 550 MB in this folder)
Thats the same behaviour as I have with copyfolder, I cant see anything.
the next problem is to make shure, that all subfolders and subsubfolders are copied, so I have to modify my listaddlast-Code, i have forgotten the subfolders :)
ok thats no hard work...

but

Why the gui becomes no priority bitween my Loop ???

For Local alleFiles:TFiles = EachIn filelist

if it dont work too to show something bitween these loop, I can make it again with the easy-Command coyfolder

and wait until it ends

or I will do it with the mysterious callback function...


Garfield(Posted 2007) [#9]
Hello again, itīs eastertime and space for me to work on my project.

Sorry grable, I dont understand this code...

Please axplain it if You have time.

A Function in a function (progress)?
CopyFileProgress( "infile", "outfile", progress)

and another function in a function??
callback(pos:Int,size:Int)

I havent any idea how this work

I have done it with all files copy within the folder, but the gui wont updatet and I see the copied files only if the job has done, after 2 minutes ( approx 800 files, 550 MB in this folder)
Thats the same behaviour as I have with copyfolder, I cant see anything.
the next problem is to make shure, that all subfolders and subsubfolders are copied, so I have to modify my listaddlast-Code, i have forgotten the subfolders :)
ok thats no hard work...

but

Why the gui becomes no priority bitween my Loop ???

For Local alleFiles:TFiles = EachIn filelist

if it dont work too to show something bitween these loop, I can make it again with the easy-Command coyfolder

and wait until it ends

or I will do it with the mysterious callback function...

by the way how do You separate the code from the text in the posts?????


Garfield(Posted 2007) [#10]
Deletet because of double posting


Garfield(Posted 2007) [#11]
Deletet because of double posting


Garfield(Posted 2007) [#12]
Deletet because of double posting


Garfield(Posted 2007) [#13]
Deletet because of double posting


Garfield(Posted 2007) [#14]
Deletet because of double posting


Garfield(Posted 2007) [#15]
Deletet because of double posting


Garfield(Posted 2007) [#16]
Deletet because of double posting


Garfield(Posted 2007) [#17]
Deletet because of double posting


Garfield(Posted 2007) [#18]
Deletet because of double posting


Garfield(Posted 2007) [#19]
Deletet because of double posting


Garfield(Posted 2007) [#20]
Deletet because of double posting


Garfield(Posted 2007) [#21]
Delete because of double posting