Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 519
  • Last Modified:

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
0
ocsscott
Asked:
ocsscott
1 Solution
 
rettiseertCommented:
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
0
 
ocsscottAuthor Commented:
ok is this visual?  i thought about uising that kinda thing but it shows dialogs and animations this needs to be silent.  is it?
0
 
rettiseertCommented:
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

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
rettiseertCommented:
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
0
 
ocsscottAuthor Commented:
hmmm nothing happened.  no files deleted. any ideas?
0
 
ocsscottAuthor Commented:
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
0
 
ocsscottAuthor Commented:
There are readonly and hidden files in the directory could this be the problem.  I need it to handle that per the spec.
0
 
rettiseertCommented:
It works for me...

just try "c:\oldcomputer" instead of "c:\oldcomputer\"
0
 
ocsscottAuthor Commented:
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
0
 
rettiseertCommented:
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...
0
 
ocsscottAuthor Commented:
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.  
0
 
Robberbaron (robr)Commented:
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
0
 
GPrentice00Commented:
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.
0
 
ocsscottAuthor Commented:
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.
0
 
moduloCommented:
PAQed with points refunded (400)

modulo
Community Support Moderator
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now