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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
SiddharthRoutCommented:
But I am not checking for upper case or lowercase?

Sid
0
Rowby Goren Makes an Impact on Screen and Online

Learn about longtime user Rowby Goren and his great contributions to the site. We explore his method for posing questions that are likely to yield a solution, and take a look at how his career transformed from a Hollywood writer to a website entrepreneur.

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
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.