A while loop instead of a foreach is preferred in method 1.
The foreach will store all matching files in a list, which
could take lots of memory if there are many files. The while
method doesn't store an intermediate list. (List vs scalar
context behaviour of <>).