Find the answer to your Linux question:
Results 1 to 6 of 6
In perl I've written this program. Code: #!/usr/local/bin/perl use MP3&#58;&#58;Info; opendir&#40;DIRHANDLE,"c&#58;\Documents and Settings\Nallelcm.NALLELCM-LT\My Documents\My Music\"&#41;; @filenames = readdir&#40;DIRHANDLE&#41;; print "\nMusiX-ML\n"; closedir&#40;DIRHANDLE&#41;; open&#40;MUSICXML, ">C&#58;\Documents and Settings\Nallelcm.NALLELCM-LT\My Documents\My Music\\$music.xml"&#41;; print MUSICXML "<?xml ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Sep 2004
    Posts
    35

    Perl directory reading?


    In perl I've written this program.
    Code:
    #!/usr/local/bin/perl
    
    use MP3&#58;&#58;Info;
    opendir&#40;DIRHANDLE,"c&#58;\\Documents and Settings\\Nallelcm.NALLELCM-LT\\My Documents\\My Music\\"&#41;;
    @filenames = readdir&#40;DIRHANDLE&#41;; 
    
    print "\nMusiX-ML\n";
    closedir&#40;DIRHANDLE&#41;;
    open&#40;MUSICXML, ">C&#58;\\Documents and Settings\\Nallelcm.NALLELCM-LT\\My Documents\\My Music\\\$music.xml"&#41;;
    print MUSICXML "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>";
    print MUSICXML "<music>";
    foreach $filename &#40;@filenames&#41; &#123;
    	if &#40;$filename =~ /^\w.*\.mp3$/i&#41; &#123;
    		my $file = "C&#58;\\Documents and Settings\\Nallelcm.NALLELCM-LT\\My Documents\\My Music\\" . $filename;
    		my $tag = get_mp3tag&#40;$file&#41;;
    		foreach my $field qw&#40;ARTIST TITLE&#41; &#123;
    			if &#40;$field eq ARTIST&#41; &#123;
    				print MUSICXML "<song " . ucfirst&#40; lc $field &#41;. "=\"" . $tag->&#123;$field&#125; . "\" ";
    			&#125;
    			if &#40;$field eq TITLE&#41; &#123;
    				print MUSICXML ucfirst&#40; lc $field &#41;. "=\"" . $tag->&#123;$field&#125;. "\"/>";
    			&#125;
    		&#125;
    	&#125;
    &#125;
    print MUSICXML "</music>";
    close&#40;MUSICXML&#41;;
    What the program does (if your to lazy to read the code) is goes through my music directory and lists all the music in an XML file, I wont get into the reaon why I want it in an XML file... but anyway... the problem arises... that i've changed the way I sort my music... I no longer have all my music bulked into a music folder... i have it sorted like "musicfolder/ARTIST/" so is there a way to look through each directory... i could probibly figure it out if someone told me how to find the directorys in a directory, im assuming its similar to finding the files... i just cant figure it out

  2. #2
    Linux Guru
    Join Date
    Mar 2003
    Location
    Wisconsin
    Posts
    1,907
    What you want to do is create a loop that looks at each file in the topmost directory and checks whether it's a file or directory. If it's a directory, then descend into it, otherwise process it. Make sure you include a counter or some other means to get back to the previous dirctory, unless you can always cd.. For me, I have my tunes as /music/artistname/albumname, but sometimes it's just /music/albumname, so I coulnd't just cd ../../ everytime.

    Good Luck!
    Jeremy
    Registered Linux user #346571
    "All The Dude ever wanted was his rug back" - The Dude

  3. #3
    Just Joined!
    Join Date
    Sep 2004
    Posts
    35
    Quote Originally Posted by jeremy1701
    What you want to do is create a loop that looks at each file in the topmost directory and checks whether it's a file or directory. If it's a directory, then descend into it, otherwise process it. Make sure you include a counter or some other means to get back to the previous dirctory, unless you can always cd.. For me, I have my tunes as /music/artistname/albumname, but sometimes it's just /music/albumname, so I coulnd't just cd ../../ everytime.

    Good Luck!
    Jeremy
    how would i go about checking to see if its a directory?

  4. #4
    Linux Guru sarumont's Avatar
    Join Date
    Apr 2003
    Location
    /dev/urandom
    Posts
    3,682
    Code:
    if&#40; -d $file &#41;
    "Time is an illusion. Lunchtime, doubly so."
    ~Douglas Adams, The Hitchhiker's Guide to the Galaxy

  5. #5
    Just Joined!
    Join Date
    Sep 2004
    Posts
    35
    Quote Originally Posted by sarumont
    Code:
    if&#40; -d $file &#41;
    that only outputs . and ..
    i got it to work, if anyone's interested...
    Code:
    #!/usr/local/bin/perl
    
    use MP3&#58;&#58;Info;
    opendir&#40;DIRHANDLE,"c&#58;\\Documents and Settings\\Nallelcm.NALLELCM-LT\\My Documents\\My Music\\"&#41;;
    	@toplvl = readdir&#40;DIRHANDLE&#41;; 
    closedir&#40;DIRHANDLE&#41;;
    $count = 0;
    print "\nMusiX-ML\n";
    open&#40;MUSICXML, ">C&#58;\\Documents and Settings\\Nallelcm.NALLELCM-LT\\My Documents\\My Music\\\$music.xml"&#41;;
    print MUSICXML "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>";
    print MUSICXML "<music>";
    foreach $dir &#40;@toplvl&#41; &#123;
    	if &#40;! -d $dir &#41; &#123;
    		opendir&#40;TOPLVL, "c&#58;\\Documents and Settings\\Nallelcm.NALLELCM-LT\\My Documents\\My Music\\$dir"&#41;;
    			foreach $name &#40;sort readdir&#40;TOPLVL&#41;&#41; &#123;
    				if &#40;! -d $name&#41; &#123;
       					push&#40;@files, "c&#58;\\Documents and Settings\\Nallelcm.NALLELCM-LT\\My Documents\\My Music\\$dir\\$name"&#41;;
    				&#125;
      			&#125;
    		closedir&#40;TOPLVL&#41;;
    	&#125;	
    &#125;
    foreach $filename &#40;@files&#41; &#123;
    	if &#40;$filename =~ /^\w.*\.mp3$/i&#41; &#123;
    		$count++;
    		my $file = $filename;
    		my $tag = get_mp3tag&#40;$file&#41;;
    		foreach my $field qw&#40;ARTIST TITLE&#41; &#123;
    			if &#40;$field eq ARTIST&#41; &#123;
    				print MUSICXML "<song " . ucfirst&#40; lc $field &#41;. "=\"" . $tag->&#123;$field&#125; . "\" ";
    			&#125;
    			if &#40;$field eq TITLE&#41; &#123;
    				print MUSICXML ucfirst&#40; lc $field &#41;. "=\"" . $tag->&#123;$field&#125;. "\"/>";
    			&#125;
    		&#125;
    	&#125;
    &#125;
    print MUSICXML "</music>";
    print "\n$count files added to File";
    close&#40;MUSICXML&#41;;
    if anyone's bored and feel like giving me pointers as to where I could be more efficent.

  6. #6
    Linux User
    Join Date
    Jul 2004
    Posts
    256
    On windows, if you use forward slashes for directories ( "/" instead of "\\") you save yourself a lot of escaping :P

    Heres an example, it reads the directory you call the function with, and each sub-directory (it's a recursive function) Its part of an old script, so it might not work exactly right, but its to show you how you can use a recursive function to search directories recursively
    Code:
    #!/usr/bin/perl -w
    
    use strict; #use warning, and strict
    
    my $dir_root; #dir to start in
    
    sub dir_read 
    &#123;
      #parse directory for directories and files
    
      #local to this function
      my @dir_list;
      my @file_list;
      my $dir_prefix = $_&#91;0&#93;;  
      
      print "reading dir&#58; $&#123;dir_prefix&#125;\n";
      
      opendir&#40;aDIR, $dir_prefix&#41;;
      #read, add to array
      while&#40;$_ = readdir&#40;aDIR&#41;&#41;
      &#123;
        #if a dir
        if&#40;-d "$&#123;dir_prefix&#125;/$&#123;_&#125;"&#41;
        &#123;
          #dont allow . or ..
          if&#40;$_ ne "." && $_ ne ".."&#41;
          &#123;
            #add to array
            push&#40;@dir_list, "$&#123;dir_prefix&#125;/$&#123;_&#125;"&#41;;
          &#125;
        &#125;
        #else a file
        else
        &#123;
          #filter any files you don't want 
          if&#40; $_ ne "dirlist.pl"&#41;
          &#123;
            push&#40;@file_list, "$&#123;dir_prefix&#125;/$&#123;_&#125;"&#41;;
          &#125;
        &#125;
      &#125;
      
      closedir&#40;aDIR&#41;;
      
    
    
        #print dir and file list&#58;
        foreach $_ &#40;@dir_list&#41;
        &#123;
          print "\tdir&#58; $&#123;_&#125;\n";
        &#125;  
        foreach $_ &#40;@file_list&#41;
        &#123;
          print "\tfile&#58; $&#123;_&#125;\n";
        &#125;  
      
      
      #search lower dirs
      foreach $_ &#40;@dir_list&#41;
      &#123;
        &dir_read&#40;$_&#41;;
      &#125;
    &#125;
    
    
    if&#40;! $ARGV&#91;0&#93;&#41;
    &#123;
      #print usage
      print "Usage&#58; perl dirlist.pl rootpath\n";  
    &#125;
    else 
    &#123;
      #check if path exists
      $dir_root = $ARGV&#91;0&#93;;
      if&#40; -d $dir_root&#41;
      &#123;
        #dir is ok
        print "root directory&#58; $&#123;dir_root&#125;\n";
      &#125;
      else
      &#123;
        #end
        die&#40;"root directory&#58; $&#123;dir_root&#125; does not exist!, stopped\n"&#41;;
      &#125;
    
      #read dir, and sub directories
      &dir_read&#40;$dir_root&#41;;
      
      print "done.\n";
    &#125;
    --monkey

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •