qa.hlp (Table of Contents; Topic list)
Important Notice
The pages on this site contain documentation for very old MS-DOS software, purely for historical purposes. If you're looking for up-to-date documentation, particularly for programming, you should not rely on the information found here, as it will be woefully out of date.
CopyFile
   Example  Back  Contents  Index
──────────────────────────────────────────────────────────────────────────────
 
;* CopyFile - Copies a file from a specified directory to another. Allows
;* two different copy methods. See the OpenFile, CloseFile, ReadFile, and
;* WriteFile procedures for specific examples on opening, closing, reading
;* from, and writing to files.
;*
;* Shows:   DOS Functions - 3Ch (Create File)
;*                          5Bh (Create New File)
;*          Instruction - clc
;*
;* Params:  imode  - 0 = Create new target file or overwrite existing file
;*                   1 = Abort and return error code if target file already
;*                       exists (only for DOS versions 3.0 and higher)
;*          fspec1 - Pointer to ASCIIZ source file specification
;*          fspec2 - Pointer to ASCIIZ target file specification
;*
;* Return:  Short integer with error code
;*          0 if successful
;*          1 if error
 
        .DATA
buffer  DB      BUFFER_SIZE DUP(?)      ; Buffer for diskette read
 
        .CODE
        EXTRN GetVer:PROC
 
CopyFile PROC \
        USES ds si di, \
        imode:WORD, fspec1:PTR BYTE, fspec2:PTR BYTE
 
        LOCAL eof_flag:BYTE
 
; Open source file for read only
 
        LoadPtr ds, dx, fspec1          ; Point DS:DX to source file
        mov     ax, 3D00h               ; AH = function #, AL = access code
        int     21h                     ; Open File (for read only)
        jc      e_exit
        mov     si, ax                  ; SI = file handle for source
 
; Open target file according to copy mode
 
        LoadPtr ds, dx, fspec2          ; Point DS:DX to target file
        cmp     imode, 1                ; Determine DOS function
        je      check                   ; Imode = 1?
        mov     ah, 3Ch                 ; No?  Request Create File
        jmp     SHORT set               ;   (destroy existing)
check:  call    GetVer                  ; Yes?  First check DOS version
        cmp     ax, 300                 ; 3.0 or higher?
        jb      close                   ; No?  Abort with error code
        mov     ah, 5Bh                 ; Request Create New File
 
set:    sub     cx, cx                  ; Normal attribute for target
        int     21h                     ; DOS function for target file
        jc      close                   ; If open error, abort
        mov     di, ax                  ; DI = file handle for target
 
; Both files successfully opened. Now read from source and copy to target.
 
        mov     ax, @data
        mov     ds, ax                  ; DS:DX = buffer. Read/write
        mov     dx, OFFSET buffer       ;   to and from here.
        mov     eof_flag, 0             ; Initialize end-of-file flag
loop1:  mov     bx, si                  ; Handle for source file
        mov     cx, BUFFER_SIZE         ; CX = number of bytes to read
        mov     ah, 3Fh                 ; Request DOS read
        int     21h                     ; Read from File
        jc      close                   ; If error, exit
        cmp     ax, cx                  ; All bytes read successfully?
        je      @F                      ; Yes?  Continue
        inc     eof_flag                ; No?  Raise flag
@@:     mov     bx, di                  ; Handle for target file
        mov     cx, ax                  ; Write number of bytes read
        mov     ah, 40h                 ; Request DOS write
        int     21h                     ; Write from buffer to target file
        jc      close                   ; If error, exit
        cmp     eof_flag, 0             ; Finished?
        je      loop1                   ; No?  Loop to read next block
        clc                             ; Yes?  Clear CY to indicate
                                        ;   success
close:  pushf                           ; Preserve flags while closing
        mov     bx, di                  ; Handle for target file
        mov     ah, 3Eh                 ; Request DOS Function 3Eh
        int     21h                     ; Close File
        sub     ax, ax                  ; Clear error code
        popf                            ; Recover flags
        jnc     exit                    ; If successful, exit
 
e_exit: mov     ax, 1                   ; Else set error code
exit:   ret
 
CopyFile ENDP
                                    -♦-