|
-
Nov 26th, 2006, 11:04 AM
#1
Thread Starter
Junior Member
Folder deletion problem
I'm trying to make a program which will delete a folder under the directory "C:\Documents and Settings\user_here\Application Data"
I have tried "kill" and "rmdir" but I get error 75 msg" It wants me to kill each file under the dir before I can remove any folder.
I'm not trying to kill the app. data folder just a few folders underneath it.
If anyone has any ideas, reply.
Thanks
-
Nov 26th, 2006, 11:33 AM
#2
Thread Starter
Junior Member
Re: Folder deletion problem
Nevermind found:
VB Code:
Private Sub Form_Load()
'Replace the 'C:\MyDir' below with the name of the directory you want to delete.
x = DelTree("C:\DIR")
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 <> "." And strFile <> ".." Then
intAttr = GetAttr(strDir & "\" & strFile)
If (intAttr And vbDirectory) Then
strAllDirs = strAllDirs & strFile & Chr$(0)
Else
If intAttr <> vbNormal Then
SetAttr strDir & "\" & strFile, vbNormal
If Err Then DelTree = Err: Exit Function
End If
Kill strDir & "\" & 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 & "\" & strFile)
If x Then DelTree = x: Exit Function
Loop
RmDir strDir
If Err Then
DelTree = Err
Else
DelTree = 0
End If
End Function
-
Nov 26th, 2006, 11:59 AM
#3
Re: Folder deletion problem
or you could just use FSO:
VB Code:
' Set reference to Microsoft Scripting Runtime:
Private Sub Command1_Click()
Dim fso As FileSystemObject
Set fso = New FileSystemObject
fso.DeleteFolder "C:\mytest", True
Set fso = Nothing
End Sub
-
Nov 26th, 2006, 12:23 PM
#4
Re: Folder deletion problem
Here is the simple code:
VB Code:
Public Sub RemoveDirectory(ByVal sDir As String)
Shell "Cmd.exe /C Rmdir /S /Q " & sDir
End Sub
'Call this routine as:
'Call RemoveDirectory("C:\Temp\Test\")
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|