Image

Imageamoe wrote in Imagedevelopers 😡aggravated

Listens: Fear Factory - Shock

Perl problem

Any Perl gurus here? If there are, can you guys please help to explain why this subroutine doesn't work. Its task is to take a full filename in as a parameter (running on Windows, so something like "C:/Temp/foo.bar"), rip it apart, read the directory it says it's in, see if there's anything else in the directory with the same name, and if there is to add a random number between the name and suffix (foo95467.bar) and return it. I've debugged it as much as I can, but it still seems to fail horribly. Anyone help?

sub scan_dir
{
my $filename = $_[0];
(my $short_name, my $path) = (fileparse($filename))[0,1];
$_ = $short_name;
/(\..*)$/;
my $suffix = $1;
opendir(DIR, $path);
while (my $file_found = readdir(DIR))
{
if ($file_found eq $short_name)
{
my $random = int(rand(99999));
my $new_name = $path.$short_name.$random.$suffix;
&scan_dir($newname);
}
}
return $filename;
}