This is windows application code. It will auto delete all the folders in the selected path but exclude those list in the defined text file. Before it delete, it will make a copy to backup folder. However, backup folder will be deleted upon this application start.

using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace DSQ
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();

// Delete backup folder if exist
if (Directory.Exists(”c:\\Backup”))
Directory.Delete(”c:\\Backup”, true);

// Run delete function only if RequiredDirectory.txt found
if (File.Exists(”c:\\test2\\RequiredDirectory.txt”))

{
// Get all the subdirectory
string[] subdirectoryEntries = Directory.GetDirectories(”c:\\test2″);
// Scan trough all the subdirectory
foreach (string subdirectory in subdirectoryEntries)
{
int Found = 0;
// Load RequiredDirectory.txt
using (StreamReader sr = new StreamReader(”c:\\test2\
\RequiredDirectory.txt”))
{
String line;
// Compare subdirectory with lists of directory in RequiredDirectory.txt
while ((line = sr.ReadLine()) != null)
{
// if match, increase Found counter
if (line.CompareTo(subdirectory).ToString() == “0″)
Found++;
}

}
// Do nothing if found
if (Found > 0)
;
// Copy a backup and delete it
else
{
string backup = “C:\\backup\\” + subdirectory.Remove(0, 9);
copyDirectory(subdirectory, backup);
Directory.Delete(subdirectory, true);
}
}
}
else
{
textBox1.Text = “RequiredDirectory.txt not found”;
}

Environment.Exit(0);
}

// Copy directory function
public static void copyDirectory(string Src, string Dst)
{
String[] Files;

if (Dst[Dst.Length - 1] != Path.DirectorySeparatorChar)
Dst += Path.DirectorySeparatorChar;
if (!Directory.Exists(Dst)) Directory.CreateDirectory(Dst);
Files = Directory.GetFileSystemEntries(Src);
foreach (string Element in Files)
{
// Sub directories
if (Directory.Exists(Element))
copyDirectory(Element, Dst + Path.GetFileName(Element));
// Files in directory
else
File.Copy(Element, Dst + Path.GetFileName(Element), true);
}
}
}
}

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • StumbleUpon
  • Technorati
  • Reddit
  • Facebook
  • Google
  • Live
  • YahooMyWeb
  • Furl
  • Slashdot
  • Spurl
  • Mixx
  • BlinkList
  • Bumpzee