Search code examples
vb.netvisual-studio-2012visual-studio-express

Read file name for 5 to 10 digit account number, is this possible?


I am attempting to write a program in VB.net which will output some values in to a text file. Please be patient with me as I am very new to VB.net.

What I have so far is below:

Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
    Dim str As String
    For Each File As String In System.IO.Directory.GetFiles(TextBox1.Text)
        str = str & File & "|" & System.IO.Path.GetFileNameWithoutExtension(File).Split("-")(0).Trim & "|" & System.IO.Path.GetFileNameWithoutExtension(File).Split("-")(0).Trim & "||" & DateTimePicker1.Text & "|" & Environment.NewLine
    Next

    System.IO.File.WriteAllText("C:\output\output.txt", str)

End Sub

Results of output file (output.txt) when button3 is clicked are below:

C:\DirectoryTest\Clients\2356851-Kathy Winkler - Family Investments.pdf|2356851|2356851||04/10/2013|

C:\DirectoryTest\Clients\58736 -Katrina Armon - Sandlewood Homes Co.pdf|58736|58736||04/10/2013|

C:\DirectoryTest\Clients\Karen Cooper - 001548 - Famtime.pdf|Karen Cooper|Karen Cooper||04/10/2013|

My code so far does exactly what I want it to do, the only thing is that I want to make the code smarter but don’t know how. Smarter as in is there a way I can make the below code only pick up the 5 to 10 digit account number seen in the filename, and if no account number exists in the file name to bring up a message box?

System.IO.Path.GetFileNameWithoutExtension(File).Split("-")(0).Trim & "|" & System.IO.Path.GetFileNameWithoutExtension(File).Split("-")(0).Trim

As you can see from the last line of the output…

C:\DirectoryTest\Clients\Karen Cooper - 001548 - Famtime.pdf|Karen Cooper|Karen Cooper||04/10/2013|

…the customers name “Karen Cooper” is being displayed in both areas where the account number should be displayed. This is why I need to make this code smarter somehow have it search the file name for a 5 to 10 digit account number to display it after the file name as see in the other 2 examples.

Please let me know if this is possible. And do let me know if you have any questions.


Solution

  • You should give this a try. I haven't had a chance to test it but it should work

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        Dim str As String
        For Each File As String In System.IO.Directory.GetFiles(TextBox1.Text)
            Dim strs as String() = System.IO.Path.GetFileNameWithoutExtension(File).Split("-")
            Dim AccountNum as int = 0
            For each section in strs()
                ' Loop through each section separated by - and try to cast it to an int
                 ' you may want to use cLong instead
                Try
                   AccountNum = cInt(section.trim())
                   exit for
                Catch
                End Try
            Next
          ' DO LOGIC HERE TO BUILD OUTPUT with the account num now known
        Next
    
        System.IO.File.WriteAllText("C:\output\output.txt", str)
    
    End Sub