?
Solved

Listing a directory with spaces in perl

Posted on 2009-04-20
6
Medium Priority
?
728 Views
Last Modified: 2012-05-06
here is my situation...

I have a music folder with thousands of artists, the directory structure is as follows:
artist name > Album name > Songs

example: [savona@bighat scripts]$ ls /home/savona/drobo/Music/The\ Beatles/
The Beatles - A Hard Day's Night    The Beatles - Revolver
The Beatles - Help                  The Beatles - Rubber Soul
The Beatles - Let It Be             The Beatles - Sgt. Peppers Lonely Hearts Club Band
The Beatles - Magical Mystery Tour


As a learning experience (just starting with perl) I wanted to make a perl script that will make me a nice clean music list text file.  Something that would end up looking like this:

The Beatles
      The Beatles - A Hard Day's Night
      The Beatles - Help
      The Beatles - Let It Be
      The Beatles - Magical Mystery Tour
      The Beatles - Revolver
      The Beatles - Rubber Soul
      The Beatles - Sgt. Peppers Lonely Hearts Club Band

So I have been sitting here hacking away at it but I am at a lost here.  How am I going to list the directories out if they have spaces in them?  I tried multiple ways, globbing, splitting, escaping (i.e. \\\)... I know it is something so simple I am missing.

Here is the code I have so far... some things may be commented out since I was trying a bunch of different things.  Any help would be appreciated.
#!/usr/bin/perl
# Set some variables
$musicdir="/home/savona/drobo/Music/";
# Get long listing of Music directory and awk out artist names and make temp files
system("ls -l $musicdir > /tmp/musiclist");
system("`awk '{print \$8,\$9,\$10,\$11,\$12,\$13,\$14,\$15,\$16}' /tmp/musiclist > /tmp/musiclist2`");
system("rm -f /tmp/musiclist");
system("`awk '{print \$1,\$2,\$3,\$4,\$5,\$6,\$7}' /tmp/musiclist2 > /tmp/musiclist3`");
# open temp file for reading
open (MUSICLIST, '/tmp/musiclist2');
while (<MUSICLIST>) {
        chomp($name);
#       @n = split / /;
#       $fullname = `echo "@n[0]" "@n[1]" "@n[2]" "@n[3]" "@n[4]" "@n[5]" "@n[6]" "@n[7]"`;
#       system("ls -l $musicdir$fullname >> /home/savona/musiclist.txt");
#
print "$name";
}

Open in new window

0
Comment
Question by:savone
6 Comments
 
LVL 9

Expert Comment

by:ghostdog74
ID: 24190971
if you want to use Perl, then use Perl. There's no need to use awk or any shell tools (for what you want).

use File::Basename;
my $music = "/home/savona/drobo/Music/The Beatles";
chdir($music);
my $artistname = basename($music);
print $artistname."\n";
while ( <*>){
 print "\t".$_."\n"; 
}

Open in new window

0
 
LVL 10

Accepted Solution

by:
oleber earned 980 total points
ID: 24191072
simplify at most
my $music = "/home/savona/drobo/Music/";
 
chdir $music;
foreach my $artist ( <*> ){
    if ( -d $artist ) {
        print "$artist\n"; 
        chdir $artist;
        foreach my $album ( <*> ){
            print "\t$album\n" if -d $album; 
        }    
        chdir "..";
    };
}

Open in new window

0
 
LVL 5

Expert Comment

by:vikaskhoria
ID: 24191080
You can also use this code snippet to list all the files in a drectory.
Do let me know i you need any more help on this.
$direct=<Directory Path>;
opendir(DIR,$direct);
print readdir(DIR);
closedir(DIR);

Open in new window

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 23

Author Closing Comment

by:savone
ID: 31572540
Thanks, this code does exactly what I wanted it to.  Is it possible you can take a second and explain what the code is doing?  How did you get around the spaces in the directory names?

Thanks!
0
 
LVL 23

Author Comment

by:savone
ID: 24193030
Oleber, how would I print the output to a file?
0
 
LVL 10

Expert Comment

by:oleber
ID: 24193123
as usual, redirect the output

perl script.pl > my_file.txt
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans
Suggested Courses

579 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question