Purpose: To identify files over 40 GB that are in the filepath sprcified and its subdirectories. We had an issue where backups would fail sporadically with an error indicating they were out of space. However, when reviewing the disk in the morning, there was clearly enough space for a backup to complete. The intention with this is to schedule it to run hourly to identify a new large file that shows up that would hinder the backups completion. Put the results of DIR in a table for easy querying.
Alternative Usage: Modify the code to be able to pass in a size value to find out the largest files for a given path. How often have you asked the question, what is using up all the space on my D drive? This would allow you to find that out.
PreRequisites: Create a database named utilities or else change the database name in the script. Create a table named LargeFiles in Utilities or else rename it. The script for this table creation is commented out in the header. After compiling and executing, do a select on utilities.dbo.largefiles to see what it found.
Cautions: see various other articles on the usage of xp_cmdshell
I welcome enhancement suggestions and hope this will start you on the path to accomplishing what you need to by putting the results of the DIR command into a table.