Solved

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

Posted on 2013-05-31
11
475 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Crystal reports vb.net 2 54
VB.NET (2008) - Refactoring Question 2 24
VB.NET 2008 - SQL Timeout 9 34
Visual Studios 1 76
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.

861 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