SFML community forums

Help => Network => Topic started by: Niely on December 20, 2014, 08:45:14 pm

Title: Check if file exists on FTP-server
Post by: Niely on December 20, 2014, 08:45:14 pm
Hello

I need to somehow check if a file exists on a FTP server.
Is there a special function for this? Or can it be done through the download function?

That for example, if a file doesn't exist, it'll throw an error-code out?
If I can figure this out, the basic of my project is somehow ready.  ;D

Thanks for reading,
Niely
Title: Re: Check if file exists on FTP-server
Post by: Laurent on December 20, 2014, 09:29:46 pm
This is more related to the FTP protocol than to SFML. And Google has plenty of answers to this question, I quickly checked the 3 first links and they were all relevant.

Regarding SFML, don't forget that you can use any non-implemented FTP command with the sf::Ftp::sendCommand function. Therefore, SFML should not block you if you find a way to do what you want in the FTP protocol.
Title: Re: Check if file exists on FTP-server
Post by: Hapax on December 20, 2014, 09:32:10 pm
Couldn't this be done by getting the directory listing (http://www.sfml-dev.org/documentation/2.2/classsf_1_1Ftp.php#a8f37258e461fcb9e2a0655e9df0be4a0)?
Title: Re: Check if file exists on FTP-server
Post by: Niely on December 20, 2014, 10:16:25 pm
Didn't expected answers so fast. :)

This is more related to the FTP protocol than to SFML. And Google has plenty of answers to this question, I quickly checked the 3 first links and they were all relevant.

Regarding SFML, don't forget that you can use any non-implemented FTP command with the sf::Ftp::sendCommand function. Therefore, SFML should not block you if you find a way to do what you want in the FTP protocol.

Really? Didn't knew that, will take a look on that as first.

If it doesn't work, I shall try DirectoryListing. :)
Title: Re: Check if file exists on FTP-server
Post by: Niely on December 21, 2014, 10:17:17 pm
I tried a few methods, but none actually worked.
The DirectoryListing code also didn't worked + I actually don't understand it.

Someone another hint?
Title: Re: Check if file exists on FTP-server
Post by: Ixrec on December 22, 2014, 12:16:40 am
Could you show us what specifically didn't work?  And what do you mean by "didn't work"?

Like Laurent said, you can send any FTP command with the sendCommand function.  So in all likelihood either you or the server you're talking to is misunderstanding the protocol.  Are you even sure there is an FTP server set up on the machine you're talking to?
Title: Re: Check if file exists on FTP-server
Post by: Niely on December 22, 2014, 05:07:11 pm
I tried this:
Response sf::Ftp::sendCommand(const string & ls "file.txt");
Response::getMessage();
 

Based on this article:
http://www.sfml-dev.org/documentation/2.2-fr/classsf_1_1Ftp.php

However, the code itself doesn't really sound logic in my ears + it just doesn't work.

This is the g++ output:
receive_chat.cpp: In function ‘int main()’:
receive_chat.cpp:18:1: error: ‘Response’ was not declared in this scope
 Response sf::Ftp::sendCommand(const string & ls "file.txt");
 ^
receive_chat.cpp:18:10: error: expected ‘;’ before ‘sf’
 Response sf::Ftp::sendCommand(const string & ls "file.txt");
          ^
receive_chat.cpp:19:1: error: ‘Response’ is not a class, namespace, or enumeration
 Response::getMessage();
 ^

What is the right code?
Title: Re: Check if file exists on FTP-server
Post by: Hapax on December 22, 2014, 07:44:30 pm
Not sure where you got that code from but it doesn't seem to be from that article that you linked...

Quote
// Create a new FTP client
sf::Ftp ftp;
...
// Connect to the server
sf::Ftp::Response response = ftp.connect("ftp://ftp.myserver.com");
if (response.isOk())
    std::cout << "Connected" << std::endl;
...
// Send specific commands (here: FEAT to list supported FTP features)
response = ftp.sendCommand("FEAT");
if (response.isOk())
    std::cout << "Feature list:\n" << response.getMessage() << std::endl;
Title: Re: Check if file exists on FTP-server
Post by: Ixrec on December 22, 2014, 07:48:18 pm
@Niely: That code isn't even close to syntactically correct C++, much less a correct usage of the SFML API.

My guess is you were basing it on this:
Quote
Response sf::Ftp::sendCommand   (   const std::string &    command, const std::string &    parameter = "" )      
Send a command to the FTP server.

