In this blog you will learn how to:
- Delete a file
- Delete all the files in a folder
- Delete/Create a directory using VBA
Please note that Kill command permanently deletes the file. we cant "undo" the delete.
'Macro Purpose: To Create/Remove a folder
Sub Create_RemoveDirectory()
On Error Resume Next
Kill "C:\Test\*" ' Deletes
all the files in the folder Test
RmDir "C:\Test\" ' Deletes empty folder
MkDir "C:\Test" ' Creates a Folder
name Test
On Error GoTo 0
End Sub
Sub DeleteFile()
On Error Resume Next
Kill ("C:\Test1.xlsx")
Kill ("C:\Test\*.txt")
On Error GoTo 0
End Sub
Execute the code step by step for better understanding
Sub SampleProcedure()
Dim fpath As
String, ffile As String
fpath =
"C:\Test\"
ffile =
Dir(fpath + "*.txt")
Do While
ffile <> ""
Kill (fpath & ffile)
ffile = Dir
Loop
MsgBox
"Files deleted in the given folder"
End Sub
Hope this would help beginners J
No comments:
Post a Comment