Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Check Existence of a File

by Anonymous Monk
on May 06, 2003 at 19:33 UTC ( [id://256006]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I need help here, please. I have a page that has links to a batch file in the user's computer, if the user doesn't have the batch file it will generate a page not found page, if it is found it will display the results by the batch file. My question is, there is a way to check for the presence of the batch file in the user computer and redirect them to a page explaining that don't have it. Or by detecting from the browser that the batch file doesn't exist when page can't not be displayed comes up, therefore they will be redirect somewhere. Can someone point me to the right direction on how to do such a program or even if it is possible? Thank you very much!!

Replies are listed 'Best First'.
Re: Check Existence of a File
by kelan (Deacon) on May 06, 2003 at 19:51 UTC

    if ( -e "path/to/file" ) { print "File exists, hooray!"; } else { print "I couldn't find the file. Hohum:("; }

    kelan


    Perl6 Grammar Student

Re: Check Existence of a File
by Jenda (Abbot) on May 06, 2003 at 20:42 UTC

    I hope not. I don't want your CGIs, nor your JavaScript to be able to see what do or don't I have on my disk.

    "Link to a batch file on the user's computer" sounds suspicious enough by itself. What are you trying to do???

    Jenda
    Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.
       -- Rick Osborne

    Edit by castaway: Closed small tag in signature

      I'm with jenda. What kind of hat is this question wearing? Page implies a website, so no, a website should not be able to see a file on my computer, unless I'm running my own local webserver. Please explain.
      --
      “Every bit of code is either naturally related to the problem at hand, or else it's an accidental side effect of the fact that you happened to solve the problem using a digital computer.”
      M-J D
Re: Check Existence of a File
by nite_man (Deacon) on May 06, 2003 at 19:55 UTC
    For checking of file existing you can use -e (see perldoc -f -e and you will know all about working with file system in Perl):
    if(-e $fname) { print 'File exists!' } else { print 'File not found' }
    in your script or in the module.
    Also you can use module File::Find or File::Find::Rule
          
    --------------------------------
    SV* sv_bless(SV* sv, HV* stash);
    
Re: Check Existence of a File
by cbro (Pilgrim) on May 06, 2003 at 19:49 UTC
    I would suggest File::Find, readdir, or failing on a function such as open.
    To explain a little:
    If you use readdir, you could get a list of all the files and do a regex match to see if the file your looking for exists. If not, redirect the user...as you suggested.
    You could even do something like:
    #pseudo code open(FILE,"filename") || call_function_that_redirects_user_as_you_suggested. #end pseudo code
    HTH,
    Chris

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://256006]
Approved by sschneid
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (4)
As of 2024-04-25 16:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found