CopyFile subroutine for your file-duplicating pleasure


Hey OptoFans,

I recently needed to copy a file from one name to another. We don’t currently have anything built-in to rename or copy a file, so I wrote my own little subroutine, and thought I’d share.

The code in the sub looks like this, in case you’re curious how I did it but don’t need it just this second. Note I used TransferNChars which works like a charm for files. If you’re using it for TCP stuff, you could run into buffer limitations in the tcp/ip stack if the N (number of bytes you’re transferring) gets too big, like over 1000.

sSourceHandle = "file:r," + sSourceFilename;
sDestHandle = "file:w," + sDestinationFilename;

SetCommunicationHandleValue(sDestHandle, chFileDest);
SetCommunicationHandleValue(sSourceHandle, chFileSource);

// Open the source file
nCHResult = OpenOutgoingCommunication(chFileSource);

if (nCHResult == 0) then // open source should be okay
  nCHResult = OpenOutgoingCommunication(chFileDest);

  nBytesCopied = GetNumCharsWaiting(chFileSource);

  if (nCHResult == 0) then // open dest should be okay

    nCHResult = TransferNChars(chFileDest, chFileSource, nBytesCopied);



if (nCHResult < 0) then // something went wrong, we'll return than instead of the nBytesCopied
  nBytesCopied = nCHResult;

Hope that’s handy!

-OptoMary (1.54 KB)