Our Products:   CompleteFTP  edtFTPnet/Free  edtFTPnet/PRO  edtFTPj/Free  edtFTPj/PRO
0 votes
2.4k views
in .NET FTP by (1.3k points)
Hi,
We transfer files into a temporary folder, and then rename it to the current build number, so to do this, we were storing the ServerDirectory in a var, changing up, and then finding the name of that folder with GetFileInfos(), as follows:

[code]
string currentDestinationPath = FtpClient.ServerDirectory;
FtpClient.ChangeWorkingDirectoryUp();
FTPFile[] allFiles = FtpClient.GetFileInfos();

int i = 0;
for (i = 0; i < allFiles.Length && (allFiles[i].Path != currentDestinationPath); i++);
string folderToRename = allFiles[i].Name;
[/code]

This works fine, except on a client's Linux server, where GetFileInfos() (for some reason) appends "//" after the name, which means that we never find the folder in our loop. I presume this is simply the server's behaviour and there's nothing we can do about it?

So we're re-writing this code and wanted to ask which was a better solution, 1) passing a path into RenameFile or 2) passing just the folder name:

1.
[code]
RenameDir(string newFolderName)
{
string currentDestinationPath = FtpClient.ServerDirectory;
FtpClient.ChangeWorkingDirectoryUp();

FtpClient.RenameFile(currentDestinationPath, FtpClient.ServerDirectory +"/"+ newFolderName);
}
[/code]
2.
[code]
RenameDir(string newFolderName)
{
string currentDestinationPath = FtpClient.ServerDirectory;
FtpClient.ChangeWorkingDirectoryUp();

FtpClient.RenameFile(currentDestinationPath, newFolderName);
}
[/code]

Or, will it effectively amount to the same thing on Linux/Unix servers?
Do you have any other suggestions as to the safest method of doing this?

Thanks,
Graeme

6 Answers

0 votes
by (161k points)
Why not just trim // from the directory names if it exists?
0 votes
by (1.3k points)
We don't really want to apply a fix for a specific issue to generic code. If another server returns some other unexpected values then we'd have to fix for that as well.

Besides, the two solutions I gave seem to be simpler/better code. The question is, are there any possible scenarios where either of them would fail, such as where the full path would be renamed to the newFolderName (in 2). In other words, if you want to rename "/home/sftp/main/0" to "/home/sftp/main/1", is it possible that it would move "/home/sftp/main/0" and create a "/1" folder off the root?

Or in 1, where calling RenameFile("/home/sftp/main/0", "/home/sftp/main/1") would not understand that only the "0" should be renamed to "1"?
0 votes
by (161k points)
As long as you use absolute paths it should be ok.

Trimming trailing / chars isn't as hacky as it sounds as this char often appears on the end of listings, is about the only char that does.
0 votes
by (1.3k points)
Yes, trimming paths is fine, but I was referring more to GetFileInfos() doing other strange things, as I read in another post that it was adding datetime stamps to the name or something. So we'd prefer not to rely on that method.

Any idea why only one server (of many) would return "/path//", whilst the others only return "/path/"?
0 votes
by (161k points)
No. Weird.
0 votes
by (1.3k points)
Yes, very! And typically, it worked fine on all the test sites of the customer, and as soon as they went live, it happened on the production server! :?

Categories

...