?
Solved

Increment file name if it exists on file upload

Posted on 2012-04-05
3
Medium Priority
?
696 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 2000 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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses
Course of the Month14 days, 22 hours left to enroll

770 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