in reply to Avoiding duplicate filenames

You could look for all filenames that match the current filename maybe?

select filename from $database_table where filename like '$filename%.html' order by filename desc

If your database allows you regular expressions, you can even match the filename plus digits at the end.

Also, let me recommend that you rewrite your Do_SQL routine as a routine that takes the SQL as a parameter?

A first implementation could be:

sub Do_SQL2 { my( $sql ) = @_; $sth = $dbh->prepare( $sql ); $sth->execute(); }

Then you can call it in a saner way without using global variables:

Do_SQL2($SQL);