Why doesn't this work? (filehandling+regexp)

Please support our Perl advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: May 2008
Posts: 10
Reputation: musturd is an unknown quantity at this point 
Solved Threads: 0
musturd musturd is offline Offline
Newbie Poster

Why doesn't this work? (filehandling+regexp)

 
0
  #1
Apr 29th, 2009
  1. #!/usr/bin/perl
  2. use warnings;
  3. use strict;
  4.  
  5. push @INC, "C:/Perl/Modules";
  6.  
  7. print "What is current download directory? ";
  8. my $dir = <STDIN>;
  9. opendir DH, $dir or die "Can't open directory...";
  10. my $parent = $_;
  11. while ($parent = readdir(DH))
  12. {
  13. next if $parent eq "." or $parent eq "..";
  14. if(-d $parent)
  15. {
  16. opendir DI, "$dir/$parent" or die "Can't open directory...";
  17. while ($_ = readdir(DI))
  18. {
  19. next if $_ eq "." or $_ eq "..";
  20. if (/(\w+ )(\d+)\b(\d+)\b(.*)(\d+)/)
  21. {
  22. print "Long File";
  23. our $month = $1;
  24. our $day = $2;
  25. our $year = $3;
  26. our $name = $5;
  27. }
  28. else if(/(\w+ )(\d+)\b(\d+)\b(.*)(\d)\b/)
  29. {
  30. print "Short File";
  31. our $month = $1;
  32. our $day = $2;
  33. our $year = $3;
  34. our $name = $5;
  35. }
  36. }
  37. }
  38. }
  39. print "Done";

