Delete file from folder

vmjan02

Well-known Member
Joined
Aug 15, 2012
Messages
1,057
Office Version
  1. 365
  2. 2021
  3. 2019
  4. 2016
  5. 2013
I have the code and is working all perfect, but it is not deleting only this file from folder (Interaction Reports_637169203236732652.xlsx). Not sure as what is wrong


VBA Code:
Sub DeleteRawfile()

'' This will Delete the raw files from the folder


Sheets("Reference").Select

    Dim oFolder As Object, oFile As Object, i As Integer
    Dim xDir As String
    xDir = Application.ActiveWorkbook.Path
    With Worksheets("Reference")
    With CreateObject("Scripting.FileSystemObject")
        Set oFolder = .GetFolder(xDir)
        For Each oFile In oFolder.Files
            Select Case LCase(.GetExtensionName(oFile))
                Case "xls", "xlsb", "csv", "xlsx"
                Cells(i + 2, 8) = oFile.Path
                On Error Resume Next
                kill oFile.Path --- 'delete files from folder'
                i = i + 1
            End Select
        Next oFile
    End With
    End With
Range("H2:H20").Select
Selection.Cells.Clear
Range("a2").Select

End Sub
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Does the code delete all the files in the folder or it doesn't delete any files at all? Can you give more details please?
 
Upvote 0
I deleted all the files apart from this file (Interaction Reports_637169203236732652.xlsx)
 
Upvote 0
Hi, got the solution

added this code before kill

VBA Code:
SetAttr oFile.Path, vbNormal


VBA Code:
Sub DeleteRawfile()
'' This will Delete the raw files from the folder

Sheets("Reference").Unprotect "etmc123$"
Sheets("Reference").Select

    Dim oFolder As Object, oFile As Object, i As Integer
    Dim xDir As String
    xDir = Application.ActiveWorkbook.Path
    With Worksheets("Reference")
    With CreateObject("Scripting.FileSystemObject")
        Set oFolder = .GetFolder(xDir)
        For Each oFile In oFolder.Files
            Select Case LCase(.GetExtensionName(oFile))
                Case "xls", "xlsb", "csv", "xlsx"
                Cells(i + 2, 8) = oFile.Path
                SetAttr oFile.Path, vbNormal   '' This new line added and all work well.
                On Error Resume Next
                kill oFile.Path
                i = i + 1
            End Select
        Next oFile
    End With
    End With
Range("H2:H20").Select
Selection.Cells.Clear
Range("a2").Select
Sheets("Reference").Protect "etmc123$"
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,584
Messages
6,120,387
Members
448,956
Latest member
JPav

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