Check If File Exists On FTP

This is a discussion on Check If File Exists On FTP within the C++ Programming forums, part of the General Programming Boards category; I'm using libcurl to connect to my site via FTP. I want to know how to check if a file ...

  1. #1
    Registered User
    Join Date
    Aug 2004
    Posts
    193

    Check If File Exists On FTP

    I'm using libcurl to connect to my site via FTP. I want to know how to check if a file on my FTP exists or not. Or better yet, I want to know if a file I upload to my FTP overwrites an existing file or not.

    Any help is greatly appreciated. I can post the code that I have so far if necessary. Thanks.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    Send the command to do an 'ls' and study the response?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Aug 2004
    Posts
    193
    Would you mind posting an example? I know 'ls' will return the files within a directory, but how would I check if a certain file is in it. I'd prefer not to do a loop because I have hundreds of files within this directory.

  4. #4
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    Maybe you could do ls filetocheck.ext? If it returns the file, then file exists, otherwise, it doesn't.
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    > but how would I check if a certain file is in it
    What????
    You can do FTP, but you can't figure out how to do strcmp() in a loop?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User
    Join Date
    Aug 2004
    Posts
    193
    "I'd prefer not to do a loop because I have hundreds of files within this directory."

    Where would libcurl store the result of "ls filetocheck"? I think I would be able to run the command, but where do the results of the command end up?

  7. #7
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    I believe you can pipe ls output into your program directly...
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    > "I'd prefer not to do a loop because I have hundreds of files within this directory."
    Ah, premature optimisation disease.

    > Where would libcurl store the result of "ls filetocheck"?
    No idea - did you read the manual?
    I was under the impression you had done this bit.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  9. #9
    Registered User
    Join Date
    Aug 2004
    Posts
    193
    Well, I found out how to check if a file exists even though it's problem not the best way. Just call:

    Code:
    curl_easy_setopt(curl, CURLOPT_URL, "ftp://user:pass@ftp.example.com/filetocheck");
    Then, when you get the result:

    Code:
    res = curl_easy_perform(curl);
    That will report with an error (PERIOD). However, THE FIRST error that is produced is:

    CURLE_FTP_COULDNT_RETR_FILE

    The only time that error is produced is if CURL could "RETR"ieve the file, meaning the file doesnt exist if that error is produced. Otherwise, different errors are produced (like, couldn't write retrieved data to application). I really dont care what error is produced, I just want to know if CURL could "RETR"ieve the file or not. So for those who are using this as an example, this is what you do after you get the results in res:

    Code:
    if(res==CURLE_FTP_COULDNT_RETR_FILE) {
         MessageBox(0, "File Doesnt Exist code belongs here", "Result", 0);
    } else {
         MessageBox(0, "File Does Exist code belongs here", "Result", 0);
    }
    Thanks for your help (also, res wont contain the results of the command you enter; it returns the error in running the commands. So if it performs successfully, then the error is CURLE_OK, which doesnt help me).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need Help Fixing My C Program. Deals with File I/O
    By Matus in forum C Programming
    Replies: 7
    Last Post: 04-29-2008, 07:51 PM
  2. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 04:02 AM
  3. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  4. spell check in C using a dictionary file
    By goron350 in forum C Programming
    Replies: 10
    Last Post: 11-25-2004, 05:44 PM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21