I use Windows Vista with ActivePerl.
Basically I have a bunch of folders within a large parent folder each with two mp3 files and a txt file (which I don't care about) in this format:
Month DD YYYY . Name . ##(#)kbps . XX(X) minutes.mp3
the two mp3 files are exactly the same format that the ones with two digit minutes I want to name one way (lets say "short") and ones with 3 digits I want to name another way (lets say "long"). I also have other mp3 files which are not in this format and I cannot figure out a way to grab the length or bitrate of an mp3 file in perl (maybe someone can help with this too).

Anyway, this code does not run. I get a syntax errors at lines 28 and 38. I am using 'else if' wrong probably...
(ignore my misplaced mys and ours)

I am very new to Perl programming and would love some help. Thanks.
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 898
Reputation: KevinADC has a spectacular aura about KevinADC has a spectacular aura about 
Solved Threads: 67
KevinADC's Avatar
KevinADC KevinADC is offline Offline
Practically a Posting Shark

Re: Why doesn't this work? (filehandling+regexp)

 
0
  #2
Apr 29th, 2009
try this:

  1. my $dir = <STDIN>;
  2. chomp $dir;#<-- remove the end of line character(s)

and change "else if" to "elsif".
Last edited by KevinADC; Apr 29th, 2009 at 11:30 pm.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 10
Reputation: musturd is an unknown quantity at this point 
Solved Threads: 0
musturd musturd is offline Offline
Newbie Poster

Re: Why doesn't this work? (filehandling+regexp)

 
0
  #3
Apr 29th, 2009
It's still giving me a syntax error at the else if line and the final closing bracket of the while loop.
(I'm sure you solved one problem with it though)
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 898
Reputation: KevinADC has a spectacular aura about KevinADC has a spectacular aura about 
Solved Threads: 67
KevinADC's Avatar
KevinADC KevinADC is offline Offline
Practically a Posting Shark

Re: Why doesn't this work? (filehandling+regexp)

 
0
  #4
Apr 29th, 2009
Try this, I didn't really fix any syntax errors though besides "else if":

  1. #!/usr/bin/perl
  2. use warnings;
  3. use strict;
  4.  
  5. push @INC, "C:/Perl/Modules";
  6.  
  7. print "What is current download directory? ";
  8. my $dir = <STDIN>;
  9. chomp $dir;
  10. opendir DH, $dir or die "Can't open directory: $!";
  11. while (my $parent = readdir(DH))
  12. {
  13. next if ($parent eq "." or $parent eq "..");
  14. if(-d $parent)
  15. {
  16. opendir DI, "$dir/$parent" or die "Can't open directory: $!";
  17. while ($_ = readdir(DI))
  18. {
  19. next if ($_ eq "." or $_ eq "..");
  20. if (/(\w+ )(\d+)\b(\d+)\b(.*)(\d+)/)
  21. {
  22. print "Long File";
  23. our $month = $1;
  24. our $day = $2;
  25. our $year = $3;
  26. our $name = $5;
  27. }
  28. elsif(/(\w+ )(\d+)\b(\d+)\b(.*)(\d)\b/)
  29. {
  30. print "Short File";
  31. our $month = $1;
  32. our $day = $2;
  33. our $year = $3;
  34. our $name = $5;
  35. }
  36. }
  37. }
  38. }
  39. print "Done";
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 10
Reputation: musturd is an unknown quantity at this point 
Solved Threads: 0
musturd musturd is offline Offline
Newbie Poster

Re: Why doesn't this work? (filehandling+regexp)

 
0
  #5
Apr 29th, 2009
Oops, I missed the elsif change you mentioned...
It runs now, but I didn't get any Long File or Short File returns, just the Done.
So either my regexps are wrong, or my directory handling is wrong. Am I doing anything else wrong?

The regular expression:
(\w+ ) - the month (any amount of word characters and a space)
(\d+) - 1 or 2 numbers (the day)
\b - a boundary
(\d+) - 4 numbers (the year)
\b - a boundary
(.*) - everything up to the last numbers in the string (because its greedy
(\d+) - 2 or 3 numbers (the length)

Is this wrong?
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 898
Reputation: KevinADC has a spectacular aura about KevinADC has a spectacular aura about 
Solved Threads: 67
KevinADC's Avatar
KevinADC KevinADC is offline Offline
Practically a Posting Shark

Re: Why doesn't this work? (filehandling+regexp)

 
0
  #6
Apr 30th, 2009
Work on the script one problem at a time. First check that the directory I/O is working:

  1. use warnings;
  2. use strict;
  3.  
  4. push @INC, "C:/Perl/Modules";
  5.  
  6. print "What is current download directory? ";
  7. my $dir = <STDIN>;
  8. chomp $dir;
  9. print "Current Directory is: $dir\n\n";
  10. opendir DH, $dir or die "Can't open directory: $!";
  11. while (my $parent = readdir(DH)){
  12. next if ($parent eq "." or $parent eq "..");
  13. print "> $parent\n";
  14. if(-d $parent){
  15. opendir DI, "$dir/$parent" or die "Can't open directory: $!";
  16. while (my $sub = readdir(DI)){
  17. next if ($sub eq "." or $sub eq "..");
  18. print ">>> $sub\n";
  19. }
  20. close DI;
  21. }
  22. }
  23. close DH;

Then if that is working try adding in the regexp stuff and see what happens. If it does not work then you have to figure out what the problem is.
Last edited by KevinADC; Apr 30th, 2009 at 1:09 pm.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 10
Reputation: musturd is an unknown quantity at this point 
Solved Threads: 0
musturd musturd is offline Offline
Newbie Poster

Re: Why doesn't this work? (filehandling+regexp)

 
0
  #7
May 1st, 2009
It lists all of the files and subdirectories from parent directory but nothing in any of the subdirectories...
Last edited by musturd; May 1st, 2009 at 5:43 pm.
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 898
Reputation: KevinADC has a spectacular aura about KevinADC has a spectacular aura about 
Solved Threads: 67
KevinADC's Avatar
KevinADC KevinADC is offline Offline
Practically a Posting Shark

Re: Why doesn't this work? (filehandling+regexp)

 
0
  #8
May 1st, 2009
If you want to drill down through all sub directories you should use the File::Find module that comes with perl.
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 898
Reputation: KevinADC has a spectacular aura about KevinADC has a spectacular aura about 
Solved Threads: 67
KevinADC's Avatar
KevinADC KevinADC is offline Offline
Practically a Posting Shark

Re: Why doesn't this work? (filehandling+regexp)

 
1
  #9
May 1st, 2009
If you really want to do it manually you need a recursive function, something like this:

  1. use warnings;
  2. use strict;
  3.  
  4. #push @INC, "C:/Perl/Modules";
  5.  
  6. print "What is current download directory? ";
  7. my $dir = <STDIN>;
  8. chomp $dir;
  9.  
  10. list($dir);
  11.  
  12. sub list {
  13. my ($dir) = @_;
  14. print "$dir\n";
  15. opendir(my $DH, $dir) or do{print "Can't open $dir: $!\n"; return};
  16. my @list = grep {$_ ne '.' and $_ ne '..'} readdir $DH;
  17. foreach my $file (@list){
  18. print "\t$file\n";
  19. list("$dir/$file") if (-d "$dir/$file");
  20. }
  21. }
Last edited by KevinADC; May 1st, 2009 at 7:18 pm.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 10
Reputation: musturd is an unknown quantity at this point 
Solved Threads: 0
musturd musturd is offline Offline
Newbie Poster

Re: Why doesn't this work? (filehandling+regexp)

 
0
  #10
May 2nd, 2009
Thanks a lot!!
Now I need to experiment with regular expressions...
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the Perl Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC