Solved

ASP.net Adding a timestame to File name

Posted on 2009-07-01
4
486 Views
Last Modified: 2012-05-07
Hello,
I would like to add time stamp to file name in following format.

imagename200907011905.jpg
imagename200907011905.tiff


My problem is that the extension is always different.
How do I separate extension and append time stamp?
I would like to store it in database, so I would like to know the variable name, store procedure as well.

I'm using ASP.NET VB and SQL server 2008 express edition.

Thank you


Dim myfilename As String

        Dim currentTime As String = "0000".Substring(0, 4 - DateTime.Now.Year.ToString().Length) & DateTime.Now.Year.ToString() & "00".Substring(0, 2 - DateTime.Now.Month.ToString().Length) & DateTime.Now.Month.ToString() & "00".Substring(0, 2 - DateTime.Now.Day.ToString().Length) & DateTime.Now.Day.ToString() & "00".Substring(0, 2 - DateTime.Now.Hour.ToString().Length) & DateTime.Now.Hour.ToString() & "00".Substring(0, 2 - DateTime.Now.Minute.ToString().Length) & DateTime.Now.Minute.ToString() & "00".Substring(0, 2 - DateTime.Now.Second.ToString().Length) & DateTime.Now.Second.ToString()

        myfilename = FileField.PostedFile.FileName & currentTime

        Dim c As String = System.IO.Path.GetFileName(myfilename)

        Try

            FileField.PostedFile.SaveAs(Server.MapPath("~/blog/" + c))

            Span1.InnerHtml = "File upload successful."

        Catch ex As Exception
 

        End Try

Open in new window

0
Comment
Question by:jtuttle99
  • 2
4 Comments
 
LVL 3

Accepted Solution

by:
jbeasle3 earned 250 total points
ID: 24759972
There may be a better way, but I would do it like this (see code snippit)
		Dim myfilename As String

		Dim currentTime As String = Format(Now, "yyyyMMddHHmm")

		Dim myFileParts() As String
 

		myFileParts = Split(FileField.PostedFile.FileName, ".")

		If myFileParts.Length > 1 Then

			myfilename = myFileParts(0) & currentTime & "." & myFileParts(1)

		Else

			myfilename = FileField.PostedFile.FileName & currentTime

		End If

		Dim c As String = System.IO.Path.GetFileName(myfilename)

		Try

			FileField.PostedFile.SaveAs(Server.MapPath("~/blog/" + c))

			Span1.InnerHtml = "File upload successful."

		Catch ex As Exception
 

		End Try

Open in new window

0
 
LVL 15

Assisted Solution

by:jinal
jinal earned 250 total points
ID: 24760401
Use System.IO.Path functionality to get detail.

Dim myfilename As String
Dim strExtension As String
Dim strTimeStamp As String
strTimeStamp = DateTime.Now.ToString("yyyyMMddhhmm")
myfilename = System.IO.Path.GetFileNameWithoutExtension(FileField.FileName)
strExtension = System.IO.Path.GetExtension(FileField.FileName)
Dim fullname As String = myfilename + strTimeStamp + strExtension
Try
FileField.PostedFile.SaveAs(Server.MapPath("~/blog/" + fullname))
Span1.InnerHtml = "File upload successful."
Catch ex As Exception
End Try
 
0
 
LVL 3

Expert Comment

by:jbeasle3
ID: 24760711
yep. either way works, except I like the 24-hour date format (HH) as opposed to the 12 (hh).
0
 

Author Closing Comment

by:jtuttle99
ID: 31599040
Thank you, it worked!
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

746 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

10 Experts available now in Live!

Get 1:1 Help Now