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 wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Finding things the hard way

Status
Not open for further replies.

rickgerdes

IS-IT--Management
Feb 11, 2003
44
US
I have a routine that seaches a directory tree for files that match the criteria - filename=8 numeric characters.*

Any eight numbers, doesn't matter. Wherever it finds these files, it needs to delete them. Currently I do this the very hard way of: (please don't laugh)

open(LIST, "dir t:\\tmwin\\????????.* /s/b/n |");
@all=<LIST>;
close LIST;
foreach $l (@all)
{
$l1 = substr $l, -13, 1;
$l2 = substr $l, -12, 1;
$l3 = substr $l, -11, 1;
$l4 = substr $l, -10, 1;
$l5 = substr $l, -9, 1;
$l6 = substr $l, -8, 1;
$l7 = substr $l, -7, 1;
$l8 = substr $l, -6, 1;
if ($l1 =~ /1|2|3|4|5|6|7|8|9|0/) {
if ($l2 =~ /1|2|3|4|5|6|7|8|9|0/) {
if ($l3 =~ /1|2|3|4|5|6|7|8|9|0/) {
if ($l4 =~ /1|2|3|4|5|6|7|8|9|0/) {
if ($l5 =~ /1|2|3|4|5|6|7|8|9|0/) {
if ($l6 =~ /1|2|3|4|5|6|7|8|9|0/) {
if ($l7 =~ /1|2|3|4|5|6|7|8|9|0/) {
if ($l8 =~ /1|2|3|4|5|6|7|8|9|0/) {
system(&quot;del $l&quot;);
}
}
}
}
}
}
}
}
}

If there is a better way (there _has_ to be) can someone let me know?
 
well, that's very ugly. Nothing to laugh at if you want my opinion. To read a single dir use the readdir() function. If you need to traverse a tree use the File::Find module. for a regex that matches 8 digits use /^\d{8}\..*$/.

Good luck!
 
Oh... and to delete a file use perl's unlink() function. I'ts wise to avoid system calls as much as possible.
 
Got it to function right in only three tries. Thank you!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top