How to remove a directory with sub dirs and files in it?

How to remove a directory with sub dirs and files in it?

Postby Otto » Thu Nov 13, 2008 7:57 pm

As topic.
Thanks in advance
Otto
User avatar
Otto
 
Posts: 6045
Joined: Fri Oct 07, 2005 7:07 pm

Postby JC » Thu Nov 13, 2008 8: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.
Last edited by JC on Thu Nov 13, 2008 8:38 pm, edited 2 times in total.
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
User avatar
JC
 
Posts: 445
Joined: Thu Feb 21, 2008 11:58 am
Location: Brazil

Postby JC » Thu Nov 13, 2008 8: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
User avatar
JC
 
Posts: 445
Joined: Thu Feb 21, 2008 11:58 am
Location: Brazil

Postby Otto » Thu Nov 13, 2008 8:42 pm

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

Regards,
Otto
User avatar
Otto
 
Posts: 6045
Joined: Fri Oct 07, 2005 7:07 pm

Postby Enrico Maria Giordano » Thu Nov 13, 2008 9:05 pm

Code: Select all  Expand view
#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
User avatar
Enrico Maria Giordano
 
Posts: 8356
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia

Postby Armando » Thu Nov 13, 2008 11:08 pm

Friends:

With xHarbour

Code: Select all  Expand view
// 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
User avatar
Armando
 
Posts: 3076
Joined: Fri Oct 07, 2005 8:20 pm
Location: Toluca, México

Postby Enrico Maria Giordano » Fri Nov 14, 2008 8:15 am

DirRemove() only deletes empty directories.

EMG
User avatar
Enrico Maria Giordano
 
Posts: 8356
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia

Postby Otto » Fri Nov 14, 2008 8:54 am

Thank you, Enrico,
I use your
FUNCTION DELETEDIR( cDir )
and all is working.
Thanks again,
Otto
User avatar
Otto
 
Posts: 6045
Joined: Fri Oct 07, 2005 7:07 pm

Re: How to remove a directory with sub dirs and files in it?

Postby MarcoBoschi » Thu Mar 01, 2012 8: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
User avatar
MarcoBoschi
 
Posts: 1018
Joined: Thu Nov 17, 2005 11:08 am
Location: Padova - Italy

Re: How to remove a directory with sub dirs and files in it?

Postby Enrico Maria Giordano » Thu Mar 01, 2012 9:19 am

Code: Select all  Expand view
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
User avatar
Enrico Maria Giordano
 
Posts: 8356
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia

Re: How to remove a directory with sub dirs and files in it?

Postby MarcoBoschi » Thu Mar 01, 2012 10:15 am

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


deletedir is perfect!
User avatar
MarcoBoschi
 
Posts: 1018
Joined: Thu Nov 17, 2005 11:08 am
Location: Padova - Italy

Re: How to remove a directory with sub dirs and files in it?

Postby nageswaragunupudi » 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: Select all  Expand view
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
User avatar
nageswaragunupudi
 
Posts: 10295
Joined: Sun Nov 19, 2006 5:22 am
Location: India

Re: How to remove a directory with sub dirs and files in it?

Postby nageswaragunupudi » 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: Select all  Expand view
  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
User avatar
nageswaragunupudi
 
Posts: 10295
Joined: Sun Nov 19, 2006 5:22 am
Location: India


Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: Google [Bot] and 24 guests