Monday, January 31, 2011

Deleting files past a certain amount of days

Recently I have come across a need to create a console tool to delete files after X amount of days and found that there wasn't any good tutorials or examples out for this language. So I decided to write my own version. This one currently does not support entering a target directory. If I get enough attention on this I will make one. Also if you like this tool give me a shout out in the comments.


Purpose:
Delete files modifed after X amount of days.

Code:
using System;
using System.IO;

//  Developed by PhyberOptic
//  PhyberOptic.blogger.com
//
//  This program is free software: you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.
//  
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//  
//  You should have received a copy of the GNU General Public License
//  along with this program.  If not, see .

class Program
{
    public static bool delete = false;
    public static int number = 0;
    static Int32 Main(string[] args)
    {
        if (args.Length < 2)
        {
            Console.WriteLine("Checks last file modification times greater than (#) days\r\nand if found deletes if set to true.\r\n\r\nUsage: {0} [days] [delete:true|false]\r\n\r\nExample:{0} 7 true", Environment.GetCommandLineArgs()[0]);//, Environment.GetCommandLineArgs()[0]);
            return 0;
        }
        if(args[1] == "true")
        {
            delete = true;
        }

        string[] files = Directory.GetFiles(Environment.CurrentDirectory, "*.*",  SearchOption.AllDirectories);

        // Display all the files.
        foreach (string file in files)
        {
            FileInfo fileInfo = new FileInfo(file);
            DateTime dt = fileInfo.LastWriteTime;
            DateTime dtToday = System.DateTime.Now;
            if (DateDiff(dt, dtToday) >= Convert.ToInt32(args[0]))
            {
                if (delete)
                {
                    File.Delete(file);
                    Console.WriteLine("{0}: Deleted", file);
                }
                else
                {
                    Console.WriteLine(file);
                }
            }
        }
        return 0;
    }

    public static int DateDiff(DateTime StartDate, DateTime EndDate)
    {
        int NumDaysDiff;
        TimeSpan ts = EndDate.Subtract(StartDate);
        NumDaysDiff = ts.Days;
        return NumDaysDiff;
    }
    
}
Download here: Here

Anti Spam Campaign

Saturday, January 8, 2011

PS3Proxy Tool

Lately, I have been developing a new proxy tool mainly for backup, Debugging, etc. This tool can also be used for the playstation as I have encountered and works better and has more features then the previous versions like CF3B5 or 3r1c. Allowing options such as save log, Auto-Download files, Configuration settings, Auto-select Network IP address(No more ipconfig copy&paste!) just to start out with.

Tell me what you think about this.

Sample




An elephant