Solved

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

Posted on 2013-05-31
11
468 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
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
 
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

813 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

12 Experts available now in Live!

Get 1:1 Help Now