Delete Files in Dir

McM

Board Regular
Joined
May 11, 2004
Messages
67
I am stuck. How can I delete files in a dir, but not the file I have open in that dir?

Thank for all the help.

McM
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
Try this. You need to have reference set to the Microsoft Scripting Runtime. Obviously test this first!

Code:
Sub DeleteFiles()
'macro to delete all files in a folder
    
    Dim FileSys As FileSystemObject
    Dim objFile As File
    Dim myFolder
    Dim strFilename As String
    Dim dteFile As Date
        
    'set path for files - change for your folder
    Const myDir As String = "C:\"
    
    'set up filesys objects
    Set FileSys = New FileSystemObject
    Set myFolder = FileSys.GetFolder(myDir)
        
    
    'loop through each file and check for name match
    For Each objFile In myFolder.Files
        If objFile.Name <> ThisWorkbook.Name Then
            objFile.Delete
            
        End If
    Next objFile
    
            
    Set FileSys = Nothing
    Set myFolder = Nothing
End Sub
 
Upvote 0
Thanks for responding lozzablake. I will test this and let you know.

Thanks again

Mcm
 
Upvote 0
I dont think I gave enough information.

Here is the code I already have

Dim strPath As String, strFileMask As String
strPath = "C:\Documents and Settings\All Users\Desktop\"
strFileMask = "*test*.xls"
If Dir(strPath & strFileMask) <> "" Then
Kill strPath & strFileMask
Else
MsgBox "No Files Found "
End If

It error on Kill strPath & strFileMask because I have the file open. Is there a way to delete all the xls files with the word test in the name, except the test file that is open?

Thanks for the help.
 
Upvote 0
I have changed the code slightly to only delete if it contains test by using the INSTR function

Code:
Sub DeleteFiles()
'macro to delete all files in a folder
    
    Dim FileSys As FileSystemObject
    Dim objFile As File
    Dim myFolder
    Dim strFilename As String
    Dim dteFile As Date
        
    'set path for files - change for your folder
    Const myDir As String = "C:\"
    
    'set up filesys objects
    Set FileSys = New FileSystemObject
    Set myFolder = FileSys.GetFolder(myDir)
        
    
    'loop through each file and check for name match on *test*
    For Each objFile In myFolder.Files
        If (InStr(1, UCase(objFile.Name), "TEST")) > 0 And objFile.Name <> ThisWorkbook.Name Then
            objFile.Delete
            
        End If
    Next objFile
    
            
    Set FileSys = Nothing
    Set myFolder = Nothing
End Sub
 
Upvote 0
I get a compile error on
Dim FileSys As FileSystemObject

I am sure it is becasue I do not know what to do with this:

'set up filesys objects
Set FileSys = New FileSystemObject
Set myFolder = FileSys.GetFolder(myDir)

I do apologize, I am very new at this.

Thanks for your help.
 
Upvote 0
Thank You lozzablake, this does exactly what I needed.

and

Thank You Maytas for showing me what to change.
 
Upvote 0

Forum statistics

Threads
1,214,652
Messages
6,120,746
Members
448,989
Latest member
mariah3

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top