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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Kill and RmDir

Status
Not open for further replies.

Cimso

Technical User
Sep 25, 2001
22
US
Does anyone have any idea why this isn't working?

If Dir(&quot;C:\Custlink&quot;) <> &quot;&quot; Then
Kill (&quot;C:\Custlink\*.*&quot;)
If Dir(&quot;C:\Custlink\Batch&quot;) <> &quot;&quot; Then
Kill (&quot;C:\Custlink\Batch\*.*&quot;)
RmDir (&quot;C:\Custlink\Batch&quot;)
If Dir(&quot;C:\Custlink\Copied&quot;) <> &quot;&quot; Then
Kill (&quot;C:\Custlink\Copied\*.*&quot;)
RmDir (&quot;C:\Custlink\Copied&quot;)
If Dir(&quot;C:\Custlink\Logs&quot;) <> &quot;&quot; Then
Kill (&quot;C:\Custlink\Logs\*.*&quot;)
RmDir (&quot;C:\Custlink\Logs&quot;)
RmDir (&quot;C:\Custlink&quot;)
End If
End If
End If
End If
 
first make 2 function similair to these

Public Function KillFile(f As String) As Boolean
If FileExist(f) Then
Kill f
KillFile = True
Else
KillFile = False
End If
End Function

Function FileExist(ByVal s As String) As Boolean
FileExist = False
If Dir$(s) <> &quot;&quot; Then
FileExist = True
End
End Function
then change your code to this

Killfile (&quot;C:\Custlink\Batch\*.*&quot;)
RmDir (&quot;C:\Custlink\Batch&quot;)
Killfile (&quot;C:\Custlink\Copied\*.*&quot;)
RmDir (&quot;C:\Custlink\Copied&quot;)
Killfile (&quot;C:\Custlink\Logs\*.*&quot;)
RmDir (&quot;C:\Custlink\Logs&quot;)
Killfile (&quot;C:\Custlink\*.*&quot;)
RmDir (&quot;C:\Custlink&quot;)

the problem you may have had was your nested if statements required at least 1 file in each directory to work properly
better yet make a function using the 2 i gave you to kill and remove the directory as the same time
 
A nicer solution may be to use the API...

(Nicked some code from MSDN, and modified for this question)

SHFileOperation does things like the animated 'flying folders' when copying, etc. (And it makes your Delete call easy!)

Copy this into the Declarations of a form....

Option Explicit

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

'Constants included for completeness....

Private Const FO_MOVE As Long = &H1
Private Const FO_COPY As Long = &H2
Private Const FO_DELETE As Long = &H3
Private Const FO_RENAME As Long = &H4



Private Const FOF_MULTIDESTFILES As Long = &H1
Private Const FOF_CONFIRMMOUSE As Long = &H2
Private Const FOF_SILENT As Long = &H4
Private Const FOF_RENAMEONCOLLISION As Long = &H8
Private Const FOF_NOCONFIRMATION As Long = &H10
Private Const FOF_WANTMAPPINGHANDLE As Long = &H20
Private Const FOF_CREATEPROGRESSDLG As Long = &H0
Private Const FOF_ALLOWUNDO As Long = &H40
Private Const FOF_FILESONLY As Long = &H80
Private Const FOF_SIMPLEPROGRESS As Long = &H100
Private Const FOF_NOCONFIRMMKDIR As Long = &H200

Private Type SHFILEOPSTRUCT
hwnd As Long
wFunc As Long
pFrom As String
pTo As String
fFlags As Integer
fAnyOperationsAborted As Boolean
hNameMappings As Long
lpszProgressTitle As String
End Type

' You now need to wrap up a function to call SHFileOperation...

Sub DeleteThisFile(Filename As String)
' send me a folder and I'll delete all subfolders, too
On Error Resume Next

Dim FileStruct As SHFILEOPSTRUCT
Dim X As Long
Dim P As Boolean

FileStruct.pFrom = Filename
FileStruct.fFlags = FOF_SILENT + FOF_ALLOWUNDO + FOF_NOCONFIRMATION
FileStruct.wFunc = FO_DELETE
X = SHFileOperation(FileStruct)
End Sub


' Now just call to delete the top-level folder...

Private Sub Form_Load()
'It'll take out the lot with just one call!
DeleteThisFile (&quot;C:\Custlink&quot;)
End Sub

 
Why not just

Dim oFSO As Scripting.FileSystemObject

Set oFSO = New Scripting.FileSystemObject
oFSO.DeleteFolder folderspec:=&quot;C:\Custlink&quot;, force:=True
Set oFSO = Nothing
 
Personally, I don't the scripting object. Too many security implications for my liking. I would prefer to use the Windows API.

There always more that one way of doing things, It's just a case of horses for courses!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top