In order for this site to work correctly we need to store a small file (called a cookie) on your computer. Most every site in the world does this, however since the 25th of May 2011, by law we have to get your permission first. Please abandon the forum if you disagree.

Para que este foro funcione correctamente es necesario guardar un pequeño fichero (llamado cookie) en su ordenador. La mayoría de los sitios de Internet lo hacen, no obstante desde el 25 de Marzo de 2011 y por ley, necesitamos de su permiso con antelación. Abandone este foro si no está conforme.

Delete Folder

Xailer professional forum in English
Timm
Posts: 93
Joined: Mon Jun 21, 2010 11:01 am

Delete Folder

Postby Timm » Mon Feb 19, 2018 10:13 am

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.
User avatar
ignacio
Site Admin
Posts: 8538
Joined: Mon Apr 06, 2015 8:00 pm
Location: Madrid, Spain
Contact:

Re: Delete Folder

Postby ignacio » Tue Feb 20, 2018 2:18 pm

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,

Code: Select all

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
Ignacio Ortiz de Zúñiga
[Equipo de Xailer / Xailer team]
http://www.xailer.com
http://www.xailer.info
Timm
Posts: 93
Joined: Mon Jun 21, 2010 11:01 am

Re: Delete Folder

Postby Timm » Fri Mar 09, 2018 1:37 pm

Hi Ignacio,

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

Code: Select all

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.

Return to “English”