Solved

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

Posted on 2013-05-31
11
481 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

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

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 86

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 86

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Write to a printer using vb.net 9 61
VB.NET 2008 Winforms Signing 13 42
Copying from a network share 3 26
Learning About The VB.NET TableLayoutPanel Control 5 17
I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

730 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