Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
It's been a while since I wrote a script, so I thought I would ask for help. Here is my dilemma. I want to write a script that will do the following:
1.) Read in a text file (in this file, on each line there will be the absolute path for an image).
2.) After it reads in the file, it needs to go through each line, determine if the image exists or not. If the image does indeed exists, then go ahead and remove it. Once deleting all of the images, shoot out an OK message.
Any and all help will be appreciated.
Thanks.

#!/usr/local/bin/perl
use File::Copy;
$textfile="mylist.txt";
if ( !(-o "$textfile") )
{
print "You are NOT a owner of $textfile file.\n";
exit();
}open(READ,"$textfile");
open(WRITE,">$textfile.new");
$non_exist_occur=0;while($line=)
{
if ( $line =~ /\n$/) # if character at EOL
{
chop($line); # get rid of it
}
$line =~ s/^\s+//; #zap-out leading spaces
$line =~ s/\s+$//; #zap-out trailing spcsif (-f $line)
{
print WRITE "$line\n";
}
else
{
print "Deleted: $line\n";
$non_exist_occur++;
}
}
close(WRITE);
close(READ);if ($non_exist_occur)
{
copy("$textfile","$textfile.bak");
copy("$textfile.new","$textfile");
print "$textfile updated; $non_exist_occur lines deleted.\n";
}
else
{
print "Nothing deleted; $textfile is unchanged.\n";
}
unlink("$textfile.new");

Well, perl is quite an overkill for a task that can be done easily with basic unix. And not all shops have perl installed.
#!/bin/sh
cat mylist.txt | while read fname other
do
if [ -f $fname ] ; then
if rm -f $fname 2> /dev/null ; then
:
else
echo "Could not remove --> $fname"
fi
else
echo "Does not exist ----> $fname"
fi
done
echo '\nDone.\n'
exit 0The above script will remove the files listed in mylist.txt. It assumes the first word on each line is the file reference, and it ignores any preceding spaces and any spaces or whatever that might follow on that line.
That perl script restricts you from removing files where you are not the owner, but that was not in your requirement. My script removes all listed files that I am allowed to remove, whether I own them or not. If I have write permissions on the directory, then I will be allowed to remove them, even if I do not own them and even if I do not have write permissions on the file.
You did not ask to see "does not exist" diagnostics, so if you want, you can remove those two lines (else, echo Does not exist).
And if you want a script more suitable for batch, you could use the following version. Each run appends to a log file. For this batch version, I included "Removed" messages for better audit trail.
#!/bin/sh
exec >> removal.log
echo "==== Starting: `date` ===="
cat mylist.txt | while read fname other
do
if [ -f $fname ] ; then
if rm -f $fname 2> /dev/null ; then
echo "Removed -----------> $fname"
else
echo "Could not remove --> $fname"
fi
else
echo "Does not exist ----> $fname"
fi
done
echo '==== finished =================================='

![]() |
Search and Replace comman...
|
how can i creat my excuta...
|

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |