473,386 Members | 1,598 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Reading from a directory fails

I've got a script that's supposed to read files from a directory, and do pattern match on each file in the dir. If I run this script from within the dir that contains the files, it works fine but it fails to find any file if I run it from the parent dir.

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.  
  3. use strict;
  4. use warnings;
  5.  
  6. opendir my $DIR, '/home/tester1/logs/' or die "opendir .: $!\n";
  7. print "$DIR\n";
  8. my @files = grep /\.log$/i, readdir $DIR;
  9. closedir $DIR;
  10. # print "Got ", scalar @files, " files\n";
  11.  
  12. open my $out,'>',"report.txt";
  13.  
  14. foreach my $file (@files) {
  15.     open my $FILE, '<', $file or die "$file: $!\n";
  16.     while (<$FILE>) {
  17.        print $out $_ if /^<string to search>/;
  18. }
  19.     close $FILE;
  20. }
  21. close $out;
  22.  
  23.  
This is the output I get ...

bash-4.1$ ./test.pl
GLOB(0x200161f8)
test1.log: A file or directory in the path name does not exist.
bash-4.1$
Jan 17 '14 #1
2 1145
RonB
589 Expert Mod 512MB
Here's my answer I posted on your crosspost at perlguru.

readdir only returns the filename without its path. If you're not in the same directory as the path given to opendir, then you'll need to prepend that path to the filename in your open call.

Please read the documentation for the readdir function.
perldoc readdir
http://perldoc.perl.org/functions/readdir.html
Jan 17 '14 #2
Thanks, Ron! I appreciate it.
Jan 21 '14 #3

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

Similar topics

10
by: 3A Web Hosting | last post by:
Hi Folks This is probably starring me in the face but how do I read the contents of a hard drive directory? I've been playing around with the opendir($dir) sample from php.net and can read from...
2
by: Florian Liefers | last post by:
"Hello world\n", how can get a list of files/directories from a specified directory? something like FILE *fp=fopen("directory") while (...) { file=....; cout << file << endl; }
2
by: Greg Smith | last post by:
Hi, all. I have been asked to write an application that would keep track on archive data on CDs and DVDs. I will basically need a line for each file. I have started out by saving a "dir" to a...
1
by: Peter John | last post by:
I am using the following code to read from and write to the registry. The writing works fine but reading always fails. Can anyone suggest what is going wrong? Imports Microsoft.Win32 Public...
3
by: Karel | last post by:
Hello, I have a VB.NET application where I want to move directories over a network. I tried this with system.io.directory.move, but that doesn't work over different volumes. Has anyone a...
23
by: **Developer** | last post by:
Is there an easy way to copies all files in a directory into another directory? What about coping subdirectories too? Thanks in advance for any info
4
by: Peter Pippinger | last post by:
Hello NG, I have written some code which worked fine under c#. But i donīt know, how this shoud work in c++. I have tryed much things, but i canīt find out how to use DirectoryInfo and FileInfo...
4
by: Tim | last post by:
Can someone please tell me how I can get the last file added to a directory ( I am assuming it is a file attribute). I have built a text parsing application. Right now I have to manually get the...
1
by: mkarja | last post by:
Hi, I have a windows MDI program that draws some shapes that can be saved into a file and read from that file. The save seems to work with the WriteFile function, but for some reason the...
4
by: Laharl | last post by:
My Operating Systems professor has assigned homework that basically boils down to implementing ls -lra, but with a different output format. In other words, list the files and subdirectories (and a...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...

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.