Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Kill Directory 1

Status
Not open for further replies.

aweiss

Programmer
Dec 9, 1999
15
0
0
DE
Hello,

how can I kill a Directory from VB? The Directory das Sub-Directories.

Can anyone help me??

Thanks

Bye
 
To remove a directory you need to use the RMDIR function. Unfortunately, you need to recurse throught the sub directories to KILL the files first, since RMDIR only removes empty directories.
 
'Attention with the code !

Delete Directory And All Its Content

'This example imitate the DOS deltree command.
'Insert the following code to your form:

Private Sub Form_Load()
'Replace the 'C:\MyDir' below with the name of the directory you want to delete.
x = DelTree("C:\MyDir")
Select Case x
Case 0: MsgBox "Deleted"
Case -1: MsgBox "Invalid Directory"
Case Else: MsgBox "An Error was occured"
End Select
End Sub

Function DelTree(ByVal strDir As String) As Long
Dim x As Long
Dim intAttr As Integer
Dim strAllDirs As String
Dim strFile As String
DelTree = -1
On Error Resume Next
strDir = Trim$(strDir)
If Len(strDir) = 0 Then Exit Function
If Right$(strDir, 1) = "\" Then strDir = Left$(strDir, Len(strDir) - 1)
If InStr(strDir, "\") = 0 Then Exit Function
intAttr = GetAttr(strDir)
If (intAttr And vbDirectory) = 0 Then Exit Function
strFile = Dir$(strDir & "\*.*", vbSystem Or vbDirectory Or vbHidden)
Do While Len(strFile)
If strFile <> &quot;.&quot; And strFile <> &quot;..&quot; Then
intAttr = GetAttr(strDir &amp; &quot;\&quot; &amp; strFile)
If (intAttr And vbDirectory) Then
strAllDirs = strAllDirs &amp; strFile &amp; Chr$(0)
Else
If intAttr <> vbNormal Then
SetAttr strDir &amp; &quot;\&quot; &amp; strFile, vbNormal
If Err Then DelTree = Err: Exit Function
End If
Kill strDir &amp; &quot;\&quot; &amp; strFile
If Err Then DelTree = Err: Exit Function
End If
End If
strFile = Dir$
Loop
Do While Len(strAllDirs)
x = InStr(strAllDirs, Chr$(0))
strFile = Left$(strAllDirs, x - 1)
strAllDirs = Mid$(strAllDirs, x + 1)
x = DelTree(strDir &amp; &quot;\&quot; &amp; strFile)
If x Then DelTree = x: Exit Function
Loop
RmDir strDir
If Err Then
DelTree = Err
Else
DelTree = 0
End If
End Function


Eric De Decker
vbg.be@vbgroup.nl

Licence And Copy Protection AxtiveX
Source CodeBook for the programmer
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top