Move a bunch of videos into directories based on their names

This program moves some videos from the desktop to a specific directory, depending on the name of the video. If the video's name begins with "9", for example, it moves the video into C:\Users\ben\Desktop\coursera\hinton\ch9\.

#!perl
use warnings;
use strict;
my $out = "coursera/hinton";
my @files = <*.mp4>;
print "@files\n";
for my $file (@files) {
    my ($first) = split / /, $file;
    print "$file\n";
    die unless $first =~ /^\d+$/;
    my $dir = "$out/ch$first";
    if (! -d $dir) {
        print "make dir $dir\n";
        mkdir $dir or die $!;
    }
    print "rename <<$file>> to $dir\n";
    rename $file, "$dir/$file" or die $!;
}

(download)

When running this type of program, before running it for real, it is better to make it just print out what it is going to do. Thus comment out the mkdir and rename lines, and run it with just the print statements first.

If the program needs to be run again and again, on the other hand, the print statements become irritating, so it is better to add, for example, a variable $verbose and wrap the print statements so that they are only executed if $verbose has a true value.


Copyright © Ben Bullock 2009-2023. All rights reserved. For comments, questions, and corrections, please email Ben Bullock (benkasminbullock@gmail.com) or use the discussion group at Google Groups. / Privacy / Disclaimer