While the most often used commands are provided as member functions in the sf::Ftp class, this method can be used to send any FTP command to the server. If the command requires one or more parameters, they can be specified in parameter. If the server returns information, you can extract it from the response using Response::getMessage().
In other words, using the function/method signatures as if they were actual function calls.  Hapax already demonstrated the correct syntax for calling these functions.

You really need to learn C++ properly before trying to use libraries written in C++, preferably using a good book about the language (https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list).
Title: Re: Check if file exists on FTP-server
Post by: Niely on December 22, 2014, 09:48:15 pm
^I already know C++ well and already made some pretty neat projects with it.
This is just my first time using an extra library and some parts of the SFML help-doc are not that logically.

I already had the FTP connection thing etc, only didn't understand the sendcommand part; now I do.

Thanks a lot all!
Title: Re: Check if file exists on FTP-server
Post by: Gambit on December 23, 2014, 12:05:28 am
This is just my first time using an extra library and some parts of the SFML help-doc are not that logically.

You are kidding right? There is nothing wrong with the documentation if you read it properly and if you think that something doesnt make sense, you are more than welcome to contribute.
Title: Re: Check if file exists on FTP-server
Post by: Niely on January 01, 2015, 01:14:40 pm
I did achieved this by using the rename function, and if the rename function throws an error back the file doesn't exist. :)
Title: Re: Check if file exists on FTP-server
Post by: Ixrec on January 01, 2015, 03:36:43 pm
This is just my first time using an extra library and some parts of the SFML help-doc are not that logically.

You are kidding right? There is nothing wrong with the documentation if you read it properly and if you think that something doesnt make sense, you are more than welcome to contribute.

That tone doesn't really encourage him to tell us what was confusing him.

@Niely: When you said parts of the SFML docs are "not that helpful", did you have anything specific in mind?
Title: Re: Check if file exists on FTP-server
Post by: Niely on January 01, 2015, 03:51:42 pm
The SFML help-docs are really helpful.
Better then most C++ documentations I've seen.

Only this code:
sf::Ftp::ListingResponse response = ftp.getDirectoryListing();
if (response.isOk())
{
    const std::vector<std::string>& listing = response.getListing();
    for (std::vector<std::string>::const_iterator it = listing.begin(); it != listing.end(); ++it)
        std::cout << "- " << *it << std::endl;
}

// you can also get the listing of a sub-directory of the current directory:
response = ftp.getDirectoryListing("subfolder");

On this page:
http://www.sfml-dev.org/tutorials/2.2/network-ftp.php

Isn't clear. Because it isn't really an example but more a wall of characters.
I personally like documentations more which really shows examples of how to use it, instead of using the type of variable. :)

Could also be laying at me.
Title: Re: Check if file exists on FTP-server
Post by: Ixrec on January 01, 2015, 04:04:13 pm
That's definitely an example of how to use getDirectoryListing so I'm a bit confused.  Are you trying to say the loop for printing out the std::vector<std::string> should be left out, because that's not really necessary once you know the type returned by getListing()?  As in:

Quote
Getting the list of directories and files contained in the current directory:
sf::Ftp::ListingResponse response = ftp.getDirectoryListing();
if (response.isOk()) {
    const std::vector<std::string>& listing = response.getListing();
}

You can also get the listing of a sub-directory of the current directory:
response = ftp.getDirectoryListing("subfolder");
Title: Re: Check if file exists on FTP-server
Post by: Laurent on January 01, 2015, 08:00:01 pm
This piece of code retrieves the contents of the directory and prints it on the standard output. I don't know what it is if not an example ;)
Title: Re: Check if file exists on FTP-server
Post by: Niely on January 01, 2015, 08:24:03 pm
Yeah, but previous times it didn't work. Really weird..
Title: Re: Check if file exists on FTP-server
Post by: Jesper Juhl on January 01, 2015, 11:09:33 pm
I did achieved this by using the rename function, and if the rename function throws an error back the file doesn't exist. :)
Ouch.

You really can't make that conclusion. The rename function may have failed (throws an error back) because you simply are not allowed to rename a file - that is, the file does exist but the FTP server does not allow you to rename it. Could also be that the file exists but the name you are trying to rename it to is not valid. You simply cannot conclude that a file does not exist just based on rename failing. Also, if rename succeeds you have now renamed the file - are you sure you want to do that?
This seems to me like a very wrong way to go about this.
Why don't you just:
 a) request a directory listing and see if the file is in the list.
 b) try to RETRieve the file and check if that succeeds.
 c) ask for the SIZE (http://tools.ietf.org/html/rfc3659#section-4) of the file and check the returned value.

See also RFC 959 (https://www.ietf.org/rfc/rfc959.txt).