Our Products:   CompleteFTP  edtFTPnet/Free  edtFTPnet/PRO  edtFTPj/Free  edtFTPj/PRO
0 votes
3.4k views
in .NET FTP by (520 points)
I'm trying to put myself into a subdirectory before transfer based on whether it exists or not. Here is my sample code:

// First check to see if the directory we need already exists
if (!ftpConnection.ChangeWorkingDirectory(strServerDirectory + "/" + strBatchDestDir))
{
// Directory does not exist, try one level up >>>>>>>>>>>>>>>>>THIS CODE won't execute because it directly goes to the exception below....
ftpConnection.ChangeWorkingDirectory( strServerDirectory + "/" + strBatchDestDir.Substring(0, 8 ));
}
}
catch (FTPException ftpException)
{
// First test to make sure error is just that directory doesn't exist
if (ftpException.ReplyCode == 550)
{
try
{

I can't seem to figure out how to just test and see if a directory exists or not. Every time I do a check to see if the dir exists it throws an exception. That is fine but since I may have to create multiple subdirectories the try catch logic gets ugly. Is there any way to test for an existing directory without it throwing an exception?

thanks

4 Answers

0 votes
by (162k points)
You can use GetFileInfos()
0 votes
by (520 points)
You can use GetFileInfos()


Okay so if I do this:

// First check to see if the top level directory we need already exists,
FTPFile[] ftpFile = ftpConnection.GetFileInfos(strServerDirectory + "/" + strBatchDestDir.Substring(0, 8 ));

// If it does not exist then create a new directory here
if (ftpFile.Count() <= 0)
ftpConnection.CreateDirectory(strServerDirectory + "/" + strBatchDestDir.Substring(0, 8 ));

I can check the file count to make sure that no files exists but if I'm just testing for an 'empty' directory this logic fails. I create the directory with no files. If I do the code above again I still have a count of zero . When I try to create the same directory, a second time, it falls into the try catch. I'm trying to just test for an empty directory without getting an exception. Anything else I can do?

thanks a lot.
0 votes
by (162k points)
Use GetFileInfos() on the *parent* directory to see if the name of the subdirectory you are looking for is listed.
0 votes
by (520 points)
Thanks Bruce that worked fine. For a future release it would be nice to have a 'DirectoryExists' method which returns true or false. My test ftp server has a lot of directories to traverse through to figure out if one exists or not. Would be a nice performance boost not to have to do that.

regards,

Jim

Categories

...