Trying to rename a file on our site, not working

Discussion in 'FTP' started by Jackxxx, Jan 14, 2014.

  1. I'm trying to rename the DB backup file and add a datetime stamp to it. When I open the aspx page on my local PC I don't get any errors, but the file name is not changed. Can anyone see what I'm doing incorrectly?

    I changed the credentials and file names here in the code, just keep things anonymous.

    Thank you,

    Code:
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
           
        'Dim ftpServerIP As String = "x.x.x.x"
            Dim ftpServerIP As String = "ftp.Domain.net"
            Dim ftpServerSubFolder As String = "/App_Data/"
    
            RenameFileName(ftpServerIP & ftpServerSubFolder & "DB_12345_backup.bak", "DB_12345_backup" & System.DateTime.UtcNow & ".bak")
    
    
        End Sub
        Private Sub RenameFileName(ByVal currentFilename As String, ByVal newFilename As String)
            Dim ftpUserID As String = "MyUserName"
            Dim ftpPassword As String = "MyPassword"
    
            Dim reqFTP As FtpWebRequest = Nothing
            Dim ftpStream As Stream = Nothing
    
            Try
                reqFTP = DirectCast(FtpWebRequest.Create(New Uri(currentFilename)), FtpWebRequest)
                reqFTP.Method = WebRequestMethods.Ftp.Rename
                reqFTP.RenameTo = newFilename
                reqFTP.UseBinary = True
                reqFTP.KeepAlive = False
                reqFTP.Credentials = New NetworkCredential(ftpUserID, ftpPassword)
    
                Dim response As FtpWebResponse = DirectCast(reqFTP.GetResponse(), FtpWebResponse)
                ftpStream = response.GetResponseStream()
                ftpStream.Close()
                response.Close()
            Catch ex As Exception
                If ftpStream IsNot Nothing Then
                    ftpStream.Close()
                    ftpStream.Dispose()
                End If
                Throw New Exception(ex.Message.ToString())
            End Try
    
        End Sub
    
     
  2. Elshadriel

    Elshadriel Winhost Staff

    Maybe the path is wrong. Try to output the path string using a Response.Write so that you can review it.
     
  3. I set my aspx page autoeventwireup to true and now I get this error "Invalid URI: The format of the URI could not be determined."

    Can anyone help?
     
  4. I'm trying it a new way and I no longer get any errors, but it does not work. Here is my new code, can anyone help. I'm beginning to think this is an issue with the Winhost server setup.

    Any help is very appreciated!

    Code:
    Dim stempPath As String = "ftp://ftp.domain.net/App_Data/"
                Dim username As String = "myusername"
                Dim password As String = "mypassword"
                '# move file to remote server
                Try
                    Label1.Text = stempPath.ToString
    
                    Dim ftpReq As FtpWebRequest = CType(WebRequest.Create((stempPath & "" & "DB_12345_backup.bak")), FtpWebRequest)
                    ftpReq.Credentials = New NetworkCredential(username, password)
                    ftpReq.Method = WebRequestMethods.Ftp.Rename
                    ftpReq.RenameTo = "DB_12345_backup" & System.DateTime.UtcNow & ".bak" ' this will move the file in same folder
                    Dim ftpResp As FtpWebResponse = CType(ftpReq.GetResponse, FtpWebResponse)
    
                Catch ex As WebException
                    Dim status As String = CType(ex.Response, FtpWebResponse).StatusDescription
                End Try
     
  5. We have resolved this issue. It was not a Winhost issue, we did not have the uri setup correctly.

    Thank you,
     
    Last edited by a moderator: Oct 14, 2015
    ComputerMan, Michael and Elshadriel like this.

Share This Page