One way to clear the contents of a folder full of CSV files is to use standard VB I/O and overwrite them by looping through the folder and rewriting them with an empty string. Here's a reusable procedure to do
this...
Code:
Sub WriteTextFileContents(Text As String, Filename As String, Optional
AppendMode As Boolean = False)
' A reuseable procedure to write, overwrite, or append large amounts of
data
' to a text file in one single step.
Dim iNum As Integer
On Error GoTo ErrHandler
iNum = FreeFile()
If AppendMode Then
Open Filename For Append As #iNum: Print #iNum, vbCrLf & Text;
Else
Open Filename For Output As #iNum: Print #iNum, Text;
End If
ErrHandler:
Close #iNum: If Err Then Err.Raise Err.Number, , Err.Description
End Sub 'WriteTextFileContents()
...and here's how to use it:
Code:
Sub OverWriteCSVs()
Const sText As String = ""
Dim f As Variant, sPath As String
With Application.FileDialog(msoFileDialogFolderPicker)
If .Show = False Then Exit Sub 'User cancels
sPath = .SelectedItems(1)
End With
If Right(sPath, 1) <> "\" Then sPath = sPath & "\"
f = Dir(sPath, 7)
Do While f <> ""
If UCase(Right(f, 3)) = "CSV" Then _
WriteTextFileContents sText, sPath & f
f = Dir 'Get next file
Loop
End Sub
Bookmarks