Solved

VB.Net create a string for a date from a filename with a different format

Posted on 2013-05-31
11
456 Views
Last Modified: 2013-07-11
Evening,

I am working on a VB.net project with a windows form. I’m new to this so please bear with me.

Is someone able to provide some sample code on how to create a string which will display only the date in a different format? I know I am not being clear here, sorry, I don’t know the proper terminology to explain what I’m looking for. Please see my example below on what I require.

In C:\test I have the below files all written in the format “AccountNumber-YYYYMMDD-ClientName.pdf”. This filename will always be like this.

12345-20130525-John Smith.pdf
24545-20130526-Ted Wilson.pdf
2220034-20130527-Peter Shantel.pdf
423423-20130528-Kevin Charlot.pdf

I need some code which will read the file name and translate the date to “MM/DD/YYYY” in a string, so the output of the above example would look like this:

05/25/2013
05/26/2013
05/27/2013
05/28/2013

My code below writes information to a text file, where the “MM/DD/YYYY” is, is where the date will go from the file name. At the end of line 11.

'Define random numbers for comment field of each file

        Dim randomclass As New System.Random()
        Dim randomnumber As Integer

'create file from destination directory of all files

Dim str As String
        For Each rfiles As String In System.IO.Directory.GetFiles(destdir)
            randomnumber = randomclass.Next(10000, 99999)
            str = str & rfiles & "|" & System.IO.Path.GetFileNameWithoutExtension(rfiles).Split("-")(0).Trim & "|" & "_" & "||" & "docgroup" & "|" & "doctype" & "|" & "docsubtype" & "|" & "swfoi" & randomnumber & "|" & "MM/DD/YYYY" & "|" & Environment.NewLine

Next

        Dim outputname As String = [String].Format("C:\Shared\ITDept\swfoisrc\swfoi{0}.txt", DateTime.Now.ToString("MMddyyyyhhmmss"))
        System.IO.File.WriteAllText(outputname, str)

Open in new window


Output of this would like like below:

C:\test\swfoi\05312013174857\12345-20130525-John Smith.pdf|12345|_||docgroup|doctype|docsubtype|swfoi20139|05/25/2013|

Open in new window



Is someone able to tell me how I can achieve this?

Kind Regards,
N
0
Comment
Question by:nobushi
  • 6
  • 3
  • 2
11 Comments
 
LVL 6

Expert Comment

by:jello024
ID: 39212065
Dim s1 = "24545-20130526-Ted Wilson.pdf"
Dim s2 = s1.Substring(s1.IndexOf("-") + 1, 8)
Dim s3 = s2.Substring(0, 4) + "-" + s2.Substring(4, 2) + "-" + s2.Substring(6, 2)
0
 
LVL 6

Expert Comment

by:jello024
ID: 39212068
Dim s1 = "24545-20130526-Ted Wilson.pdf"
Dim s2 = s1.Substring(s1.IndexOf("-") + 1, 8)
Dim s3 = s2.Substring(4, 2) + "/" + s2.Substring(6, 2) + "/" + s2.Substring(0, 4)
0
 
LVL 1

Author Comment

by:nobushi
ID: 39212115
This doesn't appear to work. Unless I'm doing something wrong.

See below of my code with your adjustments:

'Find date in file name and convert to MM/DD/YYYY

        Dim s1 = destdir
        Dim s2 = s1.Substring(s1.IndexOf("-") + 1, 8)
        Dim s3 = s2.Substring(4, 2) + "/" + s2.Substring(6, 2) + "/" + s2.Substring(0, 4)

        'Define random number and letters for comment field of each file

        Dim randomclass As New System.Random()
        Dim randomnumber As Integer

        'create sbt file from dest directory of all files to be indexed.

        Dim str As String
        For Each rfiles As String In System.IO.Directory.GetFiles(destdir)
            randomnumber = randomclass.Next(10000, 99999)
                   str = str & rfiles & "|" & System.IO.Path.GetFileNameWithoutExtension(rfiles).Split("-")(0).Trim & "|" & "_" & "||" & "docgroup" & "|" & "doctype" & "|" & "docsubtype" & "|" & "swfoi" & randomnumber & "|" & s3 & "|" & Environment.NewLine


        Next

        Dim outputname As String = [String].Format("C:\Shared\ITDept\swfoisrc\swfoi{0}.txt", DateTime.Now.ToString("MMddyyyyhhmmss"))
        System.IO.File.WriteAllText(outputname, str)

