How to convert a MT940 bank file to CSV readable format for data load

mithun_laha
New Contributor II
 
1 ACCEPTED SOLUTION

RobbSalzmann
Valued Contributor

Here's an example to get you started.

'main
ConvertMT940ToCSV("path_to_MT940_file.sta", "output.csv")

'class functions
Private Sub WriteToCSV(transactions As List(Of String), filePath As String)
    Using writer As New StreamWriter(filePath)
        writer.WriteLine("Date,Credit/Debit,Amount") ' CSV Header
        For Each transaction As String In transactions
            writer.WriteLine(transaction)
        Next
    End Using
End Sub

Private Function ParseTransaction(transactionLine As String) As String
    ' Simplified parsing: Extract date and amount as example
    Dim pattern As String = "^\:61\:(\d{6})(C|D)(\d+,\d{0,2})"
    Dim match As Match = Regex.Match(transactionLine, pattern)
    If match.Success Then
        Dim dateValue As String = match.Groups(1).Value
        Dim creditDebit As String = match.Groups(2).Value
        Dim amount As String = match.Groups(3).Value.Replace(",", ".") ' Change comma to dot for CSV
        Return $"{dateValue},{creditDebit},{amount}"
    End If
    Return String.Empty
End Function

 

View solution in original post

5 REPLIES 5

RobbSalzmann
Valued Contributor

Here's an example to get you started.

'main
ConvertMT940ToCSV("path_to_MT940_file.sta", "output.csv")

'class functions
Private Sub WriteToCSV(transactions As List(Of String), filePath As String)
    Using writer As New StreamWriter(filePath)
        writer.WriteLine("Date,Credit/Debit,Amount") ' CSV Header
        For Each transaction As String In transactions
            writer.WriteLine(transaction)
        Next
    End Using
End Sub

Private Function ParseTransaction(transactionLine As String) As String
    ' Simplified parsing: Extract date and amount as example
    Dim pattern As String = "^\:61\:(\d{6})(C|D)(\d+,\d{0,2})"
    Dim match As Match = Regex.Match(transactionLine, pattern)
    If match.Success Then
        Dim dateValue As String = match.Groups(1).Value
        Dim creditDebit As String = match.Groups(2).Value
        Dim amount As String = match.Groups(3).Value.Replace(",", ".") ' Change comma to dot for CSV
        Return $"{dateValue},{creditDebit},{amount}"
    End If
    Return String.Empty
End Function

 

Thanks for sharing the code. 

I am getting one issue If my pattern comes from different code example 20, 25 codes, I am unable to write that transaction in single line. Have you encountered the same?

Krishna
Valued Contributor

@mithun_laha  - Did you explore the below Market Place Solution? BAI Parser Solution.

 

Krishna_0-1715019035239.png

 

Thanks
Krishna

mithun_laha
New Contributor II

Hi Krishna - Yes, I did. Its only for BAI file format and works on version 8.