How to Copy Files from Basic Without Using SHELL Statement (77876)



The information in this article applies to:

  • Microsoft Basic Professional Development System (PDS) for MS-DOS and MS OS/2 7.0
  • Microsoft Basic Professional Development System (PDS) for MS-DOS and MS OS/2 7.1
  • Microsoft BASIC Compiler for MS-DOS and OS/2 6.0
  • Microsoft BASIC Compiler for MS-DOS and OS/2 6.0b
  • Microsoft QuickBASIC 4.0
  • Microsoft QuickBASIC 4.0b
  • Microsoft QuickBASIC 4.5

This article was previously published under Q77876

SUMMARY

Basic has no statement to copy files from one directory, drive, or filename to another. One method of copying files is by SHELLing to MS-DOS and using the COPY command. You can also open files for BINARY access and input the file one byte at a time and send it to another file. This is essentially the same as the MS-DOS COPY command.

Note that the NAME statement will rename a file (or a directory), or if given a different path for the second argument, will move a file to the new destination (deleting the file's copy in the old destination). An example is further below.

MORE INFORMATION

Other methods, such as MS-DOS interrupts, can be used to input larger file chunks at a time. However, trying to duplicate MS-DOS COPY with MS-DOS interrupts is more complicated than the program below.

The program below uses the INPUT statement to input the name of the file to copy and the name of the file to output. The "bigchunk" variable can be changed to fit your application, and can be up to 32K bytes in size.

File Copying Example

DECLARE SUB getbigchunk ()
DECLARE SUB getrest ()
INPUT "Enter original filename : ", name1$
INPUT "Enter filename to copy to : ", name2$
OPEN name1$ FOR BINARY AS #1     ' Open copied from file
OPEN name2$ FOR BINARY AS #2     ' Open copied to file
DIM SHARED onebyte AS STRING * 1
DIM SHARED bigchunk AS STRING * 10000   ' Can be up to 32768 bytes
IF LOF(1) > 10000 THEN      ' To speed up process, get data in chunks
  CALL getbigchunk
END IF
CALL getrest
END

SUB getbigchunk
chunk = LOF(1) \ 10000
FOR i = 0 TO chunk - 1
  GET #1, (i * 10000) + 1, bigchunk
  PUT #2, (i * 10000) + 1, bigchunk
NEXT
END SUB

SUB getrest
IF LOF(1) > 10000 THEN
   startpoint= LOF(1)-(LOF(1) MOD 10000) + 1
ELSE
   startpoint = 1
END IF
FOR i = startpoint TO LOF(1)
  GET #1, i, onebyte               ' Input byte
  PUT #2, i, onebyte               ' Output byte
NEXT
END SUB
				

File Moving Example

The Basic NAME statement can move a file from one directory to another on the same disk. (Also, the NAME statement can rename a directory, but cannot move a directory.) The NAME statement will give the error "RENAME ACROSS DISKS" when attempting to move a file from one drive to another (such as from C:\ to D:\). For more information on NAME, see page 217 in the "Microsoft Basic 7.0 Language Reference" manual provided with Microsoft Basic PDS versions 7.0 and 7.1.

In the following example, the NAME statement moves MYNAME.DAT from C:\TEST to C:\NEWTEST:
Source$ = "C:\TEST\MYNAME.DAT"
Dest$ = "C:\NEWTEST\MYNAME.DAT"
NAME Source$ AS Dest$
				

Modification Type:MinorLast Reviewed:8/16/2005
Keywords:KB77876