I am having a little difficulty interpreting your question, so if this answer is off point I apologize. I think what is happening is that since you are accessing the $dir variable outside of the conditional loop, you are caching a value of $dir from a previous traverse of the loop. Perhaps this could be fixed with something along the lines of
if ($fsline =~ /(.:\\.*)/){
$dir = $1;
print "$dir\n"; #works fine here
} else {
next;
}