Renaming existing files (how to)

Last post 03-13-2012, 4:23 PM by Kenneth. 1 replies.
Sort Posts: Previous Next
  •  12-19-2011, 3:31 PM 72358

    Renaming existing files (how to)

    Hi there

    I have been testing ASP Uploader and found that it's a very good program and the easiest out of the dozen or so I've tried of the Flash series.

    One issue that I came across was that the program allows you to overwrite existing files without renaming them. For example if a file named "1.jpg" exists, and you upload another file named "1.jpg" it will remove the original one. The preferred method might be to rename the new file to something like 1(1).jpg, 1(2).jpg, 1(3).jpg and so forth...

    After reviewing the ASP Uploader documentation, the code below is shown for renaming uploaded files:

    <%@ Language="VBScript" %>   
    <!-- #include file="aspuploader/include_aspuploader.asp" -->  
      
    <%   
        Dim uploader, mvcfile   
        Set uploader=new AspUploader   
        Set mvcfile=uploader.GetValidatingFile()   
        If mvcfile.FileName = "some.bmp" then   
            uploader.WriteValidationError("My custom error : Invalid file name. ")   
            Response.End  
        End if   
           
        targetfilepath= "savefiles/myprefix_" & mvcfile.FileName   
           
        dim fs   
        Set fs=Server.CreateObject("Scripting.FileSystemObject")   
        if fs.FileExists(server.mappath(targetfilepath)) then   
          fs.DeleteFile(server.mappath(targetfilepath))   
        end if   
        set fs=nothing   
           
        mvcfile.MoveTo( server.mappath(targetfilepath))   
      
        uploader.WriteValidationOK()   
    %>  

    After looking at this code, it appears that it will only delete existing files, not actually rename them.

    I've changed the code so that it will insert two random letters into the filename (676 possibilities) if the original filename exists. Thought I'd share this code with you in case you wanted to avoid overwriting files.

    <%@ Language="VBScript" %>   
    <!-- #include file="aspuploader/include_aspuploader.asp" -->  
      
    <%   
        Dim uploader, mvcfile   
        Set uploader=new AspUploader   
        Set mvcfile=uploader.GetValidatingFile()   
        If mvcfile.FileName = "some.bmp" then   
            uploader.WriteValidationError("My custom error : Invalid file name. ")   
            Response.End  
        End if   
           
        targetfilepath= "savefiles/myprefix_" & mvcfile.FileName   
           
        dim fs   
        Set fs=Server.CreateObject("Scripting.FileSystemObject")   
        if fs.FileExists(server.mappath(targetfilepath)) then   

     ' add two random  characters to filename
     randomize
     random_number1 = Int(26 * Rnd + 97)
     random_number2 = Int(26 * Rnd + 97)
     fname = Chr(random_number1) + Chr(random_number2)
            targetfilepath= "savefiles/myprefix_(" & fname & ")" & mvcfile.FileName   
        end if   
        set fs=nothing   
           
        mvcfile.MoveTo( server.mappath(targetfilepath))   
      
        uploader.WriteValidationOK()   
    %>  



     

  •  03-13-2012, 4:23 PM 73436 in reply to 72358

    Re: Renaming existing files (how to)

    Hi oap,
     
    Thank you for sharing your experience.
     
    Regards,
     
    Ken 
View as RSS news feed in XML