FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin for Harbour/xHarbour How to remove a directory with sub dirs and files in it?
Posts: 6983
Joined: Fri Oct 07, 2005 07:07 PM
How to remove a directory with sub dirs and files in it?
Posted: Thu Nov 13, 2008 07:57 PM

As topic.
Thanks in advance
Otto

Posts: 445
Joined: Thu Feb 21, 2008 11:58 AM
How to remove a directory with sub dirs and files in it?
Posted: Thu Nov 13, 2008 08:03 PM

Otto,

Via MS-DOS with command DEL and parameter /S is possible to delete all files into all sub-directories.

Or you need to create a recursive function, starting of the last sub-directory and removing all files and, after, removing the own directory... or something like this.

Peace and lighting!

Júlio César M. Ferreira

FWH 8.10 / xHB 1.1.0 / xDevStudio 0.72 / Pelles C 5.0.1 / SQLLIB 1.9
Posts: 445
Joined: Thu Feb 21, 2008 11:58 AM
How to remove a directory with sub dirs and files in it?
Posted: Thu Nov 13, 2008 08:21 PM
Peace and lighting!

Júlio César M. Ferreira

FWH 8.10 / xHB 1.1.0 / xDevStudio 0.72 / Pelles C 5.0.1 / SQLLIB 1.9
Posts: 6983
Joined: Fri Oct 07, 2005 07:07 PM
How to remove a directory with sub dirs and files in it?
Posted: Thu Nov 13, 2008 08:42 PM

Julius,
thank you. I thought there is somewhere a function for this task.

Regards,
Otto

Posts: 9020
Joined: Thu Oct 06, 2005 08:17 PM
How to remove a directory with sub dirs and files in it?
Posted: Thu Nov 13, 2008 09:05 PM
#include "Fivewin.ch"
#include "Directry.ch"


FUNCTION MAIN()

    ? LMKDIR( "TEST" )

    ? MEMOWRIT( "TEST\TEST.TXT", "This is a test" )

    ? DELETEDIR( "TEST" )

    RETURN NIL


FUNCTION DELETEDIR( cDir )

    LOCAL aDir, cName

    LOCAL i

    aDir = DIRECTORY( cDir + "\*.*", "HRD" )

    FOR i = 1 TO LEN( aDir )
        cName = aDir[ i, F_NAME ]

        IF cName == "."; LOOP; ENDIF
        IF cName == ".."; LOOP; ENDIF

        cName = cDir + "\" + cName

        IF "D" $ aDir[ i, F_ATTR ]
            IF !DELETEDIR( cName )
                RETURN .F.
            ENDIF
        ELSE
            IF FERASE( cName ) = -1
                ? "Impossibile cancellare il file " + cName + "."
                RETURN .F.
            ENDIF
        ENDIF
    NEXT

    IF !LRMDIR( cDir )
        ? "Impossibile cancellare la cartella " + cDir + "."
        RETURN .F.
    ENDIF

    RETURN .T.


EMG
Posts: 3358
Joined: Fri Oct 07, 2005 08:20 PM
How to remove a directory with sub dirs and files in it?
Posted: Thu Nov 13, 2008 11:08 PM
Friends:

With xHarbour

// The example creates and deletes directories and outlines possible
// error conditions.

   PROCEDURE Main
      ? CurDrive()+":\"+CurDir()    // result: C:\xhb\tests

      ? MakeDir( "C:\Temp\Data" )   // result: 0

      ? DirRemove( "Data" )         // result: 2

      ? DirRemove( "C:\Temp\data" ) // result: 0

      ? DirRemove( "C:\temp" )      // result: 145
   RETURN


Regards
SOI, s.a. de c.v.
estbucarm@gmail.com
http://www.soisa.mex.tl/
http://sqlcmd.blogspot.com/
Tel. (722) 174 44 45
Carpe diem quam minimum credula postero
Posts: 9020
Joined: Thu Oct 06, 2005 08:17 PM
How to remove a directory with sub dirs and files in it?
Posted: Fri Nov 14, 2008 08:15 AM

DirRemove() only deletes empty directories.

EMG

Posts: 6983
Joined: Fri Oct 07, 2005 07:07 PM
How to remove a directory with sub dirs and files in it?
Posted: Fri Nov 14, 2008 08:54 AM

Thank you, Enrico,
I use your
FUNCTION DELETEDIR( cDir )
and all is working.
Thanks again,
Otto

