Página 1 de 1

Delete Folder

Publicado: Lun Feb 19, 2018 10:13 am
por Timm
Hi!

I delete folders with oFs:DeleteFolder( cFolder, .t. ) but this crashes with an error message when a file in the directory is in use (for example in Adobe Reader).

Does someone have a idea how to check if a folder can be deleted?

Best regards, Timm.

Re: Delete Folder

Publicado: Mar Feb 20, 2018 2:18 pm
por ignacio
Hi Timm,

The easiest way: Just use a TRY..CATCH

But I suggest to first read all the files on that directory and below and try to delete them one by one. If any deletion fails show a message.

Use the following function to retrieve all the files.

Regards,

Código: Seleccionar todo

STATIC FUNCTION RecurseDir( cMask, lRecursive, aFiles )

   LOCAL aDir, aFile
   LOCAL cPath, cFile

   DEFAULT aFiles TO {}, lRecursive TO .F.

   aDir  := Directory( cMask )
   cPath := FilePath( cMask )
   cMask := FileFullname( cMask )

   FOR EACH aFile IN aDir
      cFile := aFile[ F_NAME ]
      AAdd( aFiles, cPath + "\" + cFile )
   NEXT

   IF lRecursive
      aDir := Directory( cPath + "\*.*", "D" )
      FOR EACH aFile IN aDir
         cFile := aFile[ F_NAME ]
         IF "D" $ aFile[ F_ATTR ] .AND. cFile != "." .AND. cFile != ".."
            RecurseDir( cPath + "\" + cFile + "\" + cMask, .T., @aFiles )
         ENDIF
      NEXT
   ENDIF

RETURN aFiles

Re: Delete Folder

Publicado: Vie Mar 09, 2018 1:37 pm
por Timm
Hi Ignacio,

many thanks for your help!
I use this now to check if a file is in use before I delete the folder:

Código: Seleccionar todo

FUNCTION IsFileOpen( cPath )

   local aFile
   local lFileOpen := .f.

   for each aFile in DirectoryRecurse( cPath + "*.*" )
      if FRename( aFile[ F_NAME ], aFile[ F_NAME ] ) <> 0
         lFileOpen := .t.
         exit
      endif
   next

RETURN lFileOpen
That gives me the possibility to inform the user before starting the deleting process.

Best regards, Timm.