Check file existence on Server

Deploying to Windows? Utilizing VB Script execution? This is the place to ask Windows-specific questions.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

alemrantareq
Posts: 203
Joined: Wed Jul 23, 2008 8:46 am

Check file existence on Server

Post by alemrantareq » Sun Jan 20, 2013 5:27 pm

Hello everyone, i need to check a file exists or not on a server. I've tried several function & command but dont understand how to cook them properly. Can anyone teach me whats the exact command to check a server file existance?

The command will be - if the file exists, it will ask to download :)

I've found some examples in this forum; but all r ftp based, I mean to get a file I need to know username & pass. But I'm talking about downloading from a direct link. & the file is in rar format.

replies r most appreciated :)

alemrantareq
Posts: 203
Joined: Wed Jul 23, 2008 8:46 am

Re: Check file existence on Server

Post by alemrantareq » Mon Jan 21, 2013 1:15 pm

Well, I've tried myself & built a script that helps me partially :)

Code: Select all

on mouseUp
   put "http://siteurl.com/file.rar" into dFile
   put specialfolderpath(0) & "/file.rar" into tFile
   libURLDownloadToFile dFile, tFile
end mouseUp
But before it downloads the file, I need to check the file exists or not. Pls somebody help

Klaus
Posts: 14177
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Check file existence on Server

Post by Klaus » Mon Jan 21, 2013 1:32 pm

Mark already gave you the answer here:
http://forums.runrev.com/phpBB2/viewtop ... 320#p67359

"there is a file (or not) does only work with local files, so you need to ask the webserver if it can serve the file!
If NOT then an HTML 404 error is returned -> 404 = file not found -> Means "the result" is NOT empty in any case
after getting that URL. I am sure you have seen this in your browser at some time.

This is what I get after I enter a NON valid url in my browser:
------------------------------------------------------------------------------------------
Not Found

The requested URL /image.jpg was not found on this server.

Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.

Apache/2.2.23 (Unix) mod_ssl/2.2.23 OpenSSL/0.9.8e-fips-rhel5 mod_auth_passthrough/2.1 mod_bwlimited/1.4 FrontPage/5.0.2.2635 Server at http://www.major-k.de Port 80
........................................................................................

alemrantareq
Posts: 203
Joined: Wed Jul 23, 2008 8:46 am

Re: Check file existence on Server

Post by alemrantareq » Mon Jan 21, 2013 3:02 pm

Thanks Klaus. Yes I've seen such 404 error page many times in different websites. I've tested Mark's script & it works fine when there is such no 404 error handling in .htaccess file of that server. But now there are many sites using 404 handling scripts and redirect the visitors to the home page. In that case, Mark's script doesn't give the correct answer !!

Klaus
Posts: 14177
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Check file existence on Server

Post by Klaus » Mon Jan 21, 2013 3:12 pm

Ah, OK, well, good luck then :D

alemrantareq
Posts: 203
Joined: Wed Jul 23, 2008 8:46 am

Re: Check file existence on Server

Post by alemrantareq » Mon Jan 21, 2013 3:38 pm

Klaus wrote:Ah, OK, well, good luck then :D
Bro, I've no luck if I can't get the correct answer.

Here is an example; suppose I need to check the existence of "file.rar" linking in "http://thesiteurl.com/file.rar". Now I put the link into MyData. If MyData contains "file.rar" then it will download the file; else it will show the error dialog.

Can it be done through this way? I've tried -

Code: Select all

on mouseUp
   put URL "http://thesiteurl.com/file.rar" into myData
   put the result into rslt
   if rslt contains "file.rar" then
      // download will start
   else
      answer "No such file"
   end if
end mouseUp
But it's not working :(

Klaus
Posts: 14177
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Check file existence on Server

Post by Klaus » Mon Jan 21, 2013 3:46 pm

Please look up THE RESULT in the dictionary!
"THE RESULT" = EMPTY if an action had success!

Try this:

Code: Select all

on mouseUp
   put URL "http://thesiteurl.com/file.rar" into myData
   put the result into rslt
   if rslt = EMPTY then
    ## SUCCESS! 
    ## File is obviously on server!
    ## download will start
   else
      answer "There was an Error:" && rslt"
     ## This will cover ANY possible error like a connection error, timeout, server dow etc, 
     ## and not only a NON existing file!
   end if
end mouseUp
Best

Klaus

alemrantareq
Posts: 203
Joined: Wed Jul 23, 2008 8:46 am

Re: Check file existence on Server

Post by alemrantareq » Mon Jan 21, 2013 3:56 pm

bro, I've tried this -

Code: Select all

put URL "http://mysiteurl.com/file.rar" into myData
   put the result into rslt
   if rslt is empty then
    answer "Found"
   else
      answer "Not Found"
   end if
I've a file "file.rar" on my server. Then I've tried this script & says "Found". Then I modified the URL to "http://mysiteurl.com/file.txt" & it also says "Found" where there is no such existence of "file.txt" on the server !!

Klaus
Posts: 14177
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Check file existence on Server

Post by Klaus » Mon Jan 21, 2013 4:21 pm

Hi, I am too old for this "bro" shit, so please just call me Klaus, thanks 8)

Well, it always depends on how the webswerver is set up, although it should
NOT pretend to host a file that is not actually there.

But that is how THE RESULT works.
The server send something VALID (for RESULT), so the result is probably not the
best way to ensure the existence of the file.

I am afraid there is no special and "universal" recipe for your task, sorry.
At least I'm out of ideas...


Best

Klaus

sturgis
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1685
Joined: Sat Feb 28, 2009 11:49 pm

Re: Check file existence on Server

Post by sturgis » Mon Jan 21, 2013 4:32 pm

Check both the result and the data itself. If its returning a custom 404, chances are it still has the 404 error string in there somewhere, so look look for it! Its also possible that the last headers that are sent back will reflect the url that was re-directed to for the custom 404. So you might look up headers in the dictionary and see if anything there is helpful.

shaosean
Posts: 906
Joined: Thu Nov 04, 2010 7:53 am

Re: Check file existence on Server

Post by shaosean » Tue Jan 22, 2013 7:46 am

Not too certain if libURLFollowHttpRedirects works on the put URL command, but give it a go..

alemrantareq
Posts: 203
Joined: Wed Jul 23, 2008 8:46 am

Re: Check file existence on Server

Post by alemrantareq » Thu Jan 31, 2013 4:45 pm

checked with all the commands you've suggested, also custom 404 error checking; the file exists or not, in both case, it answers the same :(

Klaus
Posts: 14177
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Check file existence on Server

Post by Klaus » Thu Jan 31, 2013 5:09 pm

Can you post (parts of) "the result" and "myData"?
Maybe this will give us a hint...

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am

Re: Check file existence on Server

Post by Simon » Thu Jan 31, 2013 10:15 pm

Hi, I am too old for this "bro" shit, so please just call me Klaus,
You crack me up Klaus!!! :D :D :D

Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

alemrantareq
Posts: 203
Joined: Wed Jul 23, 2008 8:46 am

Re: Check file existence on Server

Post by alemrantareq » Fri Feb 01, 2013 4:48 pm

Klaus wrote:Can you post (parts of) "the result" and "myData"?
Maybe this will give us a hint...
Actually, I tried with making several scripts in different formats; but all are failed. Thats why I didnt save 'em :(

Now I forgot how did i cook them !! :?

Post Reply