Error message "500 Invalid command" when checking for FileExists

0 votes
asked Jan 30, 2014 by Uwe Keim (1,070 points)
edited Jan 31, 2014

Here is my log dump:

[FTP] [State changed] - old state 'Disconnected', new state 'Connecting'.
[(S)FTP] [Log write] - level 'Info', object type 'Rebex.Net.Ftp', object ID '9', area 'Info', message 'Connecting to bmw-motorradclub.ch:21 using Ftp 4.0.5085.0.'.
[(S)FTP] [Log write] - level 'Info', object type 'Rebex.Net.Ftp', object ID '9', area 'Info', message 'Using proxy none.'.
[(S)FTP] [Log write] - level 'Debug', object type 'Rebex.Net.Ftp', object ID '9', area 'Info', message 'Connection succeeded.'.
[FTP] [State changed] - old state 'Connecting', new state 'Reading'.
[(S)FTP] [Log write] - level 'Verbose', object type 'Rebex.Net.Ftp', object ID '9', area 'Info', message 'Received data over control connection: ', buffer 'System.Byte[]', offset '37', length '37'.
[(S)FTP] [Log write] - level 'Info', object type 'Rebex.Net.Ftp', object ID '9', area 'Response', message '220 Welcome, transfer your data now'.
[FTP] [Response read] - response '220 Welcome, transfer your data now'.
[FTP] [State changed] - old state 'Reading', new state 'Ready'.
220 Welcome, transfer your data now

[FTP] [State changed] - old state 'Ready', new state 'Sending'.
[(S)FTP] [Log write] - level 'Verbose', object type 'Rebex.Net.Ftp', object ID '9', area 'Info', message 'Sent data over control connection: ', buffer 'System.Byte[]', offset '0', length '13'.
[(S)FTP] [Log write] - level 'Info', object type 'Rebex.Net.Ftp', object ID '9', area 'Command', message 'USER 101663'.
[FTP] [Command sent] - command 'USER 101663'.
[FTP] [State changed] - old state 'Sending', new state 'Reading'.
[(S)FTP] [Log write] - level 'Verbose', object type 'Rebex.Net.Ftp', object ID '9', area 'Info', message 'Received data over control connection: ', buffer 'System.Byte[]', offset '34', length '34'.
[(S)FTP] [Log write] - level 'Info', object type 'Rebex.Net.Ftp', object ID '9', area 'Response', message '331 Password required for 101663'.
[FTP] [Response read] - response '331 Password required for 101663'.
[FTP] [State changed] - old state 'Reading', new state 'Processing'.
[FTP] [State changed] - old state 'Processing', new state 'Sending'.
[(S)FTP] [Log write] - level 'Verbose', object type 'Rebex.Net.Ftp', object ID '9', area 'Info', message 'Sent data over control connection: ', buffer 'System.Byte[]', offset '0', length '17'.
[(S)FTP] [Log write] - level 'Info', object type 'Rebex.Net.Ftp', object ID '9', area 'Command', message 'PASS **********'.
[FTP] [Command sent] - command 'PASS **********'.
[FTP] [State changed] - old state 'Sending', new state 'Reading'.
[(S)FTP] [Log write] - level 'Verbose', object type 'Rebex.Net.Ftp', object ID '9', area 'Info', message 'Received data over control connection: ', buffer 'System.Byte[]', offset '15', length '15'.
[(S)FTP] [Log write] - level 'Info', object type 'Rebex.Net.Ftp', object ID '9', area 'Response', message '230 Logged on'.
[FTP] [Response read] - response '230 Logged on'.
[FTP] [State changed] - old state 'Reading', new state 'Ready'.
[FTP] [State changed] - old state 'Ready', new state 'Sending'.
[(S)FTP] [Log write] - level 'Verbose', object type 'Rebex.Net.Ftp', object ID '9', area 'Info', message 'Sent data over control connection: ', buffer 'System.Byte[]', offset '0', length '6'.
[(S)FTP] [Log write] - level 'Info', object type 'Rebex.Net.Ftp', object ID '9', area 'Command', message 'FEAT'.
[FTP] [Command sent] - command 'FEAT'.
[FTP] [State changed] - old state 'Sending', new state 'Reading'.
[(S)FTP] [Log write] - level 'Verbose', object type 'Rebex.Net.Ftp', object ID '9', area 'Info', message 'Received data over control connection: ', buffer 'System.Byte[]', offset '22', length '22'.
[(S)FTP] [Log write] - level 'Info', object type 'Rebex.Net.Ftp', object ID '9', area 'Response', message '500 Invalid command.'.
[FTP] [Response read] - response '500 Invalid command.'.
[FTP] [State changed] - old state 'Reading', new state 'Ready'.
230 Logged on

