How to Copy Files from Basic Without Using SHELL Statement

Article translations Article translations
Article ID: 77876 - View products that this article applies to.
This article was previously published under Q77876
Expand all | Collapse all

On This Page


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.


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 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
CALL getrest

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

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

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"
NAME Source$ AS Dest$


Article ID: 77876 - Last Review: August 16, 2005 - Revision: 2.1
  • Microsoft BASIC Professional Development System 7.0
  • Microsoft BASIC Professional Development System 7.1
  • Microsoft BASIC Compiler 6.0
  • Microsoft BASIC Compiler 6.0b
  • Microsoft QuickBasic 4.0
  • Microsoft QuickBASIC 4.0b
  • Microsoft QuickBasic 4.5 for MS-DOS
Retired KB Content Disclaimer
This article was written about products for which Microsoft no longer offers support. Therefore, this article is offered "as is" and will no longer be updated.

Give Feedback


Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from