` Printed Icetips Article

Icetips Article



Par2: COPY('*.std','stdfiles.clm')
2003-07-16 -- Greg Berthume
 
> COPY('*.std','stdfiles.clm')
>
> How can I do something like this in Clarion?  API?
>
> Need to copy multiple files into a single file like you can in DOS.

Here's some code I whipped up that does the job if anybody else needs it.
Make sure you add the DOS driver to your app/project.

! Procedure Declaration:

MFileCopy               PROCEDURE(STRING pFolder, STRING pFiles, STRING
pDestFile, BYTE pFlag, BYTE pEraseFile),LONG

! Globals

gDestFile                 CSTRING(128)
gCFile                    CSTRING(FILE:MaxFilePath)

! Usage Example:

  E# = MFileCopy('c:\temp','*.asc','ascfiles.sav',1,1)
  IF E#
     STOP('MFileCopy Error: ' & E#)
  END
  STOP('MFileCopy Completed')


!---------------------------------------------------------------------------
-------------------------------!
MFileCopy PROCEDURE(STRING pFolder, STRING pFiles, STRING pDestFile, BYTE
pFlag, BYTE pEraseFile)
!---------------------------------------------------------------------------
-------------------------------!

CFile     FILE,DRIVER('DOS'),NAME(gCFile),PRE(CFL),CREATE
Record    RECORD,PRE()
Chunk        STRING(128)
          END
    END

DFile     FILE,DRIVER('DOS'),NAME(gDestFile),PRE(DFL),CREATE
Record    RECORD,PRE()
Chunk        STRING(128)
          END
    END

lFolder         CSTRING(FILE:MaxFilePath)
lFiles          CSTRING(20)
lFlag           BYTE
lEraseFile      BYTE

CFileQ          QUEUE(FILE:Queue),PRE(CFQ)
    END

CopyWindow WINDOW,AT(,,148,45),FONT('Tahoma',10,,),CENTER,GRAY,DOUBLE
       IMAGE('cmspackage.ico'),AT(6,12),USE(?Image1)
       STRING('Copying File
To ->'),AT(29,4),USE(?TransferString),TRN,FONT('Tahoma',10,,FONT:regular)
       STRING(@s12),AT(92,4),USE(gDestFile)
       PROGRESS,USE(?ProgressBar),AT(29,16,113,10),RANGE(0,100)
       STRING('File:'),AT(29,31),USE(?String4)
       STRING(@s20),AT(46,31,,10),USE(CFQ:ShortName)
     END


CODE

  lFolder   = pFolder
  lFiles    = pFiles
  gDestFile = pDestFile
  lFlag     = pFlag       ! True if the DestFile needs to be deleted if
exists.
  lEraseFile= pEraseFile  ! True if erase source file after copying to
gDestFile?

  SETPATH(lFolder)
  IF ERRORCODE()
     BEEP(BEEP:SystemExclamation)  ;  YIELD()
     MESSAGE('The specified folder does not exist.'&|
             '||Folder: ' & lFolder & '||The file copy did not occur.', |
             'MFileCopy Error', ICON:Exclamation)
     RETURN(1)
  END

  IF lFlag
     REMOVE(DFile)
  END

  IF EXISTS(gDestFile)
     OPEN(DFile)
     IF ERRORCODE()
        STOP('Destination File Open Error: ' & ERROR())
     END
  ELSE
     CREATE(DFile)
     IF ERRORCODE()
        STOP('Destination File Create Error: ' & ERROR())
     END
     OPEN(DFile)
     IF ERRORCODE()
        STOP('DFile Open Error: ' & ERROR())
     END
  END

  FREE(CFileQ)
  DIRECTORY(CFileQ,lFiles,ff_:Normal)
  IF RECORDS(CFileQ)=0
     RETURN(2)
  END

  SORT(CFileQ,CFQ:ShortName)  ! put the files in file name order

  OPEN(CopyWindow)
  ?ProgressBar{PROP:RangeLow}  = 1
  ?ProgressBar{PROP:RangeHigh} = RECORDS(CFileQ)

  LOOP LP# = 1 TO RECORDS(CFileQ)
    GET(CFileQ,LP#)
    gCFile = CFQ:ShortName
    OPEN(CFile)
    SET(CFile)
    LOOP
      NEXT(CFile)
      IF ERRORCODE()
         CLOSE(CFile)
         IF lEraseFile
            REMOVE(CFile)
         END
         BREAK
      END
      DFL:Chunk = CFL:Chunk
      APPEND(DFile)
      IF ERRORCODE()
         STOP('DFile Append Error: ' & ERROR())
         CLOSE(CFile)
         BREAK
      END
    END
    ?ProgressBar{PROP:Progress} = LP# ; DISPLAY
  END
  CLOSE(DFile)
  CLOSE(CopyWindow)
  RETURN(0)

Randy Goodhew adds:
NOTE: In your LOOP to get records...

  APPEND(DFile)  ! not adequate

use

  APPEND(DFile,BYTES(NameOfRecord)) ! for variable length records


You can also increase the size of the DOS record to at least
the size of a small HD cluster, about 2048 or so.

Greg tested:
Looks like SIZE() is needed and not BYTES().



Printed November 21, 2024, 11:04 am
This article has been viewed/printed 35234 times.