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

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.

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.
 

Forum statistics

Threads
1,141,913
Messages
5,709,292
Members
421,625
Latest member
Natalie1107

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
Top