Sub ConvertCSVtoTXT()
Dim fd As FileDialog
Dim selectedFolder As String
Dim csvFolderPath As String
Dim file As String
Dim originalFilePath As String
Dim modifiedContent As String
Dim fileNumber As Integer
Dim fs As Object
Dim lines As Variant
Dim i As Long
Dim lineParts As Variant
Set fd = Application.FileDialog(msoFileDialogFolderPicker)
fd.Title = "Please select a folder"
If fd.Show = -1 Then
selectedFolder = fd.SelectedItems(1)
csvFolderPath = selectedFolder & "\.csv files"
If Dir(csvFolderPath, vbDirectory) = "" Then
MkDir csvFolderPath
End If
Set fs = CreateObject("Scripting.FileSystemObject")
file = Dir(selectedFolder & "\*.csv")
Do While file <> ""
originalFilePath = selectedFolder & "\" & file
fs.CopyFile Source:=originalFilePath, Destination:=csvFolderPath & "\" & file
fileNumber = FreeFile
Open originalFilePath For Input As #fileNumber
modifiedContent = Input$(LOF(fileNumber), fileNumber)
Close #fileNumber
modifiedContent = Replace(modifiedContent, ";", ",")
lines = Split(modifiedContent, vbCrLf)
For i = LBound(lines) To UBound(lines)
If Trim(lines(i)) <> "" Then
lineParts = Split(lines(i), ",")
If UBound(lineParts) = 3 Then
lines(i) = lines(i) & ",Unspecified"
End If
End If
Next i
modifiedContent = Join(lines, vbCrLf)
Open Replace(originalFilePath, ".csv", ".txt") For Output As #fileNumber
Print #fileNumber, modifiedContent
Close #fileNumber
Kill originalFilePath
file = Dir
Loop
MsgBox "All files have been processed!", vbInformation, "Task Complete"
Else
MsgBox "No folder was selected.", vbExclamation, "Operation Cancelled"
End If
Set fd = Nothing
Set fs = Nothing
End Sub