Link to home
Start Free TrialLog in
Avatar of wayneray
waynerayFlag for United States of America

asked on

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.
Avatar of SiddharthRout
SiddharthRout
Flag of India image

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

Sid
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 :)
Avatar of wayneray

ASKER

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.
Show me the exact code that you are using?

specially this line

strPath = "C:\temp\"

Sid
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
Ah!

Few qustions

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

Sid
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.
>>>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
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
ASKER CERTIFIED SOLUTION
Avatar of SiddharthRout
SiddharthRout
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Raised points.
Worked like a charm. Thanks!
Did it work?

Sid
Ok I forgot to refresh the thread :)

Sid