Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Can I open a specific file (eg. '.log') without knowing the actual name of it?

by trouble (Beadle)
on Apr 07, 2001 at 00:17 UTC ( [id://70586]=perlquestion: print w/replies, xml ) Need Help??

trouble has asked for the wisdom of the Perl Monks concerning the following question:

assuming there is only one log file and in the same directory as the perl script... Thank you!
  • Comment on Can I open a specific file (eg. '.log') without knowing the actual name of it?

Replies are listed 'Best First'.
Re: Can I open a specific file (eg. '.log') without knowing the actual name of it?
by Adam (Vicar) on Apr 07, 2001 at 00:37 UTC
    glob.
    use strict; use IO::File; my @logFiles = glob( '*.log' ); my @data; for ( @logFiles ) { # You can further limit your file names thusly: die "Weird File Name" if m/[^a-zA-Z0-9._-]/; my $fh = IO::File->new( '< $_' ) or die "Open Failed for '$_', $!"; push @data, <$fh>; # Do something with the file $fh->close(); }
Re: Can I open a specific file (eg. '.log') without knowing the actual name of it?
by Anonymous Monk on Apr 07, 2001 at 00:34 UTC
    @files = glob('*.log'); if(@files == 1) { open(FILE, $files[0]); } else { #do something here }
Re: Can I open a specific file (eg. '.log') without knowing the actual name of it?
by suaveant (Parson) on Apr 07, 2001 at 00:34 UTC
    opendir(DIR, '.') or die $!; for(readdir(DIR)) { if(/\.log$/) { $file = $_; last; } } closedir(DIR);
    That'll put the name of the first file it finds in the directory that ends in .log and puts it in $file... otherwise $file stays empty...
                    - Ant
Re: Can I open a specific file (eg. '.log') without knowing the actual name of it?
by rchiav (Deacon) on Apr 07, 2001 at 00:38 UTC
    What you're looking for is called globbing. Warning - this will get all files with *.log..
    use strict; my @loglist; #put all *.log files in the array @loglist = <./*.log>;

    Then you know what each file name is..

    Hope this helps..
    Rich

    I wish there was a "10 nodes were added while you were writing this" alert :)

      I wish there was a "10 nodes were added while you were writing this" alert :)

      Yeah maybe that should be one of the things you see when you click on the preview button!

      It's too bad vroom is so busy with other stuff

      -- Dave
Re: Can I open a specific file (eg. '.log') without knowing the actual name of it?
by thabenksta (Pilgrim) on Apr 07, 2001 at 00:33 UTC

    readdir will give you a list of all the files in the current directory. You could use that and just exclude the script's filename.

    my $name = 'Ben Kittrell'; $name=~s/^(.+)\s(.).+$/\L$1$2/g; my $nick = 'tha' . $name . 'sta';
Re: Can I open a specific file (eg. '.log') without knowing the actual name of it?
by larryk (Friar) on Apr 07, 2001 at 12:50 UTC
    opendir(DIR,'.'); @all_logs = grep /\.log$/, readdir(DIR); for my $log (@all_logs) { # some processing }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://70586]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (7)
As of 2024-04-23 18:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found