[FTP] [State changed] - old state 'Ready', new state 'Sending'.
[(S)FTP] [Log write] - level 'Verbose', object type 'Rebex.Net.Ftp', object ID '9', area 'Info', message 'Sent data over control connection: ', buffer 'System.Byte[]', offset '0', length '8'.
[(S)FTP] [Log write] - level 'Info', object type 'Rebex.Net.Ftp', object ID '9', area 'Command', message 'TYPE I'.
[FTP] [Command sent] - command 'TYPE I'.
[FTP] [State changed] - old state 'Sending', new state 'Reading'.
[(S)FTP] [Log write] - level 'Verbose', object type 'Rebex.Net.Ftp', object ID '9', area 'Info', message 'Received data over control connection: ', buffer 'System.Byte[]', offset '19', length '19'.
[(S)FTP] [Log write] - level 'Info', object type 'Rebex.Net.Ftp', object ID '9', area 'Response', message '200 Type set to I'.
[FTP] [Response read] - response '200 Type set to I'.
[FTP] [State changed] - old state 'Reading', new state 'Ready'.
[FTP] [State changed] - old state 'Ready', new state 'Sending'.
[(S)FTP] [Log write] - level 'Verbose', object type 'Rebex.Net.Ftp', object ID '9', area 'Info', message 'Sent data over control connection: ', buffer 'System.Byte[]', offset '0', length '8'.
[(S)FTP] [Log write] - level 'Info', object type 'Rebex.Net.Ftp', object ID '9', area 'Command', message 'MDTM .'.
[FTP] [Command sent] - command 'MDTM .'.
[FTP] [State changed] - old state 'Sending', new state 'Reading'.
[(S)FTP] [Log write] - level 'Verbose', object type 'Rebex.Net.Ftp', object ID '9', area 'Info', message 'Received data over control connection: ', buffer 'System.Byte[]', offset '22', length '22'.
[(S)FTP] [Log write] - level 'Info', object type 'Rebex.Net.Ftp', object ID '9', area 'Response', message '500 Invalid command.'.
[FTP] [Response read] - response '500 Invalid command.'.
[FTP] [State changed] - old state 'Reading', new state 'Ready'.
[FTP] [State changed] - old state 'Ready', new state 'Sending'.
[(S)FTP] [Log write] - level 'Verbose', object type 'Rebex.Net.Ftp', object ID '9', area 'Info', message 'Sent data over control connection: ', buffer 'System.Byte[]', offset '0', length '8'.
[(S)FTP] [Log write] - level 'Info', object type 'Rebex.Net.Ftp', object ID '9', area 'Command', message 'SIZE .'.
[FTP] [Command sent] - command 'SIZE .'.
[FTP] [State changed] - old state 'Sending', new state 'Reading'.
[(S)FTP] [Log write] - level 'Verbose', object type 'Rebex.Net.Ftp', object ID '9', area 'Info', message 'Received data over control connection: ', buffer 'System.Byte[]', offset '22', length '22'.
[(S)FTP] [Log write] - level 'Info', object type 'Rebex.Net.Ftp', object ID '9', area 'Response', message '500 Invalid command.'.
[FTP] [Response read] - response '500 Invalid command.'.
[FTP] [State changed] - old state 'Reading', new state 'Ready'.
[FTP] [State changed] - old state 'Ready', new state 'Sending'.
[(S)FTP] [Log write] - level 'Verbose', object type 'Rebex.Net.Ftp', object ID '9', area 'Info', message 'Sent data over control connection: ', buffer 'System.Byte[]', offset '0', length '5'.
[(S)FTP] [Log write] - level 'Info', object type 'Rebex.Net.Ftp', object ID '9', area 'Command', message 'PWD'.
[FTP] [Command sent] - command 'PWD'.
[FTP] [State changed] - old state 'Sending', new state 'Reading'.
[(S)FTP] [Log write] - level 'Verbose', object type 'Rebex.Net.Ftp', object ID '9', area 'Info', message 'Received data over control connection: ', buffer 'System.Byte[]', offset '31', length '31'.
[(S)FTP] [Log write] - level 'Info', object type 'Rebex.Net.Ftp', object ID '9', area 'Response', message '257 "/" is current directory.'.
[FTP] [Response read] - response '257 "/" is current directory.'.
[FTP] [State changed] - old state 'Reading', new state 'Ready'.
[FTP] [State changed] - old state 'Ready', new state 'Sending'.
[(S)FTP] [Log write] - level 'Verbose', object type 'Rebex.Net.Ftp', object ID '9', area 'Info', message 'Sent data over control connection: ', buffer 'System.Byte[]', offset '0', length '26'.
[(S)FTP] [Log write] - level 'Info', object type 'Rebex.Net.Ftp', object I
Applies to: Rebex FTP/SSL

