Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Files and probably something simple

Status
Not open for further replies.

smileyone

Technical User
Jul 5, 2000
18
US
Ok, another problem...<br><br>I need to check if a file exists and if it doesn't exist, I need it to jump out of the loop.&nbsp;&nbsp;I don't know the syntax for checking if a file is there.&nbsp;&nbsp;I've looked in my book and online.&nbsp;&nbsp;I just don't think I'm looking it up correctly.<br><br>Thanks for the help!
 
Post the answer then! LOL! <p>Mike<br><a href=mailto:michael.j.lacey@ntlworld.com>michael.j.lacey@ntlworld.com</a><br><a href= Cargill's Corporate Web Site</a><br>
 
The easiest way I've seen to do it is....<br><br>open(INFO, filename) ¦¦ die<br><br>in place of die you can put other things, next or last etc.
 
If you don't want to open a file to check if it exists, there is a bunch of test operators you can use.&nbsp;&nbsp;Here's a quick summary of the most common:<br><br>-e&nbsp;&nbsp;&nbsp;&nbsp;Does the file exist?<br>-z&nbsp;&nbsp;&nbsp;&nbsp;Is the file zero length?<br>-s&nbsp;&nbsp;&nbsp;&nbsp;Is the file non-zero length?<br>-r&nbsp;&nbsp;&nbsp;&nbsp;Can I read the file?<br>-w&nbsp;&nbsp;&nbsp;&nbsp;Can I write to the file?<br>-x&nbsp;&nbsp;&nbsp;&nbsp;Can I execute the file?<br>-f&nbsp;&nbsp;&nbsp;&nbsp;Is it a standard file?<br>-d&nbsp;&nbsp;&nbsp;&nbsp;Is it a directory?<br>-T&nbsp;&nbsp;&nbsp;&nbsp;Is it a text file?<br>-B&nbsp;&nbsp;&nbsp;&nbsp;Is it a binary file?<br><br>Most of these imply the &quot;-e&quot; test.&nbsp;&nbsp;For example, if you can read a file, then it must exist.&nbsp;&nbsp;Here's a quick example that checks if we can execute a file and if it is a binary file.<br><FONT FACE=monospace><br>#!/usr/bin/perl<br><br>$My_File = &quot;/usr/bin/perl&quot;;<br><br>if ( -B $My_File && -x $My_File ) {<br>&nbsp;&nbsp;&nbsp;&nbsp;print $My_File, &quot; is an executable binary.\n&quot;;<br>}<br></font><br>Hope that helps. <p> <br><a href=mailto: > </a><br><a href= > </a><br>--<br>
0 1 - Just my two bits
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top