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
 

Some videos you may like

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)

lozzablake

Well-known Member
Joined
Dec 15, 2005
Messages
818
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
 

McM

Board Regular
Joined
May 11, 2004
Messages
67
Thanks for responding lozzablake. I will test this and let you know.

Thanks again

Mcm
 

McM

Board Regular
Joined
May 11, 2004
Messages
67
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.
 

lozzablake

Well-known Member
Joined
Dec 15, 2005
Messages
818

ADVERTISEMENT

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
 

McM

Board Regular
Joined
May 11, 2004
Messages
67
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.
 

McM

Board Regular
Joined
May 11, 2004
Messages
67

ADVERTISEMENT

Any ideas?
 

maytas

New Member
Joined
Sep 5, 2006
Messages
18
Hi,

In VBE window select Tools =>References=>Microsoft Scripting Runtime.
 

McM

Board Regular
Joined
May 11, 2004
Messages
67
Thank You lozzablake, this does exactly what I needed.

and

Thank You Maytas for showing me what to change.
 

Watch MrExcel Video

Forum statistics

Threads
1,114,090
Messages
5,545,893
Members
410,711
Latest member
Josh324
Top