use collectFirst instand of a var and iterate

agree, this code is sound reasonable in the case listFilesSafely were changed.