Question : Perl script to delete files from directory

I have a directory with about 1700 jpg files in it. I also have a text file listing about 400 files that need to be removed from this directory. I know a simple perl script can do this...I just have very little perl experience. Any ideas?

My text file goes like:
100-1-2.jpg
100-3-2.jpg
101-5-1.jpg
etc.

Thanks.

Answer : Perl script to delete files from directory


The mist.pl script that i posted earlier is partial. here is the complete one.

NOTE: i have put in error checking and reporting in this script so you know which files were deleted, which were not deleted and why.

=====mist.pl
#!/usr/local/bin/perl

$|++; ## Disable output buffering

##  FULL PATH to the data file.
$data_file="/directory/sub_directory/mist.txt";

##  Open the data file for reading, exit if unable to read
open(MST, $data_file) || die $!;
@mist=;  ##  Read each line of the file as an element of the array.
close (MST);

##  Remove newline characters from the end of each element.
chomp(@mist);

##  Full path to the directory from which the file must be deleted.
$base_dir="/directory/sub_directory/";

foreach $file (@mist){
 $full_path_to_file=$base_dir.$file;
 print "Trying to delete $full_path_to_file....";

 if (unlink($full_path_to_file)){
   print "....Successful!\n";
  }else{
    warn "...failed: $!\n";
  }
}
Random Solutions  
 
programming4us programming4us