473,545 Members | 2,003 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Reading from a directory fails

37 New Member
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 1150
RonB
589 Recognized Expert Moderator Contributor
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
joeferns79
37 New Member
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
6957
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 the server without problem but how can I read a list of files from the hard drive of the PC (win98 (spit!)) I'm using? Basically what I want to do...
2
2232
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
1430
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 text file and parsing the file, but it seems there must be a better way to do this. Is there a way to read down a directory tree and get all the...
1
1663
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 Class clsRegistry Private Const conRegKey As String = "Software\MyCompany\MyApplication"
3
4167
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 sollution for this problem? thx
23
2401
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
5022
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 under c++. Thanks for any hints! Peter Here is the c# code:
4
1357
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 file to load it up. What I want to be able to do is have the application (on load) check a directory, get the last file added and parse the file...
1
2109
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 ReadFile function fails. These WriteFile/ReadFile are win32 api functions. I'll post both, the read and write function codes here so you can take
4
7256
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 bit of data about them, for the files) in the current directory and its subdirectories using system calls. Unfortunately, it isn't working quite right....
0
7478
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7410
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7668
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7923
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7773
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
3466
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1025
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
722
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.