473,398 Members | 2,125 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,398 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 1146
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: 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
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
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
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,...

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.