$CPU 8086 ' make compatible with XT systems $LIB ALL OFF ' turn off all PowerBASIC libraries $ERROR ALL OFF ' turn off all PowerBASIC error checking $OPTIMIZE SIZE ' optimize for smaller code $COMPILE UNIT ' compile to a UNIT (.PBU) '$COMPILE EXE ' compile to a UNIT (.PBU) DEFINT A-Z ' Required for all numeric functions, forces PB to not ' include floating point in UNIT (makes it smaller) '/*-------------------------------------------------------------------- 'COPYFILE.BAS - routine to copy a file using interrupts 'This function uses only CALL INTERRUPT to open an existing file, create 'a new file and copy the old file to the new one. This function will not 'overwrite an existing file and is not able to use wildcards (*?). '========================================================================= $CODE SEG "MLIB4" '========================================================================= 'By remarking the above lines this function can be used in your PowerBASIC 'programs to copy files without shelling to DOS. The complete path and 'filename for each file, the one to be copied and the new file to be created 'must be passed to this function unless both are in the current directory. 'Examples of usage would be: ' To copy file stuff.dat to newstuff.dat in the current directory; ' oldfile$="stuff.dat":newfile$="newstuff.dat" ' copied%=copyfile%(filename1$,filename2$) ' SELECT CASE copied% ' CASE 1 ' PRINT "File has been successfully copied." ' CASE 2 ' PRINT "Error during open of ";filename1$;", check" ' PRINT "path and file name." ' CASE 3 ' PRINT "Error during creation of ";filename2$;", file may" ' PRINT "already exist or directory may be full." ' CASE 4 ' PRINT "Error during read from ";filename1$;"." ' CASE 5 ' PRINT "Error during write to ";filename2$;" disk may be full." ' END SELECT ' To copy from your current directory to a different directory: ' oldfile$="stuff.dat":newfile$="e:\newdir\newstuff.dat" ' copied%=copyfile%(filename1$,filename2$) ' SELECT CASE copied% ' CASE 1 ' PRINT "File has been successfully copied." ' CASE 2 ' PRINT "Error during open of ";filename1$;", check" ' PRINT "path and file name." ' CASE 3 ' PRINT "Error during creation of ";filename2$;", file may" ' PRINT "already exist or directory may be full." ' CASE 4 ' PRINT "Error during read from ";filename1$;"." ' CASE 5 ' PRINT "Error during write to ";filename2$;" disk may be full." ' END SELECT ' To copy from a different directory to a different directory: ' oldfile$="e:\olddir\stuff.dat":newfile$="e:\newdir\newstuff.dat" ' copied%=copyfile%(filename1$,filename2$) ' SELECT CASE copied% ' CASE 1 ' PRINT "File has been successfully copied." ' CASE 2 ' PRINT "Error during open of ";filename1$;", check" ' PRINT "path and file name." ' CASE 3 ' PRINT "Error during creation of ";filename2$;", file may" ' PRINT "already exist or directory may be full." ' CASE 4 ' PRINT "Error during read from ";filename1$;"." ' CASE 5 ' PRINT "Error during write to ";filename2$;" disk may be full." ' END SELECT ' ' To copy from a different directory to the current directory: ' oldfile$="e:\olddir\stuff.dat":newfile$="newstuff.dat" ' copied%=copyfile%(filename1$,filename2$) ' SELECT CASE copied% ' CASE 1 ' PRINT "File has been successfully copied." ' CASE 2 ' PRINT "Error during open of ";filename1$;", check" ' PRINT "path and file name." ' CASE 3 ' PRINT "Error during creation of ";filename2$;", file may" ' PRINT " already exist or directory may be full." ' CASE 4 ' PRINT "Error during read from ";filename1$;"." ' CASE 5 ' PRINT "Error during write to ";filename2$;" disk may be full." ' END SELECT ' '/*-------------------------------------------------------------------- ' COPYFILE(SOURCE$,DESTINATION$) ' Copies file SOURCE$ to DESTINATION$. ' Returns: 1 = copy successful ' 2 = error opening SOURCE$ ' 3 = error creating DESTINATION$. The file probably ' aready exists ' 4 = fatal error reading SOURCE$ ' 5 = fatal error writing DESTINATION$, perhaps disk is full. ' EXAMPLE: T = COPYFILE("C:\BATCH\NORMAL.BAT","C:\TEMP\N2.TXT") '/*-------------------------------------------------------------------- FUNCTION COPYFILE(filename1$,filename2$) PUBLIC AS INTEGER filespec1$=filename1$+CHR$(0) 'make filename1 as asciiz string segment1&=STRSEG(filespec1$) 'get memory segment of filename1 where1&=STRPTR(filespec1$) 'get memory offset of filename1 filespec2$=filename2$+CHR$(0) 'make filename2 an asciiz string segment2&=STRSEG(filespec2$) 'get memory segment of filename2 where2&=STRPTR(filespec2$) 'get memory offset of filename2 buffer$=SPACE$(1024)+CHR$(0) 'define a 1k buffer area where3&=STRPTR(buffer$) 'get memory offset of buffer segment3&=STRSEG(buffer$) 'get memory segment of buffer copyfile%=1 'set successful operation bytestoread%=1024 'assign number of bytes to read bytesread%=1024 'assign number of bytes read REG 1, &H3D00 'AH=3Dh open file function REG 4, where1& 'DX=memory offset of string REG 8, segment1& 'DS=memory segment of string CALL INTERRUPT &H21 'int 21h IF (REG(0) AND 1)<>0 THEN 'if flags register is 0 then open copyfile%=2 'was successful, if not an error is EXIT FUNCTION 'returned to your program END IF 'and the function is exited handle1%=REG(1) 'AX contains the file handle REG 1, &H5B00 'AH=5Bh create/open file function REG 3, &H0000 'CX=0 normal file attribute REG 4, where2& 'DX=memory offset of string REG 8, segment2& 'DS=memory segment of string CALL INTERRUPT &H21 'int 21h IF (REG(0) AND 1)<>0 THEN 'if flags register is 0 then create copyfile%=3 'was successful, if not an error is REG 2, handle1% 'returned to your program, the open REG 1, &H3E00 'file is closed and the function CALL INTERRUPT &H21 'exited EXIT FUNCTION END IF handle2%=REG(1) 'AX contains the file handle WHILE bytesread%=bytestoread% 'when bytes read <> bytes to read REG 1, &H3F00 'the end of the file has been reached REG 2, handle1% 'AH=3Fh file read function, BX=file REG 3, bytestoread% 'handle, CX=bytes to read REG 4, where3& 'DX=memory offset of buffer REG 8, segment3& 'DS=memory segment of buffer CALL INTERRUPT &H21 'int 21h IF (REG(0) AND 1)<>0 THEN 'if flags register is 0 then the copyfile%=4 'read was successful, if not the open REG 2, handle1% 'files are closed and an error is REG 1, &H3E00 'returned to your program, the WHILE CALL INTERRUPT &H21 'WEND loop is exited REG 2, handle2% REG 1, &H3E00 CALL INTERRUPT &H21 EXIT LOOP END IF REG 2, handle2% 'BX=file handle REG 3, REG(1) 'CX=AX from previous call which REG 4, where3& 'contains the number of bytes read REG 8, segment3& 'DX=memory offset of buffer,DS=memory REG 1, &H4000 'segment of buffer,AH=40h file write CALL INTERRUPT &H21 'int 21h IF (REG(0) AND 1)<>0 THEN 'if flags register is 0 then write was copyfile%=5 'successful, if not open files are REG 2, handle1% 'closed and an error is returned to REG 1, &H3E00 'your program CALL INTERRUPT &H21 REG 2, handle2% REG 1, &H3E00 CALL INTERRUPT &H21 EXIT LOOP 'WHILE/WEND loop is exited END IF bytesread%=REG(1) 'AX=number of bytes written which WEND 'since we are here is the same as the 'number of bytes read REG 2, handle1% 'BX=file handle REG 1, &H3E00 'AH=3Eh file close function CALL INTERRUPT &H21 'int 21h REG 2, handle2% 'BX=file handle REG 1, &H3E00 'AH=3Eh file close function CALL INTERRUPT &H21 'int 21h END FUNCTION '/*-------------------------------------------------------------------- ' $INCLUDE "C:\CODE\MLIB\MLIB.INC" ' T = COPYFILE("C:\BATCH\NORMAL.BAT","C:\TEMP\NORMAL2.BAT") ' PRINT ">>"T"<<" ' Y$ = GETKEY