Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can I delete more than one file (DEL *.*)?

Utility Program

How can I delete more than one file (DEL *.*)?

by  wgcs  Posted    (Edited  )
While DELETE FILE will only do one file at a time, it's relatively simple to cycle through all the files found that match a given filemask / skeleton. I'm sure you could write this for yourself, but it's always easier to use existing code then think it through again!

This function returns 0 if it successfully deletes all files that match pcMask. If any files can't be deleted, the number of files that couldn't be deleted is returned.

This function should never throw an error, even if some files are in use.

Code:
PROCEDURE Del( pcMask )
* Del( Skeleton )
LOCAL lcPath, lcFile, lnRes, lnDelRes
  lnRes = 0

  DECLARE INTEGER DeleteFile IN kernel32 ;
    STRING lpFileName

  lcPath = ADDBS(JUSTPATH(pcMask))
  lcFile = sys( 2000, pcMask )      && Locate first file with this mask.
  
  do while .Not. Empty( lcFile )
    delete FILE(lcPath+lcFile)
    lnDelRes = DeleteFile(pcFile)
    if lnDelRes=0
      lnRes = lnRes+1
    endif
    lcFile = sys( 2000, pcMask, 1 )  && find next matching file.
  enddo
RETURN lnRes
ENDPROC

PROCEDURE SafeDel( pcFile )
* Delete a file without throwing an error.
DECLARE INTEGER DeleteFile IN kernel32 ;
  STRING lpFileName
res = DeleteFile(pcFile)
RETURN res<>0
ENDPROC
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top