Home
Site Structure

Programming Home
  Basic Home
    GUI & OS Home
    QB Knowledge Base
    Professor Answers
    Codename Surena
    QB Downloads

Write Us

Special Links
QB DirectoryNEW

Codename Surena
Forums
Sepent Basic GUI & OS Home
Register Log In
QB Knowledge Base > How To Questions > Number 2

How to copy a file?
You can use the following sub procedure:

SUB FileCopy (SourceFile AS STRING, DestFile AS STRING)
	'The buffer size. 
	CONST BUFFSIZE = 4096

	DIM buffer AS STRING
	DIM SrcFileNum AS LONG
	DIM DestFileNum AS LONG

	'Fill the buffer with spaces so that when we use GET statement, 
	'the desired value will be read from the file.
	buffer = SPACE$(BUFFSIZE)

	SrcFileNum = FREEFILE
	
	'Make sure the source file exists. If it doesn't exist, an error occurs.
	OPEN SourceFile FOR INPUT AS #SrcFileNum
	CLOSE #SrcFileNum
	
	OPEN SourceFile FOR BINARY AS #SrcFileNum
	
	DestFileNum = FREEFILE
	
	'Make sure the destination file is empty.
	OPEN DestFile FOR OUTPUT AS #DestFileNum
	CLOSE #DestFileNum
	
	OPEN DestFile FOR BINARY AS #DestFileNum

	DO UNTIL BUFFSIZE > LOF(SrcFileNum) - SEEK(SrcFileNum) + 1
		GET #SrcFileNum, , buffer
		PUT #DestFileNum, , buffer
	LOOP

	'Copy the remaining part of the file (if any).
	IF SEEK(SrcFileNum) <= LOF(SrcFileNum) THEN
		buffer = SPACE$(LOF(SrcFileNum) - SEEK(SrcFileNum) + 1)
		
		GET #SrcFileNum, , buffer
		PUT #DestFileNum, , buffer
	END IF

	CLOSE #SrcFileNum
	CLOSE #DestFileNum
END SUB
		

Notice the use of BUFFSIZE constant. This constant is used to set the value of the buffer. The buffer is the temporary storage used for copying from the file. If you set this constant to a very small value, the file copy will become slow. Making it larger will make the process faster, but it has a limit. This means that setting the constant to a very large value will not necessarily make file copy faster.