Solved

Increment file name if it exists on file upload

Posted on 2012-04-05
3
695 Views
Last Modified: 2012-06-21
Hi

I am creating a video uploading website. I would like to avoid having files overwitten when they have the same file name. is there a way to automatically increment the filename if it exists?

My current code is just uploading:

Thanks

'declare the file path to store uploads
        Dim path As String = Server.MapPath("~/UploadedVideos/" & UserLogin.UserNo & "/")

        FileUploadVideo.PostedFile.SaveAs(path & FileUploadVideo.FileName)
0
Comment
Question by:ntd1
[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
  • 2
3 Comments
 
LVL 25

Expert Comment

by:kevp75
ID: 37813959
If IO.Exists(path & FileUploadVideo.FileName) Then
    FileUploadVideo.PostedFile.SaveAs(path & FileUploadVideo.FileName + 1)
Else
    FileUploadVideo.PostedFile.SaveAs(path & FileUploadVideo.FileName)
End If

Open in new window


Personally I would generate a random string, and append it to the filename.  Lemme see if I can drum up something official :)
0
 
LVL 25

Accepted Solution

by:
kevp75 earned 500 total points
ID: 37813991
See, I usually do something like this:
                    Dim sFileName As String = GenString() & System.IO.Path.GetFileName(FileUploadVideo.FileName)

If IO.Exists(path & FileUploadVideo.FileName) Then
    FileUploadVideo.PostedFile.SaveAs(path &  sFileName)
Else
    FileUploadVideo.PostedFile.SaveAs(path & FileUploadVideo.FileName)
End If
                                            

    Private Function GenString(Optional ByVal _MinLength As Integer = 4, Optional ByVal _MaxLength As Integer = 15) As String
        Dim _RandBytes(10) As Byte, _RetStr As String = String.Empty
        Dim _Seed As Integer = CType(Date.Now.Ticks And Integer.MaxValue, Integer)
        Dim _Rand As Random = New Random(_Seed)
        _Rand.NextBytes(_RandBytes)
        _RetStr = Convert.ToBase64String(_RandBytes).Substring(0, _Rand.Next(_MinLength, _MaxLength))
        _RetStr = _RetStr.Replace("+", String.Empty)
        _RetStr = _RetStr.Replace("/", String.Empty)
        _RetStr = _RetStr.Replace("=", String.Empty)
        Return _RetStr
        _Rand = Nothing : _Seed = 0 : Erase _RandBytes : _RetStr = String.Empty
    End Function

Open in new window

0
 

Author Closing Comment

by:ntd1
ID: 37814191
Very Good.
0

Featured Post

Enroll in June's Course of the Month

June's Course of the Month is now available! Every 10 seconds, a consumer gets hit with ransomware. Refresh your knowledge of ransomware best practices by enrolling in this month's complimentary course for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

688 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