Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

deleting files in a directory

Status
Not open for further replies.

tanveer91

Programmer
Nov 13, 2001
31
GB
Hi all,

I am having trouble trying to delete unwanted files in a specific directory.

E.G. if i wanted the perl script to search a directory for all jpg photos that DID NOT have an HTML file associated with them and then delete those jpg photos:

/html - abc.htm, abc2.htm, abc3.htm
/html/images - abc.jpg, abc2.jpg, abc3.jpg, abc4.jpg, abc5.jpg

therefore need to delete abc4.jpg and abc5.jpg
does it make sense??
 
Here's some fairly obfuscated (sorry) code that should do it:
Code:
# get files using glob
#
@htm = glob( "html/*.htm" );
@jpg = glob( "html/images/*.jpg" );

# store prefixes in hash
foreach( @htm ) {
    m<html/(\w+)\.htm> && $prefix{$1}++
}

# remove files if no prefix match
foreach( @jpg ) {
    m<html/images/(\w+)\.jpg> && !exists $prefix{$1} && unlink;
}
Cheers Neil :cool:
 
Thanks for your help 'toolkit', unfortunatly, still not removing the unwanted files!..........
i have attached the complete code, both htm & jpg files are definetly pointing to the correct directory, so that is not the problem.......again, thanks for ur help!

#!/usr/bin/perl -w
# httpd~/bin/photodel - Checks car photos if no matching html file removes photo

# get files using glob

@htm = glob(&quot;/home/httpd/html/ringways/photos/Leeds/*.htm&quot;);
@jpg = glob(&quot;/home/httpd/html/ringways/photos/Leeds/*.jpg&quot;);

# store prefixes in hash

foreach( @htm ) {
m<html/(\w+)\.htm> && $prefix{$1}++
}

# remove files if no prefix match

foreach ( @jpg ) {
m<html/images/(\w+)\.jpg> && !exists $prefix
{$1} && unlink;

}
 
That is because the regular expressions do not match the path you are using. Try the following regular expressions instead:
Code:
htm:     m<Leeds/(\w+)\.htm>
jpg:     m<Leeds/(\w+)\.jpg>

Cheers, Neil
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top