View Full Version : Copying A File
PointOfLight
11-27-2006, 02:31 AM
Here is some code that you can use to copy a file from one place to another:
sdim(data$, TBYTE, chunksize$);
fin$ = fopen(inputfile$, "r");
fout = fopen(outputfile$, "w");
while(not(feof(fin$)))
fread(data$, sizeof(data$) - 1, 1, fin$);
fwrite(data$, sizeof(data$) - 1, 1, fout$);
end;
fclose(fin$);
fclose(fout$);
The bigger chunksize$ is, the faster the copy.
There are two things I am unsure of with this code. I don't know if doing an fopen with a parameter of "w" will delete the file first if it already exists, but you might have to delete the file by hand first. The second thing I'm not sure about is what will happen if the amount of information read in is smaller than chunksize$.
kornalius
02-13-2007, 11:10 PM
Update: Change the two lines:
readin$ = fread(data$, chunksize$, 1, fin$);
fwrite(data$, readin$, 1, fout$);
This will solve the problem when the amount of bytes read is smaller than the chunksize$.
"w" will rewrite the file if it exists.
This works great on Text Files, but is there any way to copy binary files? I need to copy select image files from one directory to another, and can't seem to figure it out. Any help would be GREATLY appreciated.
Thank you very much!
-pvs
DirectDance
02-26-2007, 07:41 AM
@pvs
Try this one:
CopyFile(onlypathname1$ % "blabla.jpg", onlypathname2$ % "blubblub.jpg", 0);
It is declared in the windows.ppl file:
#declareapi CopyFile kndll CopyFile 3 1
PointOfLight
02-26-2007, 02:41 PM
I'm curious as to what problems you were having using the original code to copy binary files? I actually used the PPL logo when testing my code and it worked just fine. However, DirectDance is right in that you can use the CopyFile method to copy files as well. I was just trying to supply an alternative that would let you control the amount of data that was copied each time.
First, let me thank BOTH of you, PointOfLight and DirectDance. I am happy to see that these forums are alive and healthy. :D I am new to PPL ... haven't programmed much since my Clipper/ASM and VBasic 5 days. So, being both a newbie and rusty at the same time, I might need some help from time-to-time.
Anyway, I have yet to try DirectDance's suggestion, but it definitely looks promising, although I realize it will come with a fair amount of overhead. Thank you SO MUCH for the quick reply! I can't wait to give it a whirl.
PointOfLight ... I had written these things in other languages ages ago, and realizing that PPL has no internal function for it, I saw that I needed to delve into my aging gray matter, and rewrite it. Amazingly, what I first came up with was extremely similar to what you posted, and worked fine for text files, but it would not work with binary ones.
When I saw your post, I decided to make some minor changes to my code, using the "sdim" and "TBYTE" coding instead of my own (which set up a memvar to use as a buffer). Unfortunately it didn't make any difference ... still worked fine on a .TXT file, but would only copy the first four chars of a .PNG.
I had NOT tried running it on my Cingular 8525 (still haven't), but it failed every time running on the Intel box. Maybe I should've tried on the handheld?
Anyway, thanks you both again. I'm sure I'll be running into you two now and then ... look forward to it.
-pvs
[UPDATE: OK, the code shown by DirectDance works fine on the Intel Box. Won't be able to try it on my PDA until later, but so far, so good. Thanks a LOT!] :)
PointOfLight
02-26-2007, 11:02 PM
Actually, I'm still a bit surprised. The code I posted was actually tested against an Intel box, not a PPC. Guess I'll have to try it again at some point.
I would appreciate it if you could. I am very curious (am I doing something completely wrong?) FYI, the files I'm attempting to copy can go over 100k. Was the logo file you tested of a comparable size?
Thanks again, for your help and interest .
-pvs
PointOfLight
02-27-2007, 05:31 PM
Actually, the logo file was relatively small. However, I don't think that should matter. I will try to monkey with this today and see what I get.
Thanks again, Eric. I would be happy to hear your findings. FWIW, the code fragment I wanted to run is now working nicely ... especially since I DL'd 1.21 :)
Now to build a way for end-users to control this app flexibly, and I might have my first contribution to the WM5 world.
Thank you ... SINCERELY ... for your help!
-pvs
Anything, Eric? I'm just wondering if I should try this type of function again, using v1.21. Maybe v1.20 had a problem in this area?
vBulletin® v3.8.4, Copyright ©2000-2012, Jelsoft Enterprises Ltd.