I always have a problem in one of my automation framework where the log file which it creates exceed the size to many MB (say 100MB) and I had a hard time to open the file in text editor and also since there will be more log files in course of time, I should go manually and delete those files in regular basis to run my Virtual machines without any problem of low disk space atleast.
I can do this in two way, one is manually going to the machine and deleting it, which is way to straight forward, but still as an automation guy, I like to think a little smart than everybody.
Hence I wrote a very simple
powershell script, which performs the above said operation in one single line.
I can also call it in Windows inbuilt scheduler, to check on a daily basis and delete the file in folder and keep my folders tidy
Here is how the script looks like, in the below given script I am first filtering all the file which has extension as
log and then searching for the file if its size is greater than
2 MB and then deleting the files.
Let’s first check the code if its size is greater than 2 MB
Get-ChildItem -File e:\logs -Filter "*.log" | where length -gt 2000000
Directory: E:\logs
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 8/18/2013 6:54 PM 30625926 20130410.log
-a--- 8/18/2013 6:53 PM 20417284 20130411.log
-a--- 8/18/2013 6:53 PM 9445482 20130412.log
Let’s delete all the files by adding one more command Remove-Item
Get-ChildItem -File e:\logs | where length -gt 2000000 | Remove-Item -Force
Now just check the files once again in the directory
Directory: E:\logs
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 8/18/2013 6:46 PM 19632 20130415.log
As you can see the above code works !!!
This way I can resolve my long standing problem of low disk space.
Thanks,
Karthik KK