Computing.Net > Forums > Unix > Read input file for deleting images.

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Read input file for deleting images.

Reply to Message Icon

Name: Anthony Pomtree
Date: February 6, 2002 at 22:40:17 Pacific
Comment:

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.



Sponsored Link
Ads by Google

Response Number 1
Name: Tran
Date: February 7, 2002 at 06:06:22 Pacific
Reply:

#!/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 spcs

if (-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");


0

Response Number 2
Name: James Boothe
Date: February 7, 2002 at 07:24:49 Pacific
Reply:

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 0

The 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 =================================='


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More


Search and Replace comman... how can i creat my excuta...



Post Locked

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


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: Read input file for deleting images.

read input file variables www.computing.net/answers/unix/read-input-file-variables/5520.html

deleting rows in a file www.computing.net/answers/unix/deleting-rows-in-a-file/3676.html

shell script that reads a file www.computing.net/answers/unix/shell-script-that-reads-a-file/4967.html