Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

File Not Opening ?!

Status
Not open for further replies.

rja

Programmer
Jun 1, 2000
4
US
Point of this exercise is to open a folder, open a file, extract some info and output it.&nbsp;&nbsp;I've got the folder open, but die-ing on file opening.&nbsp;&nbsp;Don't understand why.&nbsp;&nbsp;Do you?<br><br>Also, where can I change my profile settings from &quot;programmer&quot; to &quot;technical user&quot;? (or should it be &quot;wannabe&quot; ;-)!<br><br>Here's the code so far:<br><br>#!/usr/local/bin/perl -w<br>#------------------------<br># Purpose: Open a folder, print file names from the folder, <br># open each file and extract dates in all possible date formats, <br># print each date along with name of file it came from.&nbsp;&nbsp;Learn the <br># next thingie in Perl to open the file of enlightenment <br># -----------------------<br><br>$dir = 'c:/windows/desktop/DATE';&nbsp;&nbsp; #temporary assignment to directory DATE<br><br>opendir DIR, $dir or die &quot;Error: could not open directory $dir $!\n&quot; ;&nbsp;&nbsp;&nbsp;#open or die<br><br>while ($file = readdir DIR) { #read files in directory<br> next if $file=~/^\./; #avoid . and ..<br> print &quot;Found a file: $file\n&quot;; #check by printing directory file names<br><br>@file=$file;<br><br>foreach $file (@file) {<br> open FILE or die &quot;Error: could not open file $file in directory $dir $!\n&quot; ;&nbsp;&nbsp;&nbsp;#open or die<br><br>print &quot;Here's a file: $file&quot;; #check by printing directory file names<br><br> @words = split /\s+/; #split words in file<br><br> foreach $words(@words){ #search each token<br><br> if ($words =~/(\d{1,4}¦$months)\/¦\-\d{1,4}¦(\d{1,4}¦$months)\/¦\-\d{1,4}/) <br> <br> #a numeric or text month, one or two digit day, 2 or 4 digit year with backslash or dash <br><br> {print &quot;$file\t$words\n&quot;;} #print each match per file, tabbed<br><br> }<br> }<br>}<br><br>
 
I think you simply need to close the block starting with <br><i>while ($file = readdir DIR) { #read files in directory</i><br>after the file array, <i>@file=$file; </i> is built.&nbsp;&nbsp;The way you have it, that block stays open until the end.&nbsp;&nbsp;<br><br>This works on my machine....<br><br><b>#!perl -w<br>$dir = 'c:/temp/';&nbsp;&nbsp;&nbsp;#temporary assignment to directory temp<br><br>opendir DIR, $dir or die &quot;Error: could not open directory $dir $!\n&quot; ;&nbsp;&nbsp;&nbsp;#open or die<br><br>while ($file = readdir DIR) <br>&nbsp;&nbsp;&nbsp;&nbsp;{ <br>&nbsp;&nbsp;&nbsp;&nbsp;#read files in directory<br>&nbsp;&nbsp;&nbsp;&nbsp;# next if $file=~/^\./; #avoid . and ..<br>&nbsp;&nbsp;&nbsp;&nbsp;print &quot;Found a file: $file\n&quot;; #check by printing directory file names<br>&nbsp;&nbsp;&nbsp;&nbsp;@file = $file;<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>foreach $file (@file) <br>&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;open FILE or die &quot;Error: could not open file $file in directory $dir $!\n&quot; ;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#open or die<br>&nbsp;&nbsp;&nbsp;&nbsp;print &quot;Here's a file: $file&quot;; #check by printing directory file names<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br></b>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top