Perl: Finding files with extension in multiple dirs

Soldato
Joined
5 Dec 2003
Posts
2,716
Location
Glasgow
Hi, I have about 10,000 directories to go through and I want to put into a database which ones contain any files with, for example, an .mp3 extension.

So far I have a for loop going through each directory and have tried something like this:

$fileExtension = glob(*.mp3);

if (defined ($fileExtension)) {
gotExtension = true;
}

#update DB here

which doesn't work but something like that would be my ideal way to do it and I don't think I'm too far off, maybe a problem with the glob and regex if anyone can help?
 
if mp3 {
get dir
put dir in db
gotExtension = true;
}
if( gotExtension ) move to the next folder (if there is a next)
else continue to search folder

maybe you could post more of your script so we can help better?

How would I code "if mp3" in Perl?

I'm more looking for help in terms of actual Perl code than pseudocode as i'm sure the language will have its own unique capability of doing what I want here.

The loop has to operate like in my original post as other stuff occurs further up in it.

Essentially the loop goes through each entry in the database which is the folder name, it checks to see if one file which I know the name of exists in each folder and puts a 1 in the field for that file in the database. It's easy when I know the actual filename though like so

$filename = "file1.txt";

if (-e $filename){
my $field = true;
}

That code is from memory as the script is in my work.
 
Back
Top Bottom