473,464 Members | 1,732 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Printing a "File Not Found" message in Perl.

32 New Member
Hi - I know almost nothing about Perl but do program in other languages. I am looking at Perl that I have to modify slightly by printing a "file not found" message if a file is missing from the directory or the directory just above it (hence the 'find ..' ). Please help!

Expand|Select|Wrap|Line Numbers
  1. sub findText
  2. {
  3.   my @aray = 'find .. -name veryImprotant.txt -print';
  4.   my $var = shift @aray;
  5.   # I want to check if file is not found here maybe?
  6.   return $var;
  7. }
  8.  
Jul 10 '09 #1
2 3673
nithinpes
410 Recognized Expert Contributor
While running system commands, pass the command within reverse quotes( ` ). You have used single quotes, hence the command will not be run (it is passed as a string).

Expand|Select|Wrap|Line Numbers
  1. sub findText 
  2.   my @aray = `find .. -name veryImprotant.txt -print`; 
  3.   my $var = shift @aray; 
  4.   print "File not found \n" unless($var);
  5.   return $var; 
  6.  
Jul 28 '09 #2
numberwhun
3,509 Recognized Expert Moderator Specialist
@dissectcode2
Personally, I would take a different route. I would actually use the open() function to open the file and die if it didn't work. If it opened, make a log entry into a log and then close the file.

Expand|Select|Wrap|Line Numbers
  1. sub findText {
  2.     open(FILE, "../veryImportant.txt") or die "Cannot open file:  $!";
  3.  
  4.     print LOG "File: veryImportant.txt exists.  Continuing.";
  5.  
  6.     close(<FILE>);
  7. }
  8.  
Yes, the log file represented by the LOG file handle will have had to be previously opened, but that is no biggie. My point was to show you another way of doing it that didn't involve executing a system command. This, btw, would possibly work on Unix, Linux and maybe even Windows due to the lack of system level command execution.

Regards,

Jeff
Jul 28 '09 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Mike Bobbitt | last post by:
I've recently decided to switch from compiled Apache/PHP to RPM's of both and I'm having a problem I can't seem to solve. I have a Perl script called php_include.cgi that parses a PHP file for...
3
by: Hugz | last post by:
Hello, And Thank you for taking time to help me.Myself Hugz.I am new to perl and want to write my own Scripts.But i can't understand how perl "rename" function and "file locking" works. ...
4
by: amywolfie | last post by:
I would like to put code behind a Find button on a form which: 1) Performs a find based on a field on the form 2) If NO RECORDS ARE FOUND, then displays a custom "No Records Found" message box. ...
3
by: Daniel Billingsley | last post by:
Today I went to compile a solution I've been working on for months. I've been off most of the last few months, so there's been a gap in the work, but I did compile it a few times earlier this...
1
by: Ian Lazarus | last post by:
Hello, Project = class library dll implemented in C++ as a managed wrapper around an unmanaged class, and a small project to consume it (shown at bottom). Running the consumer code results in...
0
by: Steve Chatham | last post by:
I am stuck on this. It ought to be a simple reason as to why this is problematic, in that it works on smaller groups of data (say under 40 records), but doesn't on larger groups of records (40+)....
2
by: naveeddil | last post by:
Hello, I use this command for including my ASP files into another it is working perfectly on my XP 2002 SP2, IIS server and also i upload to brinkster.com and working perfectly there but here is...
4
by: mpatharkar | last post by:
Hi all, I wrote a script to search a pattern in input file and if pattern does not found in input file ,print that pattern in to output file. The input file is...
1
by: RN1 | last post by:
Sub Page_Load(........) If Not Page.IsPostBack Then Call LoadData() End If End Sub Sub LoadData() Dim dSet As DataSet Dim sqlConn As SqlConnection Dim sqlDapter As SqlDataAdapter
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.