Delete Unnecessary File Using Excel Macro

Greetings Experts. I have been trying to find code for a macro to delete a file in a folder that contains the word "Certificate" in the file name. Students send me two files for each assignment. The "Certificate" file is kept as a record in my online gradebook so I do not need to keep this file. I am using Excel 2010.
waynerayAsked:
Who is Participating?
 
SiddharthRoutCommented:
I see that you mixed up my code with Rob's code.

Try this code without changing anything.

Sub DelFiles()
    Dim strPath As String
    Dim strFile As String
    Dim FolderName As String
    Dim lngRow As Long, lngRowCount As Long, lngStartRow As Long
    
    lngRowCount = Sheets("Class2").Range("A" & Rows.Count).End(xlUp).Row
    lngStartRow = 2

    For lngRow = lngStartRow To lngRowCount
        FolderName = Sheets("Class2").Range("A" & lngRow).Value
        '~~> Change folder here
        strPath = "C:\School\Course\TestLoc\Assignments\Week 2\" & FolderName & "\"
        strFile = Dir(strPath)
           
        Do While strFile <> ""
            If InStr(1, strFile, "Certificate", vbTextCompare) Then
                Kill strPath & strFile
            End If
            strFile = Dir
        Loop
    Next lngRow
End Sub

Open in new window


Sid
0
 
SiddharthRoutCommented:
UNTESTED

Try this. It's untested as I quickly wrote this code. If there is any error then do let me know.

Sub DelFiles()
    Dim strPath As String
    Dim strFile As String
    
    '~~> Change folder here
    strPath = "C:\temp\"
    strFile = Dir(strPath)
       
    Do While strFile <> ""
        If InStr(1, strFile, "Certificate", vbTextCompare) Then
            Kill strPath & strFile
        End If
        strFile = Dir
    Loop
End Sub

Open in new window


Sid
0
 
rspahitzCommented:
Sid, I think you can also shortcut it this way (searching for just the certifs through the Dir, then you don't have to check for upper/lowercase):

Sub DelFiles()
    Dim strPath As String
    Dim strFile As String
   
    '~~> Change folder here
    strPath = "C:\temp\"
    strFile = Dir(strPath & "*Certificate*")
       
    Do While strFile <> ""
            Kill strPath & strFile
        strFile = Dir
    Loop
End Sub
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
SiddharthRoutCommented:
But I am not checking for upper case or lowercase?

Sid
0
 
rspahitzCommented:
Well, I guess that's a matter of interpretation. vbTextCompare will do case independent check.  Since it's optional (and will check for exact case) by including it you are forcing it to not check case (overriding the default), whereas the dir command skips the case by default with no extra effort.

I know it's minor; also, by using the text in the Dir, you avoid the extra IF statement.

One other preference:

rather than:
   Do While strFile <> ""
I like to do the opposite:
   Do Until strFile = ""

Again, very minor, but to me it looks more "positive" :)

Other than those minor issues that don't affect the results, I think your code is a winner :)
0
 
waynerayAuthor Commented:
Sid and rspahitz the strFile variable is not populating. I tried both pieces of code. I know there is a file containing the word "Certificate" in the first folder.
0
 
SiddharthRoutCommented:
Show me the exact code that you are using?

specially this line

strPath = "C:\temp\"

Sid
0
 
waynerayAuthor Commented:
Possibly, I have broken the code but here is my macro incorporating the code. I have a folder for each student. The macro looks at each student folder but is not finding a file with the word "Certificate" in it.

Sub DelFiles()
    Dim strPath As String
    Dim strFile As String
    Dim lngRow As Long, lngRowCount As Long, lngStartRow As Long
   
lngRowCount = Cells(65536, "A").End(xlUp).Row
lngStartRow = 2



For lngRow = lngStartRow To lngRowCount
    '~~> Change folder here
       
    '~~> Change folder here
     '~~> Change folder here
    strPath = "C:\temp\"
    strFile = Dir(strPath)
       
    Do While strFile <> ""
        If InStr(1, strFile, "Certificate", vbTextCompare) Then
            Kill strPath & strFile
        End If
        strFile = Dir
    Loop
Next lngRow
End Sub
0
 
SiddharthRoutCommented:
Ah!

Few qustions

1) What are you using a For loop?
2) Are the files in the temp folder?

Sid
0
 
waynerayAuthor Commented:
The for loop is looping through each student folder. I use the names in my grade book as the folder name. The do while is looking through the files in each folder (I thought). I have a folder for each student with multiple files. One of the files has the word "Certificate" in it. I want to delete that file.
0
 
SiddharthRoutCommented:
>>>The for loop is looping through each student folder

But that loop is not doing anything :)

Can you upload your file so that we can give you the exact code?

Sid
0
 
waynerayAuthor Commented:
My file has sensitive information that can get me into trouble. Has student names and grades. However, I will provide a sanitized  file. The attached file has two macros. The first one looks at student names and makes sure I have a folder for that week for them. If there is no folder, one is created. That one works. I tried to join the delete file code provided with my working macro. I thought the for would loop through the folders and the do while would loop through the files. I know the for loops through the folders.  

Test.xlsm
0
 
waynerayAuthor Commented:
Raised points.
0
 
waynerayAuthor Commented:
Worked like a charm. Thanks!
0
 
SiddharthRoutCommented:
Did it work?

Sid
0
 
SiddharthRoutCommented:
Ok I forgot to refresh the thread :)

Sid
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.