modified script as a function and create resultset as txt and csv