2 Answers

+1 vote
answered Jan 31, 2014 by Tomas Knopp (58,890 points)
selected Mar 14, 2018 by Uwe Keim
 
Best answer

The standard FTP protocol does not have a dedicated command that could be easily used to detect whether a file exists. There is the MLST command that is quite reliable, but not all server support it and we have to rely on SIZE or even PWD, CWD commands for these servers.

Your server does not support SIZE command so the FileExists method fails as well.

there are 2 possible workarounds

  • STAT command:

A workaround that may work with your server is to use the little-known variant of "STAT" command that takes a path as an argument - many FTP servers will respond with listing and it's fast because there is no need to establish data connection. If you would like to give this a try, use Ftp object's SendCommand("STAT " + filename) method to send the command and ReadResponse method to read its response.

  • NLST command It is a slow data-transfer command that needs to download the full listing in order to be able to detect whether a file is there (another problem is that some servers list files and directories in NLST list, while others only list files).

Actually, you can invoke a NLST with file name with Rebex FTP simply by passing the filename to GetNameList method. However, this is still a non-trivial operation because data connection has to be esablished for it as well.

This means that rolling your own function is the best solution - it might try calling FileExists first and fall back to STAT command or GetNameList method (=NLST command) if FileExists fails.

commented Jan 31, 2014 by Uwe Keim (1,070 points)
edited Jan 31, 2014

Thank you, Tomas!

+1 vote
answered Jan 31, 2014 by Lukas Pokorny (123,810 points)
edited Jan 31, 2014

FileExists method doesn't work because the FTP server doesn't support MLST, SIZE or MDTM commands. Unfortunately, the FTP protocol doesn't provide any other fast and reliable way to check for file existence.

The workarounds mentioned by Tomas might work, but the most reliable way is to do what you do with FileZilla - simply retrieve a list of files using the GetList method (or GetNameList) and check whether the file you are looking for is there:

    FtpItemCollection list = ftp.GetList();
    bool exists = (list["myfile.txt"] != null);
commented Jan 31, 2014 by Uwe Keim (1,070 points)
edited Jan 31, 2014

Thanks a lot, Lukas, I'll do that.

...