How To Kill Files in Folders Named in Cell?

Surii

New Member
Joined
Jul 14, 2011
Messages
9
I use 'FileCopy Source:=' and 'Destination:=' functions to copy files named in cell (column B) from folders named in cell (column A) to another folders named in another cell (column C) which I want to rename them (named in column D).

HTML:
Sub Copy_Files()
    Range("A1").Select
    Range(Selection, Selection.End(xlDown)).Select
    n = Selection.End(xlDown).Row
        For i = 2 To n
            FileCopy Source:=Cells(i, 1).Value & Cells(i, 2).Value, _
            Destination:=Cells(i, 3).Value & Cells(i, 4).Value
        Next i
    MsgBox "File copy complete."

End Sub
</PRE>
:confused: But...when I want to delete all files inside the folders named in column C before I copy files from column B in folders named column A. I tried using 'Kill' --> FileKill Source:=, Kill Source:=, Kill:=, etc... none of these seem like correct functions. Anyone could help?
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Try

Code:
Sub DeleteExample1()
'You can use this to delete all the files in the folder Temp
    On Error Resume Next
    Kill "C:\Temp\*.*"
    On Error GoTo 0
End Sub

Code:
Sub DeleteExample4()
'You can use this to delete the whole folder
'Note: RmDir delete only a empty folder
    On Error Resume Next
    Kill "C:\Temp\*.*"    ' delete all files in the folder
    RmDir "C:\Temp\"  ' delete folder
    On Error GoTo 0
End Sub

Code:
Sub Delete_Whole_Folder()
'Delete whole folder without removing the files first like in DeleteExample4
    Dim FSO As Object
    Dim MyPath As String

    Set FSO = CreateObject("scripting.filesystemobject")

    MyPath = "C:\Temp"  '<< Change

    If Right(MyPath, 1) = "\" Then
        MyPath = Left(MyPath, Len(MyPath) - 1)
    End If

    If FSO.FolderExists(MyPath) = False Then
        MsgBox MyPath & " doesn't exist"
        Exit Sub
    End If

    FSO.deletefolder MyPath

End Sub

Code:
Sub Clear_All_Files_And_SubFolders_In_Folder()
'Delete all files and subfolders
'Be sure that no file is open in the folder
    Dim FSO As Object
    Dim MyPath As String

    Set FSO = CreateObject("scripting.filesystemobject")

    MyPath = "C:\Temp"  '<< Change

    If Right(MyPath, 1) = "\" Then
        MyPath = Left(MyPath, Len(MyPath) - 1)
    End If

    If FSO.FolderExists(MyPath) = False Then
        MsgBox MyPath & " doesn't exist"
        Exit Sub
    End If

    On Error Resume Next
    'Delete files
    FSO.deletefile MyPath & "\*.*", True
    'Delete subfolders
    FSO.deletefolder MyPath & "\*.*", True
    On Error GoTo 0

End Sub

Not sure which vba you need.

Biz
 
Upvote 0
Hi Biz

I'm afraid none of them relate to what I'm after. I don't use file address in the code, instead I have them in cells and I want to code them in terms of sourcing, just like how I code to copy files (the codes in my post on top).

Anyone? :confused: :confused:
 
Upvote 0
You were given solutions here and at vbaexpress. It not clear to me that you understand how to use what you were given. You used Cells() to get a value in a cell. Your posted code iterated through a range of cells. The other way to get a cell value is Range(). The last part is to use concatenation to build your string for Kill.

e.g.
Code:
Dim i as Long
i=2
Range("A2").Value2 = "C:\test"
Kill Range("A" & i).Value2 & "\*"
 
Upvote 0

Forum statistics

Threads
1,224,616
Messages
6,179,908
Members
452,949
Latest member
beartooth91

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