Requered this Dll: Ionic.Zip.dll
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Ionic.Zip;
using System.Threading;
namespace segregation
{
public partial class Move_Files : Form
{
public Move_Files()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void btnMove_Click(object sender, EventArgs e)
{
string path = txtPath.Text.Trim();
string pathToMove = txtPathMove.Text.Trim();
DirectoryInfo di = new DirectoryInfo(path);
string[] files = Directory.GetFiles(path);
//The following piece of code is for arranginging raw data files to respective datewise directories
for (int i = 0; i < files.Length; i++)
{
string[] arr = files[i].Split('_');
arr[3] = arr[3].Substring(0, 8);
arr[3] = arr[3].Insert(4, "-");
arr[3] = arr[3].Insert(7, "-");
string path2 = pathToMove + "\\" + arr[3];
string path3 = pathToMove + "\\" + arr[3] + "\\" + arr[1] + "_" + arr[2] + "_" + arr[3] + "_" + arr[4];
if (!Directory.Exists(path2))
{
Directory.CreateDirectory(path2);
try
{
File.Move(files[i], path3);
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
else
{
try
{
File.Move(files[i], path3);
}
catch (Exception ex1)
{
MessageBox.Show(ex1.ToString());
}
}
}
//The following piece of code is for moving respective organized datewise folders to monthly organized folders
DirectoryInfo di1 = new DirectoryInfo(pathToMove);
string[] files1 = Directory.GetDirectories(pathToMove);
int monthNo = 0;
string monthName = string.Empty;
string path4 = string.Empty;
string path5 = string.Empty;
for (int j = 0; j < files1.Length; j++)
{
string[] arr1 = files1[j].Split('\\', '-');
if(arr1.Length==5)//One folder
{
monthNo = Int32.Parse(arr1[3]);
monthName = CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(monthNo);
path4 = pathToMove + "\\" + monthName + " " + arr1[2];
path5 = path4 + "\\" + arr1[2] + "-" + arr1[3] + "-" + arr1[4];
}
else if(arr1.Length==6)//Two folder
{
monthNo = Int32.Parse(arr1[4]);
monthName = CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(monthNo);
path4 = pathToMove + "\\" + monthName + " " + arr1[3];
path5 = path4 + "\\" + arr1[3] + "-" + arr1[4] + "-" + arr1[5];
}
else if (arr1.Length == 7)//Three folder
{
monthNo = Int32.Parse(arr1[5]);
monthName = CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(monthNo);
path4 = pathToMove + "\\" + monthName + " " + arr1[4];
path5 = path4 + "\\" + arr1[4] + "-" + arr1[5] + "-" + arr1[6];
}
else if (arr1.Length == 8)//Four folder
{
monthNo = Int32.Parse(arr1[6]);
monthName = CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(monthNo);
path4 = pathToMove + "\\" + monthName + " " + arr1[5];
path5 = path4 + "\\" + arr1[5] + "-" + arr1[6] + "-" + arr1[7];
}
else if (arr1.Length == 9)//Five folder
{
monthNo = Int32.Parse(arr1[7]);
monthName = CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(monthNo);
path4 = pathToMove + "\\" + monthName + " " + arr1[6];
path5 = path4 + "\\" + arr1[6] + "-" + arr1[7] + "-" + arr1[8];
}
else if (arr1.Length == 10)//Six folder
{
monthNo = Int32.Parse(arr1[8]);
monthName = CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(monthNo);
path4 = pathToMove + "\\" + monthName + " " + arr1[7];
path5 = path4 + "\\" + arr1[7] + "-" + arr1[8] + "-" + arr1[9];
}
else if (arr1.Length == 11)//Seven folder
{
monthNo = Int32.Parse(arr1[9]);
monthName = CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(monthNo);
path4 = pathToMove + "\\" + monthName + " " + arr1[8];
path5 = path4 + "\\" + arr1[8] + "-" + arr1[9] + "-" + arr1[10];
}
else
{
MessageBox.Show("Too Long Path!");
}
if (!Directory.Exists(path4))
{
Directory.CreateDirectory(path4);
try
{
Directory.Move(files1[j], path5);
}
catch (Exception ex2)
{
MessageBox.Show(ex2.ToString());
}
}
else
{
try
{
Directory.Move(files1[j], path5);
}
catch (Exception ex3)
{
MessageBox.Show(ex3.ToString());
}
}
}
MessageBox.Show("Congratulations! Your all files are moved successfully");
}
private void btnZip_Click(object sender, EventArgs e)
{
string path6 = txtPathMove.Text.Trim();
DirectoryInfo di3 = new DirectoryInfo(path6);
string[] files2 = Directory.GetDirectories(path6);
//The following code is for zipping monthly data folders, subfolders and files.
for (int k = 0; k < files2.Length; k++)
{
string path7 = files2[k];
string ZipFileToCreate = files2[k] + ".zip";
using (ZipFile zip = new ZipFile())
{
try
{
zip.StatusMessageTextWriter = System.Console.Out;
zip.AddDirectory(path7);
zip.Save(ZipFileToCreate);
}
catch (Exception ex6)
{
MessageBox.Show(ex6.ToString());
}
}
Directory.Delete(files2[k], true);
}
MessageBox.Show("Congratulations! Your all files are moved and zipped successfully");
}
private void btnChooseDestPath_Click(object sender, EventArgs e)
{
DialogResult result = ChooseDestPath.ShowDialog();
if (result == DialogResult.OK)
{
txtPathMove.Text = ChooseDestPath.SelectedPath;
}
}
private void btnChooseSourcePath_Click(object sender, EventArgs e)
{
DialogResult result = ChooseSourcePath.ShowDialog();
if (result == DialogResult.OK)
{
txtPath.Text = ChooseSourcePath.SelectedPath;
}
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Ionic.Zip;
using System.Threading;
namespace segregation
{
public partial class Move_Files : Form
{
public Move_Files()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void btnMove_Click(object sender, EventArgs e)
{
string path = txtPath.Text.Trim();
string pathToMove = txtPathMove.Text.Trim();
DirectoryInfo di = new DirectoryInfo(path);
string[] files = Directory.GetFiles(path);
//The following piece of code is for arranginging raw data files to respective datewise directories
for (int i = 0; i < files.Length; i++)
{
string[] arr = files[i].Split('_');
arr[3] = arr[3].Substring(0, 8);
arr[3] = arr[3].Insert(4, "-");
arr[3] = arr[3].Insert(7, "-");
string path2 = pathToMove + "\\" + arr[3];
string path3 = pathToMove + "\\" + arr[3] + "\\" + arr[1] + "_" + arr[2] + "_" + arr[3] + "_" + arr[4];
if (!Directory.Exists(path2))
{
Directory.CreateDirectory(path2);
try
{
File.Move(files[i], path3);
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
else
{
try
{
File.Move(files[i], path3);
}
catch (Exception ex1)
{
MessageBox.Show(ex1.ToString());
}
}
}
//The following piece of code is for moving respective organized datewise folders to monthly organized folders
DirectoryInfo di1 = new DirectoryInfo(pathToMove);
string[] files1 = Directory.GetDirectories(pathToMove);
int monthNo = 0;
string monthName = string.Empty;
string path4 = string.Empty;
string path5 = string.Empty;
for (int j = 0; j < files1.Length; j++)
{
string[] arr1 = files1[j].Split('\\', '-');
if(arr1.Length==5)//One folder
{
monthNo = Int32.Parse(arr1[3]);
monthName = CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(monthNo);
path4 = pathToMove + "\\" + monthName + " " + arr1[2];
path5 = path4 + "\\" + arr1[2] + "-" + arr1[3] + "-" + arr1[4];
}
else if(arr1.Length==6)//Two folder
{
monthNo = Int32.Parse(arr1[4]);
monthName = CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(monthNo);
path4 = pathToMove + "\\" + monthName + " " + arr1[3];
path5 = path4 + "\\" + arr1[3] + "-" + arr1[4] + "-" + arr1[5];
}
else if (arr1.Length == 7)//Three folder
{
monthNo = Int32.Parse(arr1[5]);
monthName = CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(monthNo);
path4 = pathToMove + "\\" + monthName + " " + arr1[4];
path5 = path4 + "\\" + arr1[4] + "-" + arr1[5] + "-" + arr1[6];
}
else if (arr1.Length == 8)//Four folder
{
monthNo = Int32.Parse(arr1[6]);
monthName = CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(monthNo);
path4 = pathToMove + "\\" + monthName + " " + arr1[5];
path5 = path4 + "\\" + arr1[5] + "-" + arr1[6] + "-" + arr1[7];
}
else if (arr1.Length == 9)//Five folder
{
monthNo = Int32.Parse(arr1[7]);
monthName = CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(monthNo);
path4 = pathToMove + "\\" + monthName + " " + arr1[6];
path5 = path4 + "\\" + arr1[6] + "-" + arr1[7] + "-" + arr1[8];
}
else if (arr1.Length == 10)//Six folder
{
monthNo = Int32.Parse(arr1[8]);
monthName = CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(monthNo);
path4 = pathToMove + "\\" + monthName + " " + arr1[7];
path5 = path4 + "\\" + arr1[7] + "-" + arr1[8] + "-" + arr1[9];
}
else if (arr1.Length == 11)//Seven folder
{
monthNo = Int32.Parse(arr1[9]);
monthName = CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(monthNo);
path4 = pathToMove + "\\" + monthName + " " + arr1[8];
path5 = path4 + "\\" + arr1[8] + "-" + arr1[9] + "-" + arr1[10];
}
else
{
MessageBox.Show("Too Long Path!");
}
if (!Directory.Exists(path4))
{
Directory.CreateDirectory(path4);
try
{
Directory.Move(files1[j], path5);
}
catch (Exception ex2)
{
MessageBox.Show(ex2.ToString());
}
}
else
{
try
{
Directory.Move(files1[j], path5);
}
catch (Exception ex3)
{
MessageBox.Show(ex3.ToString());
}
}
}
MessageBox.Show("Congratulations! Your all files are moved successfully");
}
private void btnZip_Click(object sender, EventArgs e)
{
string path6 = txtPathMove.Text.Trim();
DirectoryInfo di3 = new DirectoryInfo(path6);
string[] files2 = Directory.GetDirectories(path6);
//The following code is for zipping monthly data folders, subfolders and files.
for (int k = 0; k < files2.Length; k++)
{
string path7 = files2[k];
string ZipFileToCreate = files2[k] + ".zip";
using (ZipFile zip = new ZipFile())
{
try
{
zip.StatusMessageTextWriter = System.Console.Out;
zip.AddDirectory(path7);
zip.Save(ZipFileToCreate);
}
catch (Exception ex6)
{
MessageBox.Show(ex6.ToString());
}
}
Directory.Delete(files2[k], true);
}
MessageBox.Show("Congratulations! Your all files are moved and zipped successfully");
}
private void btnChooseDestPath_Click(object sender, EventArgs e)
{
DialogResult result = ChooseDestPath.ShowDialog();
if (result == DialogResult.OK)
{
txtPathMove.Text = ChooseDestPath.SelectedPath;
}
}
private void btnChooseSourcePath_Click(object sender, EventArgs e)
{
DialogResult result = ChooseSourcePath.ShowDialog();
if (result == DialogResult.OK)
{
txtPath.Text = ChooseSourcePath.SelectedPath;
}
}
}
}
No comments:
Post a Comment