0

I am uploading files to a directory and I am also just getting the file itself not the directory and the folders.

For example I am getting only this 6122002_Abstract_9-11-07.pdf by using this code hpf.FileName.Substring(hpf.FileName.LastIndexOf("\") + 1).

What I want to do is separate out the 6122002, the Abstract, and the date which is 9-11-07 so I can insert it into a sql database. Does anyone know how to do this?

2 Answers 2

2

If you have this 6122002_Abstract_9-11-07.pdf Try something like

Dim Arr() As String
str = "6122002_Abstract_9-11-07.pdf"
Arr = str.Split("_")

so the array will contain 6122002, Abstract and 9-11-07.pdf

update

Dim number As String = Arr(0)
Dim name AS String = Arr(1)
Dim date As String = Arr(2).Substring(0, Arr(2).Length-4)
5
  • Thanks for getting back to me so quickly but I have already tried that. The issue I am having is trying to then extract the split so I can insert it into a sql database table.
    – sshackles
    Apr 12, 2012 at 16:58
  • you can use the array index to access the each element Apr 12, 2012 at 16:58
  • Can you maybe provide an example? I'm extremely stuck.
    – sshackles
    Apr 12, 2012 at 16:59
  • He is saying Please complete my Homework to get full marks(10 marks for Upvote and 15 marks for acceptance). So finally you will get 25/25 marks. :)
    – Pankaj
    Apr 12, 2012 at 17:00
  • Awesome. Thank you so much. It's working finally. Sometimes it's such a simple thing but you get stuck and I was stuck. Thanks again humpty dumpty.
    – sshackles
    Apr 12, 2012 at 17:17
0

To get the filename without the path and without the extension you can use

IO.Path.GetFileNameWithoutExtension(f)

And you probably should want the date part as a DateTime. You could make a class which takes a filename in its constructor and parses out the parts into properties, for example

Module Module1

    Public Class Paper
        Public Property SerialNumber As UInt64
        Public Property Type As String
        Public Property PublishedDate As DateTime

        Public Sub New(filename As String)
            Dim parts() As String = IO.Path.GetFileNameWithoutExtension(filename).Split("_"c)
            ' You should check there are three parts here.
            ' Also, you could use TryParse to make sure the parsing works.
            Me.SerialNumber = UInt64.Parse(parts(0))
            Me.Type = parts(1)
            Dim dateformats() As String = {"dd-MM-yy"} ' could add more if required
            Me.PublishedDate = DateTime.ParseExact(parts(2),
                                                   dateformats,
                                                   New Globalization.CultureInfo("en-GB"),
                                                   Globalization.DateTimeStyles.None)

        End Sub

    End Class

    Sub Main()
        Dim fs = IO.Directory.GetFiles("C:\temp\threeparts")
        Dim papers As New List(Of Paper)

        For Each f In fs
            papers.Add(New Paper(f))
        Next

        For Each p In papers
            Console.WriteLine("Serial: {0}, Type: {1}, Published: {2}", p.SerialNumber, p.Type, p.PublishedDate.ToString("dd-MMM-yyyy"))
        Next

        Console.ReadLine()

    End Sub

End Module

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.