Posts: 1091
Joined: Thu Nov 17, 2005 11:08 AM
Re: How to remove a directory with sub dirs and files in it?
Posted: Thu Mar 01, 2012 08:52 AM

Does exist a function that a function that allows me to copy
all files and folders even if they are empty?

IF I have this situation and I have to copy all structure the function directoryrecurse does not consider empty directories

C:\cdx\FOTO\la\ABCD>tree /A

C:.
+---altro danno
| +---sub one
| ---subtwo
+---doppio nome
+---double name empty
+---DUE
| +---cartella_vuota
| ---marco boschi
+---empty
+---name
+---nuova cartella
---UNO

Many thanks

marco

Marco Boschi
info@marcoboschi.it
Posts: 9020
Joined: Thu Oct 06, 2005 08:17 PM
Re: How to remove a directory with sub dirs and files in it?
Posted: Thu Mar 01, 2012 09:19 AM
Code (fw): Select all Collapse
FUNCTION DELETEDIR( cDir, lRemove )

    LOCAL aDir, cName

    LOCAL i

    DEFAULT lRemove := .T.

    aDir = DIRECTORY( cDir + "\*.*", "D" )

    FOR i = 1 TO LEN( aDir )
        cName = aDir[ i, F_NAME ]

        IF cName == "."; LOOP; ENDIF
        IF cName == ".."; LOOP; ENDIF

        cName = cDir + "\" + cName

        IF "D" $ aDir[ i, F_ATTR ]
            IF !DELETEDIR( cName )
                RETURN .F.
            ENDIF
        ELSE
            IF FERASE( cName ) = -1
                ? "Impossibile cancellare il file " + cName + "."
                RETURN .F.
            ENDIF
        ENDIF
    NEXT

    IF lRemove
        IF !REMOVEDIR( cDir )
            ? "Impossibile cancellare la cartella " + cDir + "."
            RETURN .F.
        ENDIF
    ENDIF

    RETURN .T.


DLL FUNCTION REMOVEDIR( cPathName AS LPSTR ) AS BOOL;
    PASCAL FROM "RemoveDirectoryA" LIB "kernel32.dll"


EMG
Posts: 1091
Joined: Thu Nov 17, 2005 11:08 AM
Re: How to remove a directory with sub dirs and files in it?
Posted: Thu Mar 01, 2012 10:15 AM

EMG,
I intend to copy all files and folders even if they are empty

deletedir is perfect!

Marco Boschi
info@marcoboschi.it
Posts: 10733
Joined: Sun Nov 19, 2006 05:22 AM
Re: How to remove a directory with sub dirs and files in it?
Posted: Fri Mar 02, 2012 10:44 PM
Mr Otto

To delete a folder with all its sub-folders and all contents you may try DeleteFolder() method FileSystemObject. Here is a function implementing it.

Code (fw): Select all Collapse
function DeleteFolder( cFolderToDelete )

   local oFs

   oFs   := CreateObject( "Scripting.FileSystemObject" )
   if oFs:FolderExists( cFolderToDelete )
      if MsgYesNo( "Delete " + cFolderToDelete + " and all its subfolders?" )
         TRY
            oFs:DeleteFolder( cFolderToDelete, .t. )
            MsgInfo( "Deleted" )
         CATCH
            MsgInfo( "Failed to delete" )
         END
      endif
   else
      MsgInfo( cFolderToDelete + " does not exist" )
   endif

return nil


If second parameter in the call to the method DeleteFolder() is .t., even readonly folders/files are deleted.
Regards



G. N. Rao.

Hyderabad, India
Posts: 10733
Joined: Sun Nov 19, 2006 05:22 AM
Re: How to remove a directory with sub dirs and files in it?
Posted: Sat Mar 03, 2012 12:17 AM
MarcoBoschi wrote:EMG,
I intend to copy all files and folders even if they are empty


deletedir is perfect!

The following example does the same job for me:
Code (fw): Select all Collapse
   local oFs

   oFs   := CreateObject( "Scripting.FileSystemObject" )

   // copy all subfolders (empty also) of f:\dbf\adt\ and their contents
   oFs:CopyFolder( "f:\dbf\adt\*", "f:\tempdbfs\adt\" )

   // copy files only in f:\dbf\adt\
   oFs:CopyFile(   "f:\dbf\adt\*" , "f:\tempdbfs\adt\" )
Regards



G. N. Rao.

Hyderabad, India

Continue the discussion