1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
|
using System;
using System.IO;
using System.Globalization;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public List<string> fichiers = new List<string>();
public Form1()
{
InitializeComponent();
label4.Text = "";
}
private void button3_Click(object sender, EventArgs e)
{
progressBar1.Maximum = 100;
progressBar1.Minimum = 0;
progressBar1.Step = 1;
label4.Text = "";
//DirectoryInfo directoryInfo1 = new DirectoryInfo(textBox1.Text);
//FileInfo[] files = directoryInfo1.GetFiles();
//int test = files.Count();
//if (test != 0)
//{
progressBar1.Cursor.Equals(50);
//}
bool exists = System.IO.Directory.Exists(textBox1.Text);
if (!exists)
{
MessageBox.Show("Le répertoire de destination n'existe pas");
}
else {
bool exists2 = System.IO.Directory.Exists(textBox2.Text);
if (!exists2)
{
MessageBox.Show("Le répertoire à analyser n'existe pas");
}
else
{
//DirectoryInfo directoryInfo1 = new DirectoryInfo(textBox1.Text);
//FileInfo[] files = directoryInfo1.GetFiles();
//FileInfo[] files = directoryInfo1.GetFiles();
if (Directory.EnumerateFileSystemEntries(textBox1.Text).Any())
{
MessageBox.Show("Le répertoire de destination doit être vide!");
}
else
{
if (textBox1.Text != "" && textBox2.Text != "")
{
fichiers.Clear();
lancerAnalyse(textBox2.Text);
contenuTableau();
createFiles();
label4.Text = "L'analyse est terminée.";
MessageBox.Show("L'analyse est terminée.");
}
else
{
MessageBox.Show("Vous n'avez pas indiqué les chemins pour l'opérations");
}
}
}
}
}
private void button2_Click(object sender, EventArgs e)
{
var FolderBrowserDialog1 = new FolderBrowserDialog();
if (FolderBrowserDialog1.ShowDialog(this) == DialogResult.OK)
{
textBox2.Text = FolderBrowserDialog1.SelectedPath;
}
}
private void button1_Click(object sender, EventArgs e)
{
var FolderBrowserDialog1 = new FolderBrowserDialog();
if (FolderBrowserDialog1.ShowDialog(this) == DialogResult.OK)
{
textBox1.Text = FolderBrowserDialog1.SelectedPath;
}
}
public void lancerAnalyse(string Chemin1)
{
DirectoryInfo directoryInfo1 = new DirectoryInfo(Chemin1);
label4.Text = Chemin1;
Application.DoEvents();
FileInfo[] files = directoryInfo1.GetFiles();
int y = files.Count();
int i = 0;
foreach (FileInfo file in files)
{
i++;
fichiers.Add(file.FullName);
if (y != 0)
{
decimal a = i;
decimal b = y;
decimal value = Convert.ToDecimal(a / b * 100);
int value2 = Convert.ToInt32(value);
progressBar1.Value = value2;
}
//int file1byte;
//int file2byte;
//FileStream fs1;
//FileStream fs2;
//do
//{
// // Read one byte from each file.
// file1byte = fs1.ReadByte();
// file2byte = fs2.ReadByte();
//}
//while ((file1byte == file2byte) && (file1byte != -1));
}
//Recursivite, la fonction s'appelle elle même pour faire la même ôpération dans les sous-dossiers
DirectoryInfo[] dirs = directoryInfo1.GetDirectories();
foreach (DirectoryInfo dir in dirs)
{
lancerAnalyse(dir.FullName);
}
}
public void contenuTableau()
{
//List<int> listTEMP = new List<int>();
for (int i = 0; i<fichiers.Count; i++ )
{
for (int t = i+1; t<fichiers.Count; t++){
string testi = fichiers[i];
string testt = fichiers[t];
if (fichiers[t] != "0" && fichiers[i] != "0")
{
if (FileCompare(fichiers[i], fichiers[t]))
{
fichiers[t] = "0";
}
}
//int file1byte;
//int file2byte;
//MemoryStream stream = new MemoryStream();
//MemoryStream stream2 = new MemoryStream();
//StreamWriter writer2 = new StreamWriter(stream2);
//StreamWriter writer = new StreamWriter(stream);
//writer.Write(fichiers[t]);
//writer2.Write(fichiers[i]);
//do
//{
// //Read one byte from each file.
// file1byte = stream.ReadByte();
// file2byte = stream2.ReadByte();
//}
//while ((file1byte == file2byte) && (file1byte != -1));
//if (writer == writer2)
//{
// fichiers.RemoveAt(t);
//}
//stream.Close();
//stream2.Close();
}
decimal a = i;
decimal b = fichiers.Count;
decimal value = Convert.ToDecimal(a/b*100);
int value2 = Convert.ToInt32(value);
label4.Text = "Localisation des doublons";
progressBar1.Value = value2;
}
fichiers.RemoveAll(item => item == "0");
//for (int i = 0; i <= fichiers.Count; i++ )
//{
// if (fichiers[i] == "0")
// {
// fichiers.RemoveAt(i);
// }
//}
}
public void createFiles()
{
progressBar1.Increment(0);
for (int i = 0; i < fichiers.Count; i++)
{
//MessageBox.Show(fichiers[i]);
string result = Path.GetFileName(fichiers[i]);
string nomSansExtension = Path.GetFileNameWithoutExtension(fichiers[i]);
string extension = Path.GetExtension(fichiers[i]);
FileInfo fi = new FileInfo(fichiers[i]);
DateTime date = fi.LastAccessTime;
//MessageBox.Show(date.ToString());
//MessageBox.Show(date.ToString("dd-MMMM-yyyy"));
bool exists = System.IO.Directory.Exists(textBox1.Text + "\\" + date.ToString("dd-MMMM-yyyy"));
if (!exists)
System.IO.Directory.CreateDirectory(textBox1.Text + "\\" + date.ToString("dd-MMMM-yyyy"));
int y = 0;
bool exists2;
do
{
y++;
} while (exists2 = System.IO.File.Exists(textBox1.Text + "\\" + date.ToString("dd-MMMM-yyyy") + "\\" + nomSansExtension + " - " + y + extension));
System.IO.File.Copy(fichiers[i], textBox1.Text + "\\" + date.ToString("dd-MMMM-yyyy") + "\\" + nomSansExtension + " - " + y + extension);
//bool exists = System.IO.Directory.Exists(textBox1.Text + "\\" + date.ToString("f", CultureInfo.CreateSpecificCulture("fr-FR")));
//if (!exists)
// System.IO.Directory.CreateDirectory(textBox1.Text + "\\" + date.ToString("f", CultureInfo.CreateSpecificCulture("fr-FR")));
//System.IO.File.Copy(fichiers[i], textBox1.Text + "\\" + date.ToString("f", CultureInfo.CreateSpecificCulture("fr-FR")) + "\\" + result);
decimal a = i;
decimal b = fichiers.Count;
decimal value = Convert.ToDecimal(a / b * 100);
int value2 = Convert.ToInt32(value);
label4.Text = "Ecriture des fichiers dans le dossier de destination";
progressBar1.Increment(+value2);
}
}
private bool FileCompare(string file1, string file2)
{
int file1byte;
int file2byte;
FileStream fs1;
FileStream fs2;
// Determine if the same file was referenced two times.
if (file1 == file2)
{
// Return true to indicate that the files are the same.
return true;
}
// Open the two files.
fs1 = new FileStream(file1, FileMode.Open);
fs2 = new FileStream(file2, FileMode.Open);
// Check the file sizes. If they are not the same, the files
// are not the same.
if (fs1.Length != fs2.Length)
{
// Close the file
fs1.Close();
fs2.Close();
// Return false to indicate files are different
return false;
}
// Read and compare a byte from each file until either a
// non-matching set of bytes is found or until the end of
// file1 is reached.
do
{
// Read one byte from each file.
file1byte = fs1.ReadByte();
file2byte = fs2.ReadByte();
}
while ((file1byte == file2byte) && (file1byte != -1));
// Close the files.
fs1.Close();
fs2.Close();
// Return the success of the comparison. "file1byte" is
// equal to "file2byte" at this point only if the files are
// the same.
return ((file1byte - file2byte) == 0);
}
}
} |
Partager