in reply to Script works fine in cmd but doesn't print to txt

What's going wrong is that the open mode > clobbers the output file. Although you could use >> to append to the file, opening it on every iteration of the loop is pretty inefficient, and so Fletch's suggestion of moving the open outside of the loop is better.

You should also definitely always Use strict and warnings, and apply the advice from "open" Best Practices to all of your opens.