Solved

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

Posted on 2013-05-31
11
460 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Multiple file Upload asp.net 2 38
Get String split 5 46
Visual Studio 2013 Shortcut (VB) 4 34
vb.net - How to check if current user is an administrator? 6 34
Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
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 …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

911 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

18 Experts available now in Live!

Get 1:1 Help Now