×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

File Not Opening ?!

File Not Opening ?!

File Not Opening ?!

(OP)
Point of this exercise is to open a folder, open a file, extract some info and output it.  I've got the folder open, but die-ing on file opening.  Don't understand why.  Do you?

Also, where can I change my profile settings from "programmer" to "technical user"? (or should it be "wannabe" ;-)!

Here's the code so far:

#!/usr/local/bin/perl -w
#------------------------
# Purpose: Open a folder, print file names from the folder,
# open each file and extract dates in all possible date formats,
# print each date along with name of file it came from.  Learn the
# next thingie in Perl to open the file of enlightenment
# -----------------------

$dir = 'c:/windows/desktop/DATE';   #temporary assignment to directory DATE

opendir DIR, $dir or die "Error: could not open directory $dir $!\n" ;   #open or die

while ($file = readdir DIR) { #read files in directory
next if $file=~/^\./; #avoid . and ..
print "Found a file: $file\n"; #check by printing directory file names

@file=$file;

foreach $file (@file) {
open FILE or die "Error: could not open file $file in directory $dir $!\n" ;   #open or die

print "Here's a file: $file"; #check by printing directory file names

@words = split /\s+/; #split words in file

foreach $words(@words){ #search each token

if ($words =~/(\d{1,4}¦$months)\/¦\-\d{1,4}¦(\d{1,4}¦$months)\/¦\-\d{1,4}/)

#a numeric or text month, one or two digit day, 2 or 4 digit year with backslash or dash

{print "$file\t$words\n";} #print each match per file, tabbed

}
}
}

RE: File Not Opening ?!

I think you simply need to close the block starting with
while ($file = readdir DIR) { #read files in directory
after the file array, @file=$file; is built.  The way you have it, that block stays open until the end.  

This works on my machine....

#!perl -w
$dir = 'c:/temp/';   #temporary assignment to directory temp

opendir DIR, $dir or die "Error: could not open directory $dir $!\n" ;   #open or die

while ($file = readdir DIR)
    {
    #read files in directory
    # next if $file=~/^\./; #avoid . and ..
    print "Found a file: $file\n"; #check by printing directory file names
    @file = $file;
    }
foreach $file (@file)
    {
    open FILE or die "Error: could not open file $file in directory $dir $!\n" ;       #open or die
    print "Here's a file: $file"; #check by printing directory file names
    }

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close