Link to home
Start Free TrialLog in
Avatar of ocsscott
ocsscott

asked on

Need a deltree code fot recursivly delete foles and folders...not using the FSO

I need a hunk of vb6 code to delete a folder and all files and folders inside it.  No matter how many levels deep.  

It can't use the FSO (file system object) do to the dll not being on all machines. it should also delete all files no matter the attributes that are set ie hidden or system

thanks for the help
Avatar of rettiseert
rettiseert

Try this:


Option Explicit

Private Type SHFILEOPSTRUCT
    hWnd As Long
    wFunc As Long
    pFrom As String
    pTo As String
    fFlags As Integer
    fAborted As Boolean
    hNameMaps As Long
    sProgress As String
End Type

Private Const FO_DELETE = &H3
Private Const FOF_ALLOWUNDO = &H40
Private Const FOF_NOCONFIRMATION = &H10

Private Declare Function SHFileOperation Lib "shell32.dll" Alias "SHFileOperationA" (lpFileOp As SHFILEOPSTRUCT) As Long

Private Sub Del(Path As String)

    Dim SHFileOp As SHFILEOPSTRUCT
   
    With SHFileOp
        .wFunc = FO_DELETE
        .pFrom = Path
        .fFlags = FOF_ALLOWUNDO Or FOF_NOCONFIRMATION
    End With

    SHFileOperation SHFileOp
   
End Sub

Private Sub Form_Load()

    Del "c:\vp"     'Delete c:\vp folder
   
End Sub
Avatar of ocsscott

ASKER

ok is this visual?  i thought about uising that kinda thing but it shows dialogs and animations this needs to be silent.  is it?
To make it silent add these constants:

FOF_SILENT = &H4
FOF_NOERRORUI = &H400

And use 'em in the fFlags member:

.fFlags = FOF_ALLOWUNDO Or FOF_NOCONFIRMATION Or FOF_SILENT Or FOF_NOERRORUI

If you want to know if there was an error check the value returned by SHFileOperation (0=OK, other=Error).

Also, you can remove FOF_ALLOWUNDO to delete the files permanently instead of moving them to recycle bin
hmmm nothing happened.  no files deleted. any ideas?
i took of the silent and confirm flags and get the message.

cannot delete the file: cannot read from the source file or disk

but i have confirmed the folder is there I am sending it

"c:\oldcomputer\"

which is there and has hundereds of files and folders in it
There are readonly and hidden files in the directory could this be the problem.  I need it to handle that per the spec.
It works for me...

just try "c:\oldcomputer" instead of "c:\oldcomputer\"
tried didnt matter.  it works on some folders i created but the one I need will not work.  There are hidden and read onnly files in there.

any ideas
mmm... I think this should work just as if you were trying to delete the folder in windows explorer, no matter if there are hidden and read only files.

What happen if you try to delete the folder using windows explorer? Do you get any error msg or the folder is deleted without problems?

Maybe you don't have user permisions to delete this folder or some files inside are in use by other programs...
nope deletes fin in explorer.  I am an admin.  these are files backed up from another pc i am deleting the folder so I can copy a new set of files down from the server.  
Avatar of Robberbaron (robr)
try using the SHFileOp class

works well for me. easy to use. well documented. uses Windows API call totally.
probably does same as rettiseert code but may be more complete.

'SHFileOp - VB program that demonstrates using SHFileOperation
'Copyright (c) 1997 SoftCircuits Programming (R)
'Redistributed by Permission.

'This program may be distributed on the condition that it is
'distributed in full and unchanged, and that no fee is charged for
'such distribution with the exception of reasonable shipping and media
'charged. In addition, the code in this program may be incorporated
'into your own programs and the resulting programs may be distributed
'without payment of royalties.
'
'This example program was provided by:
' SoftCircuits Programming
' http://www.softcircuits.com
primarily, the link from robberbaron requires some navigation to find the class demo, but its pretty much the same code as rettiseert's example - although, with some features that may handle folder permissions better, which is a bit of a glitch sometimes with the raw api - almost never reproducable of course, but an annoyance.
nether code worked for me, both are the common code for the sho class and api. since it didnt work I did not award points, but I really dont care so do as you will with the points.
ASKER CERTIFIED SOLUTION
Avatar of modulo
modulo

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial