|
-
May 21st, 2010, 03:01 PM
#1
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"
-
May 21st, 2010, 10:16 PM
#2
Thread Starter
Member
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
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|