Useful DOS commands I’ve come across:
Write to file
[cmd] > [file]
The result of the command on the left will be written to the file on the right. The file will be created if it doesn’t already exist, and overwritten if it does.
echo abc > c:\temp\myfile.txt
Writes the text ‘abc’ to a new file called c:\temp\myfile.txt
Append to file
[cmd] >> [file]
The result of the command on the left will be appended to the file on the right. If the file doesn’t exist it will be created.
echo abd >> c:\temp\myfile.txt
Appends the the text ‘abc’ to the file c:\temp\myfile.txt
The other week a business I was working with experienced “a perfect storm”. A production web-server ran out of disk space due to the build-up of IIS logs. Normally the standard monitoring tools used by the organisation would have picked this up, but it turns out they hadn’t been configured properly and so the alerts were being emailed to the wrong team! After various “it shouldn’t have been possible” conference calls, we decided to automate the deletion of log files for IIS (and for another system) after seven days. I figured this functionality would be offered out of the box by IIS. I was wrong!
Instead, I found this awesome post on Stack Overflow that answered the question using the forfiles command.
I create a daily schedule containing the following command to delete IIS logs:
forfiles /p "C:\inetpub\logs\LogFiles" /s /m "*.*" /c "cmd /c Del @path" /d -7
…and this to delete my other log files:
forfiles /p "C:\application_name\logs\" /m "application_name.log*.log" /c "cmd /c Del @path" /d -7
Note that I dropped the /s in the second command because I know that my application logs are all in a specific folder, and the file name pattern I’m using matches roll-over files created by log4net.
This works like a dream.