Updated script to work with folders