How can i check whether a file exists? - WeOnlyDo Discussion board

How can i check whether a file exists? (General questions)

by david.pearson@gmsl.co.uk, Monday, March 19, 2012, 14:28 (4429 days ago)

Our application uploads files using FtpDLX.Net.

We have a requirement to not overwrite files if they are already present.

How can we check for the existence of a file?

I do not want to have to list the dir contents.

Re: How can i check whether a file exists?

by wodDamir, Monday, March 19, 2012, 14:33 (4429 days ago) @ david.pearson@gmsl.co.uk

David,

Why don't you simply set Resume property to True? If server supports file transfer resuming, and component finds an exact file on server, it will not transfer it.

Other than that, you can simply call any other method (i.e. GetAttributes) on the file you wish to check existancy for. If file doesn't exist, you'll be given an error.

Hope this helps.

Regards,
Damba

Re: How can i check whether a file exists?

by david.pearson@gmsl.co.uk, Monday, March 19, 2012, 14:40 (4429 days ago) @ wodDamir

David,

Why don't you simply set Resume property to True? If server supports file transfer resuming, and component finds an exact file on server, it will not transfer it.

Other than that, you can simply call any other method (i.e. GetAttributes) on the file you wish to check existancy for. If file doesn't exist, you'll be given an error.

Hope this helps.

Regards,
Damba

Thanks. I'll try that and report back.

Re: How can i check whether a file exists?

by david.pearson@gmsl.co.uk, Monday, March 19, 2012, 14:55 (4429 days ago) @ david.pearson@gmsl.co.uk

GetAttributes does not raise an error if i call it with a file path specifying a file that does not exist.

Re: How can i check whether a file exists?

by wodDamir, Monday, March 19, 2012, 15:05 (4429 days ago) @ david.pearson@gmsl.co.uk

David,

Are you using blocking or non-blocking mode?

You could also use CheckDir or Rename (and specify both parameters to be same) methods.

Regards,
Damba

Re: How can i check whether a file exists?

by david.pearson@gmsl.co.uk, Monday, March 19, 2012, 15:10 (4429 days ago) @ wodDamir

David,

Are you using blocking or non-blocking mode?

You could also use CheckDir or Rename (and specify both parameters to be same) methods.

Regards,
Damba

The blocking property = True.

I've added an AttributesDelegate method and passed this to the component. Even when i call GetAttributes and specify a file that does not exist, it still fires the event! No errors.

These components are difficult to work with.

Re: How can i check whether a file exists?

by david.pearson@gmsl.co.uk, Monday, March 19, 2012, 15:19 (4429 days ago) @ david.pearson@gmsl.co.uk

David,

Are you using blocking or non-blocking mode?

You could also use CheckDir or Rename (and specify both parameters to be same) methods.

Regards,
Damba

What does CheckDir do? There is no documentation.

Re: How can i check whether a file exists?

by david.pearson@gmsl.co.uk, Monday, March 19, 2012, 15:26 (4429 days ago) @ david.pearson@gmsl.co.uk

David,

Are you using blocking or non-blocking mode?

You could also use CheckDir or Rename (and specify both parameters to be same) methods.

Regards,
Damba

CheckDir does not do what i want - it appears to require a directory name rather than a file name.

My test FTP server is returning file or directory not found when i call GetAttributes, so that looks like it could do what i want, but it never errors.

Re: How can i check whether a file exists?

by wodDamir, Monday, March 19, 2012, 15:49 (4429 days ago) @ david.pearson@gmsl.co.uk

David,

You didn't mention that you're using Blocking mode in your initial post, so I assumed you were using asynchronous mode.

However, this works perfectly:
[code]
String fullRemotePath = /path/to/file/to/check ;
Ftp1.Rename(fullRemotePath , fullRemotePath );
[/code]

If file doesn't exist, an exception will be thrown immediately, and if it does, it will remain same sa it was, unchanged.

Please note that there is no command in FTP that does this. The above is only a workaround on how to achieve something that isn't implemented into the protocol itself.

As for CheckDir method, you can find help on it here.

Regards,
Damba

Re: How can i check whether a file exists?

by david.pearson@gmsl.co.uk, Monday, March 19, 2012, 15:55 (4429 days ago) @ wodDamir

David,

You didn't mention that you're using Blocking mode in your initial post, so I assumed you were using asynchronous mode.

However, this works perfectly:
[code]
String fullRemotePath = /path/to/file/to/check ;
Ftp1.Rename(fullRemotePath , fullRemotePath );
[/code]

If file doesn't exist, an exception will be thrown immediately, and if it does, it will remain same sa it was, unchanged.

Please note that there is no command in FTP that does this. The above is only a workaround on how to achieve something that isn't implemented into the protocol itself.

As for CheckDir method, you can find help on it here.

Regards,
Damba

Many thanks. My solution is to use an attributes delegate method, and store the results of that in a variable. My code calls GetAttributes and checks the contents of the variable to compare the name of the file to determine whether it exists.

I'm not sure i like idea of renaming a file (i understand what you're proposing though).