Open in new window


Output looks like this:

C:\test\swfoi\05312013174857\12345-20130525-John Smith.pdf|12345|_||docgroup|doctype|docsubtype|swfoi20139|we/et/\\ks|

Open in new window


Please let me know.

Thanks,
N
0
 
LVL 1

Author Comment

by:nobushi
ID: 39234969
Is someone able to assist me with this issue?
0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 39235165
Add a function to your class:
    Private Function GetFormattedDateFromFileName(ByVal fileName As String) As String
        Dim parts() As String = fileName.Split("-")
        If parts.Length = 3 Then
            Dim dt As DateTime
            If DateTime.TryParseExact(parts(1), "yyyyMMdd", Nothing, Globalization.DateTimeStyles.None, dt) Then
                Return dt.ToString("MM/dd/yyyy")
            End If
        End If
        Return ""
    End Function

Open in new window


Then, INSIDE your loop, pass each filename to it to get the formatted date back out, and use that value in your output:
        For Each rfiles As String In System.IO.Directory.GetFiles(destdir)

            Dim formattedDate As String = GetFormattedDateFromFileName(rfiles)

            randomnumber = randomclass.Next(10000, 99999)
            Str = Str() & rfiles & "|" & System.IO.Path.GetFileNameWithoutExtension(rfiles).Split("-")(0).Trim & "|" & "_" & "||" & "docgroup" & "|" & "doctype" & "|" & "docsubtype" & "|" & "swfoi" & randomnumber & "|" & formattedDate & "|" & Environment.NewLine

        Next

Open in new window

0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 1

Author Closing Comment

by:nobushi
ID: 39235634
Great, Thank you Idle_Mind.

I do have one question though to get an understanding of this.

What would the "parts.length = 3" represent in line 3 of the function?
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 39235669
When you call Split() it returns an Array of Strings.

Your stated format was:

    12345-20130525-John Smith.pdf

So splitting on "-" should return exactly 3 parts:

    Index 0: 12345
    Index 1: 20130525
    Index 2: John Smith.pdf

Thus "parts.Length = 3" is checking to see if there were actually 3 parts returned by Split().   If the Length was not 3 then the filename was not in the expected format and you might want to put some of logic into the Else block of the If statement (I didn't put one into the example).
0
 
LVL 1

Author Comment

by:nobushi
ID: 39235681
Ahh ok i see.

Thank you so much for the explaination.

N
0
 
LVL 1

Author Comment

by:nobushi
ID: 39312361
Idle_Mind, quick question regarding this posting.

What does the below "TryParseExact(parts(1)" refer to? What is the 1 for?

This is from your code (below).

 Private Function GetFormattedDateFromFileName(ByVal fileName As String) As String
        Dim parts() As String = fileName.Split("-")
        If parts.Length = 3 Then
            Dim dt As DateTime
            If DateTime.TryParseExact(parts(1), "yyyyMMdd", Nothing, Globalization.DateTimeStyles.None, dt) Then
                Return dt.ToString("MM/dd/yyyy")
            End If
        End If
        Return ""
    End Function
 

Open in new window

0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 39312669
See my previous comment:
http://www.experts-exchange.com/Programming/Languages/.NET/Visual_Basic.NET/Q_28144561.html#a39235669

The "1" refers to the second entry of the returned array, which should be the date portion.
0
 
LVL 1

Author Comment

by:nobushi
ID: 39318036
Ahh ok.. i see.

Thanks
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now