Solved

Increment file name if it exists on file upload

Posted on 2012-04-05
3
694 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

726 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