my $path = "D:/a directory/another directory/.../a file"; my @tmp = split(/\//, $path); my $last = @tmp - 1; $path =~ /\/$tmp[$last]/; my $dir = $PREMATCH; my $fileName = $tmp[$last];