Check if folder is empty:
1 2 3 |
Public Function FolderIsEmpty(myPath As String) As Boolean FolderIsEmpty = CBool(Dir(myPath & "*.*") = "") End Function |
Delete all files in a folder:
1 2 3 |
Public Sub DeleteAllFiles(path As String) Kill path & "*.*" End Sub |
Create text file in a given path with text:
1 2 3 4 5 6 7 8 9 10 11 12 |
Public Sub CreateTextFile(path As String, fileName As String, text As String) Dim fso As Object Dim file As Object Set fso = CreateObject("Scripting.FileSystemObject") Set file = fso.CreateTextFile(path & fileName, True) file.WriteLine text file.Close End Sub |
All together:
1 2 3 4 5 6 7 8 9 10 11 |
Sub Main() Dim path As String path = "C:\Users\Username\Desktop\New folder\" CreateTextFile path, "to_delete.txt", "some text inside the file" If Not FolderIsEmpty(path) Then DeleteAllFiles path End If End Sub |