Microsoft KB Archive/77876

= How to Copy Files from Basic Without Using SHELL Statement =

Article ID: 77876

Article Last Modified on 8/16/2005

-

APPLIES TO


 * 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

-



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 &quot;bigchunk&quot; 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 &quot;Enter original filename : &quot;, name1$ INPUT &quot;Enter filename to copy to : &quot;, 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 &quot;RENAME ACROSS DISKS&quot; 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 &quot;Microsoft Basic 7.0 Language Reference&quot; 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$ = &quot;C:\TEST\MYNAME.DAT&quot; Dest$ = &quot;C:\NEWTEST\MYNAME.DAT&quot; NAME Source$ AS Dest$

Additional query words: QuickBas BasicCom 6.00 6.00b 7.00 7.10 4.00 4.00b 4.50

Keywords: KB77876

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.