Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Check if file exists on FTP-server  (Read 9927 times)

0 Members and 1 Guest are viewing this topic.

Niely

  • Full Member
  • ***
  • Posts: 101
    • View Profile
Check if file exists on FTP-server
« 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

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Check if file exists on FTP-server
« Reply #1 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.
Laurent Gomila - SFML developer

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Check if file exists on FTP-server
« Reply #2 on: December 20, 2014, 09:32:10 pm »
Couldn't this be done by getting the directory listing?
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Niely

  • Full Member
  • ***
  • Posts: 101
    • View Profile
Re: Check if file exists on FTP-server
« Reply #3 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. :)

Niely

  • Full Member
  • ***
  • Posts: 101
    • View Profile
Re: Check if file exists on FTP-server
« Reply #4 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?

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: Check if file exists on FTP-server
« Reply #5 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?

Niely

  • Full Member
  • ***
  • Posts: 101
    • View Profile
Re: Check if file exists on FTP-server
« Reply #6 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?

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Check if file exists on FTP-server
« Reply #7 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;
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: Check if file exists on FTP-server
« Reply #8 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.

Niely

  • Full Member
  • ***
  • Posts: 101
    • View Profile
Re: Check if file exists on FTP-server
« Reply #9 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!

Gambit

  • Sr. Member
  • ****
  • Posts: 283
    • View Profile
Re: Check if file exists on FTP-server
« Reply #10 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.

Niely

  • Full Member
  • ***
  • Posts: 101
    • View Profile
Re: Check if file exists on FTP-server
« Reply #11 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. :)

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: Check if file exists on FTP-server
« Reply #12 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?

Niely

  • Full Member
  • ***
  • Posts: 101
    • View Profile
Re: Check if file exists on FTP-server
« Reply #13 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.

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: Check if file exists on FTP-server
« Reply #14 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");

 

anything