Results 1 to 7 of 7

Thread: [RESOLVED] Rename Files on Server, via FTP

  1. #1

    Thread Starter
    Member
    Join Date
    Apr 2010
    Posts
    43

    Resolved [RESOLVED] Rename Files on Server, via FTP

    Hello,

    A while back I asked about going beyond basic FTP upload/download on this thread, and the response I got about using FtpWebRequest was very helpful. Now I'm looking at something that doesn't seem to be offered in FtpWebRequest: renaming a file on the server.

    My situation: several times each month I update MyConfig.csv on my website. This file is generated on my computer by Excel to control who can access which files on the site, and it runs a bit over 1 MB. The scripts on the server parse through the file to keep things running smoothly. Given that the file can take some time to transfer, I typically will upload NewMyConfig.csv to the server with a commercial FTP program, then when it is in place I rename the existing file to MyConfigBackup20100521.csv and then rename NewMyConfig.csv to MyConfig.csv. This way, if there is a problem with the upload, everything keeps running smoothly because the existing MyConfig is undisturbed, and the window when a customer might get an error from the scripts because there is no MyConfig.csv is reduced to a couple of seconds between the two rename operations.

    Now I'd like to automate all of this to happen with a VB program, but it looks like FtpWebRequest only allows you to rename directories, not individual files, on the server. What would be the best way to go about this, please?

    Many thanks,

    Herky B.

  2. #2
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: Rename Files on Server, via FTP

    According to the first result I found on google, you can simply use the RenameTo property of the FtpWebRequest. Have you tried that?

    e.g:

    Code:
    MyFtpRequest.Method = WebRequestMethods.Ftp.Rename
    MyFtpRequest.RenameTo = "New file name"
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  3. #3

    Thread Starter
    Member
    Join Date
    Apr 2010
    Posts
    43

    Re: Rename Files on Server, via FTP

    Hi Chris,

    Thanks for responding! That command shows up in the intellisense (and other places I've seen it documented) as working on a directory only, not a file. I tried implementing it as you mentioned, and it comes back without success. I'll readily acknowledge the possibility that my code is at fault there, so here's what I have:

    Code:
    Private Sub RenameFileOnServer(ByVal TheServerURL As String, ByVal OldFile As String, ByVal NewName As String)
    
            Dim MyFtpWebRequest As System.Net.FtpWebRequest = System.Net.FtpWebRequest.Create(TheServerURL & OldFile)
            MyFtpWebRequest.Credentials = New System.Net.NetworkCredential(SwapUserName, SwapPassword)
            MyFtpWebRequest.Method = System.Net.WebRequestMethods.Ftp.Rename
            MyFtpWebRequest.RenameTo() = NewName
            Dim MyResponse As System.Net.FtpWebResponse
    
            Try
                MyResponse = MyFtpWebRequest.GetResponse
    
                Dim MyStatusStr As String = MyResponse.StatusDescription
                Dim MyStatusCode As System.Net.FtpStatusCode = MyResponse.StatusCode
    
                If MyStatusCode <> Net.FtpStatusCode.FileActionOK Then
                    AddToLog("*** Rename " & OldFile & " to " & NewName & " failed.  Returned status = " & MyStatusStr)
                    QuitFtpNow()
                Else
                    AddToLog("Rename " & OldFile & " to " & NewName & " ok at " & Now.ToString)
                End If
            Catch
                AddToLog("*** Rename " & OldFile & " to " & NewName & " failed.")
                QuitFtpNow()
            End Try
        End Sub
    In the OldFile variable, I pass the directory structure (i.e. /ConfigFiles/Current/MyConfig.csv), and in the NewName I've tried passing both the same directory structure or just the new file name itself (i.e. both /ConfigFiles/Current/OldMyConfig20100521.csv and also just OldMyConfig20100521.csv) and neither one worked.

    Any thoughts?

    Thanks!

  4. #4
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: Rename Files on Server, via FTP

    Hmm yeah I see what you mean about intellisense only mentioning a directory and not a file. According to all of the sources I can find though it should work just the same for a file or a directory - I think the actual FTP protocol commands that gets sent to do a rename (RNFR and RNTO) are the same for directories or files so I'm not sure why it wouldn't work.
    Give me a few minutes and I'll do some testing against an FTP site on one of our servers at work.
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  5. #5
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: Rename Files on Server, via FTP

    OK I just tried it with your code and it worked perfectly. One thing I did notice was that you must not have Option Strict turned on as I had to add a few CType commands to cast objects to the correct type.
    EDIT: One other thing I just noticed is that I think you said you are passing in the full path for the OldFile argument - dont, it should just be the file name because you already have the rest of the path in the TheServerUrl argument. If you pass the full path in for OldFile as well then when you combine TheServerUrl and OldFile in the first line of your RenameFileOnServer method then it is going to produce the wrong path isnt it - and that may be where your code was going wrong.

    I also modified a couple of other small bits just so I could get it to run but it was only small things like changing it so it doesnt try to run AddToLog or QuitFTPNow so they should not have made any difference to anything. Give this code a try and see what happens:

    vb Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.         RenameFileOnServer("FTP://OurFtpServer/Test/", "Test.txt", "Test2.txt")
    3. End Sub
    4.  
    5. Private Sub RenameFileOnServer(ByVal TheServerURL As String, ByVal OldFile As String, ByVal NewName As String)
    6.         Dim MyFtpWebRequest As System.Net.FtpWebRequest = CType(System.Net.FtpWebRequest.Create(TheServerURL & OldFile), FtpWebRequest)
    7.         MyFtpWebRequest.Credentials = New System.Net.NetworkCredential(SwapUsername, SwapPassword)
    8.         MyFtpWebRequest.Method = System.Net.WebRequestMethods.Ftp.Rename
    9.         MyFtpWebRequest.RenameTo() = NewName
    10.         Dim MyResponse As System.Net.FtpWebResponse
    11.  
    12.         Try
    13.             MyResponse = CType(MyFtpWebRequest.GetResponse, FtpWebResponse)
    14.  
    15.             Dim MyStatusStr As String = MyResponse.StatusDescription
    16.             Dim MyStatusCode As System.Net.FtpStatusCode = MyResponse.StatusCode
    17.  
    18.             If MyStatusCode <> Net.FtpStatusCode.FileActionOK Then
    19.                 MessageBox.Show("*** Rename " & OldFile & " to " & NewName & " failed.  Returned status = " & MyStatusStr)
    20.             Else
    21.                 MessageBox.Show("Rename " & OldFile & " to " & NewName & " ok at " & Now.ToString)
    22.             End If
    23.         Catch ex As Exception
    24.             MessageBox.Show("*** Rename " & OldFile & " to " & NewName & " failed due to the following error: " & ex.Message)
    25.  
    26.         End Try
    27. End Sub

    Also as you can see I have modified your Catch block slightly so that it actually tells you what the error is instead of just saying "an error occurred"
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  6. #6

    Thread Starter
    Member
    Join Date
    Apr 2010
    Posts
    43

    Smile Re: Rename Files on Server, via FTP

    THANK YOU, Chris!

    That was exactly what I needed; I'd made an error with the directories, and that was what was throwing everything off. That code does exactly what I need, and I appreciate the catch on Option Strict. I have more time programming in Applesoft Basic than in .NET Visual Basic, so that sort of thing is exactly what I need to keep from becoming sloppy. Likewise the fix on the Catch statement!

    Major learning point, the Intellisense & some other documentation out there can be misleading; the FtpWebRequest DOES rename files as well as directories! I can pass old & new file or directory names to the sub Chris fixed (above) and get the desired result either way.

    Again, many thanks for the help -- this is a great forum with some great contributors!

    Herky Bird
    Last edited by HerkyBird; May 21st, 2010 at 10:18 PM. Reason: fix typos

  7. #7
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: [RESOLVED] Rename Files on Server, via FTP

    Glad I could help
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width