Date stamped and numbered filenames when saving to jpeg?

Author Message
SamIAm

  • Total Posts : 3
  • Scores: 0
  • Reward points : 0
  • Joined: 10/16/2009
  • Status: offline
Date stamped and numbered filenames when saving to jpeg? Monday, October 19, 2009 2:45 AM (permalink)
0
Right now the existing VB code I have uses GUID to generate "random" filenames on save. 

For instance: 

ff8514d389a041649fd8b925a32f2b51.jpg 
e1c938560fc1464ab2138065fc4661d3.jpg 


Instead I want a date stamp and numbered filename: 

for instance: 

20091015_131644_001.jpg     <--(Date_Time_NumberIncrement) 
20091015_131551_002.jpg 

Or something along those lines... 

The current code (which saves a jpeg to a random GUID filename) is below: 

///////////////////////////////////////////////////////////// 
Imports System.Web 
Imports System.Web.Services 
Imports System.Web.Services.Protocols 
Imports System.IO 

' To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
' <System.Web.Script.Services.ScriptService()> _ 
<WebService(Namespace:="http://tempuri.org/")> _ 
<WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _ 
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _ 
Public Class Service 
     Inherits System.Web.Services.WebService 


    <WebMethod()> _ 
    Public Function SaveJPEG(ByVal strJPEG As String) As String 

        'saves an uploaded photo and returns a unique name reference 

        Dim BinaryContent As Byte() = Convert.FromBase64String(strJPEG) 

        Try 
            'generate a unique name (GUID) for the filename 
            Dim strFileName As String = System.Guid.NewGuid.ToString("n") 
            ' 
             
            Dim fStream As New FileStream("D:\photos\" + strFileName + ".jpg", FileMode.CreateNew) 

            Dim bw As New BinaryWriter(fStream) 

            bw.Write(BinaryContent) 
            bw.Close() 
            fStream.Close() 

            'return the filename for use in Flash 
            Return strFileName 

        Catch ex As Exception 

            Return ex.Message 

        End Try 

    End Function 

End Class 
 
#1
    ginolard

    • Total Posts : 1347
    • Scores: 23
    • Reward points : 0
    • Joined: 8/11/2005
    • Status: offline
    Re:Date stamped and numbered filenames when saving to jpeg? Monday, October 19, 2009 5:54 PM (permalink)
    0
    This is pretty straightforward but, as is the rule on these forums, I shall merely point you in the right direction rather than writing the code for you ;)

    Firstly, you need the date & time.  This can be easily obtained from the defult variable Now().  You can then format it how you wish.

    Now, for the incremental number part I have a simple question.  Should this number be incremented every time the application runs?  By that I mean, if the last file to have been created was, say, 20091015_131551_058.jpg, should the next filename end with 059.jpg?  So, it's a constantly incrementing number?  I'm guessing so.

    If so, I would suggest one of several approaches. 

    1) Presumably the incremental suffix of the filename will be equal to amount of files in the target directory.  If so, you could simply get the number of files in that directory and add 1 (My.Computer.FileSystem should help you out there)

    2) Store the last filename created as a value in the application properties

    3) Store the last filename created in the registry somewhere

    4) Use My.Computer.Filesystem to retrieve the last filename of the target directory.  Strip out the incremental number part of the filename, convert it to an integer and add 1.


    Author of ManagePC - http://managepc.net

     
    #2

      Online Bookmarks Sharing: Share/Bookmark

      Jump to:

      Current active users

      There are 0 members and 1 guests.

      Icon Legend and Permission

      • New Messages
      • No New Messages
      • Hot Topic w/ New Messages
      • Hot Topic w/o New Messages
      • Locked w/ New Messages
      • Locked w/o New Messages
      • Read Message
      • Post New Thread
      • Reply to message
      • Post New Poll
      • Submit Vote
      • Post reward post
      • Delete my own posts
      • Delete my own threads
      • Rate post

      2000-2012 ASPPlayground.NET Forum Version 3.9