1

I am trying to loop over every text file in a folder so that the first and last character in every line is removed as these are quotation marks. Please note that my field separator/delimiter is #|#.

Currently: "Column 1#|#Column2#|#...column_n"

Desired: Column 1#|#Column2#|#...column_n

I have made some progress with the VBA but don't know how which operation to use:

Sub clean_text_file()

Dim WB As Workbook
Dim myPath As String
Dim myFile As String
Dim myExtension As String
Dim FolderPicker As FileDialog

'Optimise macro settings
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Application.EnableEvents = False

'Get the target folder
Set FolderPicker = Application.FileDialog(msoFileDialogFolderPicker)
With FolderPicker
.Title = "Select the folder where you want to convert csv files to text files"
.AllowMultiSelect = False
    If .Show <> -1 Then GoTo NextCode
        myPath = .SelectedItems(1) & "\"
End With

'Cancelling the operation
NextCode:
myPath = myPath
If myPath = "" Then GoTo ResetSettings

'Target file extension
myExtension = "*.txt*"

'Folder which contains the files with the extension
myFile = Dir(myPath & myExtension)

'Loop through every files in the excel folder
Do While myFile <> ""
    'set variable to the opened workbook
    Set WB = Workbooks.Open(FileName:=myPath & myFile)
    'make sure that the workbook has opened
    DoEvents

    'Code to remove enclosing quotation marks from every line
    '''CODE TO REMOVE FIRST AND LAST CHARACTER 

    'Ensure that the workbook is closed
    DoEvents

    'Move onto the next file in the folder
    myFile = Dir
Loop

'Completion Message
MsgBox "Files have been converted to text format"

'Reset optimisation settings
ResetSettings:
    Application.EnableEvents = True
    Application.Calculation = xlCalculationAutomatic
    Application.ScreenUpdating = True

End Sub

Would be grateful if a solution could be provided.

Community
  • 1
  • 1
Crumbo0
  • 49
  • 5
  • This should get you started or (depending on your VBA skills) all the way: https://stackoverflow.com/questions/35434220/vba-replacing-commas-in-csv-not-inside-qoutes/35440236#35440236 – Ralph Nov 07 '17 at 16:02
  • what do you want to do? Save file as text, pipe delimited or something? – MarcinSzaleniec Nov 07 '17 at 16:02
  • I need it to be saved as a text file with the three character delimiter "#|#" – Crumbo0 Nov 07 '17 at 16:46

1 Answers1

1

Within your Do While loop, use:

Dim textline
Do While myFile <> ""
    Open myFile For Input As #1
    Open myPath & "tmp.txt" For Output As #2
    While Not EOF(1)
        Line Input #1, textline
        textline = Mid(textline, 2, Len(textline) - 2)
        Print #2, textline
    Wend
    Close #1
    Close #2
    Kill myPath & myFile
    Name myPath & "tmp.txt" As myFile
    myFile = Dir
Loop

One note: it could be that, because each file is now replaced with a new file, the new file shows up again in the Dir results. If that is the case, you first have to build an array of file names with the Dir command, and then process each file. That would be:

Dim filenames()
Dim i As Integer, j As Integer

ReDim filenames(0)

Do While myFile <> ""
    If (UBound(filenames, 1) <= i) Then
        ReDim Preserve filenames(UBound(filenames) + 10)
    End If
    i = i + 1
    filenames(i) = myPath & myFile
    myFile = Dir
Loop

For j = 1 To i
    myFile = filenames(j)
    Open myFile For Input As #1
    Open myPath & "tmp.txt" For Output As #2
    While Not EOF(1)
        Line Input #1, textline
        textline = Mid(textline, 2, Len(textline) - 2)
        Print #2, textline
    Wend
    Close #1
    Close #2
    Kill myPath & myFile
    Name myPath & "tmp.txt" As myFile
Next j
Paul Ogilvie
  • 25,048
  • 4
  • 23
  • 41
  • Thank you for the quick response! I have modified my while loop to include what you mentioned, but I get a compile error of the 'Delete myFile' part in the while loop. – Crumbo0 Nov 07 '17 at 16:47
  • 1
    Should be `Kill myPath & myFile` (didn't test that as I don't want to delete my files) – Paul Ogilvie Nov 07 '17 at 17:21