Solved

Increment file name if it exists on file upload

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

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Reset asp.net login password 4 42
Webform example, External Authentication Services, OAUTH, asp.net 4.6.2 1 33
Code works but it's slow 28 70
No Data for DropDown List 2 26
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…
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 …
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

810 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