this is a bit misleading, the variable is called "dir" (for directory) but in this section you're treating is a file. Suggest rename it as appropriate.
Would be good to also move the file processing to another method to improve readability