in reply to Re^2: I need help with opening a directory and reading files in that directory.
in thread I need help with opening a directory and reading files in that directory.
Brandon,
Let's looks at the solution you have come up with; but before we do - just a quick note:
#!/usr/bin/perl -slw use strict; use warnings;
The use warnings; pragma is redundant if you supply the -w switch
--
Now, looking at your solution we can see that you've opened a target directory and proceeded to read its contents via the excerpt below:
opendir DH, "Y://perlscripts2/software/perl2/music" or die $!; # open +current directory while($_ = readdir(DH)){ # don't evaluate special dirs '.' & '..' next if $_ eq "." or $_ eq "..";
What comes next is a bit peculiar..
if ($_ =~ /\.txt/){ chomp $_; my ($artist, $song_title) = split '-', $_, 3; $length{$artist}{$artist} = $artist; $length{$artist}{$song_title} = $song_title; print "$_"; # print this file/directory's name }
What you're saying above is to match filenames with ".txt" in them. Are you sure this is what you want to do? Do you have ".txt" embedded within your music filenames?
Notwithstanding your expectations of the existence of a ".txt" filename which you'd like to process in your music files directory - you should be doing that check within the 2nd if (-f $_) conditional; so you only check legitimate regular files for the ".txt" string.
Here's a small script I wrote to process some flac files within one of my music directories (which is devoid of any ".txt" files)
#! perl -slw use strict; use Data::Dumper; $Data::Dumper::Sortkeys = 1; my ($id,$songs_ref,$artists_ref,$albums_ref,%songs,%artists,%albums); opendir DH, "D:/music/Alice In Chains/2001 - Greatest Hits" or die $!; # open current directory while($_ = readdir(DH)){ # don't evaluate special dirs '.' & '..' next if $_ eq "." or $_ eq ".."; # music files in this directory are *.flac files with the # naming style: <track#> - <band> - <album> - <track.flac> # no need to check stat info - we're processing files that # end in .flac (notice the end-of-line match char '$'?) if (m{\.flac$}){ # tip: m{..} is another way of saying /../ # add .flac to the split pattern so that it is not # included in our "<track>" field my ($track_num, $band, $album, $track) = split / \- |\.flac/; # increment our hash ref id so we don't overwrite entries $id++; # ------------------------------------------------------- # Example Using Hash References # ------------------------------------------------------- $songs_ref->{$id}->{TITLE} = $track; $songs_ref->{$id}->{ARTIST} = $band; $songs_ref->{$id}->{FAVOURITE} = 1 if $track eq 'Would'; $songs_ref->{$id}->{TRACK_LENGTH} = undef; # to do later $artists_ref->{$id}->{NAME} = $band; $artists_ref->{$id}->{SONGS}->{TITLE} = $track; $artists_ref->{$id}->{SONGS}->{ALBUM} = $album; $albums_ref->{$id}->{ARTIST} = $band; $albums_ref->{$id}->{NAME} = $album; $albums_ref->{$id}->{SONGS}->{TITLE} = $track; $albums_ref->{$id}->{SONGS}->{TRACK_NUM} = $track_num; # ------------------------------------------------------- # Example Using Hashes # ------------------------------------------------------- $songs{$id}{TITLE} = $track; $songs{$id}{ARTIST} = $band; $songs{$id}{TRACK_LENGTH} = undef; # to do later $artists{$id}{NAME} = $band; $artists{$id}{SONGS}{TITLE} = $track; $artists{$id}{SONGS}{ALBUM} = $album; $albums{$id}{ARTIST} = $band; $albums{$id}{NAME} = $album; $albums{$id}{SONGS}{TITLE} = $track; $albums{$id}{SONGS}{TRACK_NUM} = $track_num; } } closedir DH; # use perl's object data serializer to view our data print Dumper $songs_ref; print Dumper $artists_ref; print Dumper $albums_ref; print Dumper \%songs; print Dumper \%artists; print Dumper \%albums; __END__
I would like to focus on why it is you're not getting those ".txt" files open for processing, but to do that we need to establish which files are in the directory that you're reading from? Have a look at the example provided and if you find it's still necessary to open and read some kind of ".txt" file - please provide us with the output of dir "Y://perlscripts2/software/perl2/music"
Good luck!
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: I need help with opening a directory and reading files in that directory.
by brawal128 (Novice) on Sep 09, 2015 at 04:12 UTC | |
by shadowsong (Pilgrim) on Sep 09, 2015 at 09:37 UTC | |
by brawal128 (Novice) on Sep 09, 2015 at 14:46 UTC | |
by shadowsong (Pilgrim) on Sep 09, 2015 at 22:51 UTC | |
by brawal128 (Novice) on Sep 10, 2015 at 21:20 UTC | |
|