The
opendir/readdir combination return to the user filenames without the path to the files (your
dirname variable). So that, if your program is not launched from that location, or if you did not
chdir to it, your program is not going to work unless you prefix
$file and
$new with the path.
Note that the glob built-in will give you filenames with the path.