[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.
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"
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!
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.
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:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
RenameFileOnServer("FTP://OurFtpServer/Test/", "Test.txt", "Test2.txt")
End Sub
Private Sub RenameFileOnServer(ByVal TheServerURL As String, ByVal OldFile As String, ByVal NewName As String)
Dim MyFtpWebRequest As System.Net.FtpWebRequest = CType(System.Net.FtpWebRequest.Create(TheServerURL & OldFile), FtpWebRequest)
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 = CType(MyFtpWebRequest.GetResponse, FtpWebResponse)
Dim MyStatusStr As String = MyResponse.StatusDescription
Dim MyStatusCode As System.Net.FtpStatusCode = MyResponse.StatusCode
If MyStatusCode <> Net.FtpStatusCode.FileActionOK Then
MessageBox.Show("*** Rename " & OldFile & " to " & NewName & " failed. Returned status = " & MyStatusStr)
Else
MessageBox.Show("Rename " & OldFile & " to " & NewName & " ok at " & Now.ToString)
End If
Catch ex As Exception
MessageBox.Show("*** Rename " & OldFile & " to " & NewName & " failed due to the following error: " & ex.Message)
End Try
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" :)
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
Re: [RESOLVED] Rename Files on Server, via FTP