Solved

Moving files to sub-dirs based on extenstions

Posted on 2008-06-10
2
167 Views
Last Modified: 2010-03-05

I need a Perl script to do the following...

1. I have a collection of 40000+ audio and video files scattered across a file system.  I would like to run a Perl script from a directory that does the following...

2. Search for ALL audio and video files located in the current directory and move them to sub-directories named on the file extension.  Thus, all .mp3 files go into a sub-dir named mp3, .avi got into a sub-dir named avi

3. The sub-dirs named after the audio-video file extensions may or may not pre-exist in a directory.  Some of the audio video files I have seen are: .mp3, .asf, .mp4, .m4v, .mov, .avi, .wmv, .mpg, .mpeg, .rax.  This is not a complete set, and the developer should use a list of the commonly known audio video extensions.

4. Any directory may have further sub-directories that are NOT named after the audio video extension names.  If this happens, the script should traverse down into such directories and search for audio video files and execute step 2 above again...

5. Thus, at the end of he script run, there will be directories named avi / mp3 / mpg / etc located in multiple directories.  None of these directories can be empty, if an empty audio video directory is found anywhere (it probably pre-existed) with no audio or video files in it, such a directory should be deleted.

Please ask if you have any questions.
0
Comment
Question by:jmohsin
2 Comments
 
LVL 84

Accepted Solution

by:
ozo earned 500 total points
ID: 21756129
use File::Find;
find( sub{ /\.(mp3|asf|mp4|m4v|mov|avi|wmv|mpg|mpeg|rax)$/i && -f && push @{$mv{$File::Find::dir}{$1}},$_},".");
for my $d ( keys %mv ){
   for $s ( keys %{$mv{$d}} ){
      mkdir "$d/$s";
     rename "$d/$_","$d/$s/$_" or warn "rename $d/$_, $d/$s/$_ $!"   for @{$mv{$d}{$s}};
  }
}

# note that if you run this multiple times the files could get moved to
# mp3 then mp3/mp3 then mp3/mp3/mp3 ...
0
 

Author Comment

by:jmohsin
ID: 21756374

Hi Ozo,
Is there anyway not to create the recursive audio video subdirs in case the script is run more than once?  I will need to run the script again and again since the files are added to the file system every few days on an ongoing basis.  Sorry, I should have stated that above.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

867 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now