MoveTo (Verschiebe Programm)

  • Hallo Leute,

    nach langer Zeit mal, dachte ich, ich schreibe mal wieder ;)

    Ich habe MoveTo geschrieben, weil ich immer wieder meinen Desktop mit Daten voll haue.
    Am Ende geht es dann "Alle makieren -> Andere Festplatte -> Ordner -> \"Desktop_jahr_monat_tag\" -> Alles einfügen.

    Weil ich aber zu faul bin das immer zu machen, habe ich nun das Programm geschrieben, weil es das selber macht.

    Mögliche Variablen sind {year}, {month}, {day} und {name}.
    Wobei bei {name} in dem Fall abgefragt wird wie denn der Name des Ordners sein soll. (Habe allerdings nicht auf nur Systempfad zeichen geachtet)

    Nun denn wer sich das mal ansehen möchte oder vielleicht auch gebrauchen kann.
    Man kann sich irgendwo einen Ordner erstellen, das Programm rein ziehen. Umbenennen.
    Umbenennen muss man ggf. da dadurch das Programm "geändert" wird.
    Kann man gut für shell:SendTo verwenden. Bedeutet das die Verknpüfung dann bei "Senden an" auftaucht.


    Bsp.:
    MoveTo.exe -> MoveTo.mt
    in MoveTo.mt -> Editor -> Pfad eingeben

    DesktopBackup.exe -> DesktopBackup.mt
    in DesktopBackup.mt -> Editor -> Pfad eingeben


    Das Programm erstellt die Datei aber selber mit einem Default Pfad: AktuellerPfad\Backup_{year}_{month}_{day}\

    Quellcode/Projekt/Release als Download.

    Main

    Spoiler anzeigen
    [autoit]

    using System;using System.Collections.Generic;using System.Diagnostics;using System.IO;using System.Linq;
    namespace MoveTo{ class Program { static void Main(string[] args) { string strPath = Environment.CurrentDirectory + @"\Backup_{0}_{1}_{2}"; string strFilePath = string.Format(strPath, DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day); string strSettingPath = Process.GetCurrentProcess().ProcessName + ".mt"; if (File.Exists(strSettingPath)) { strFilePath = File.ReadAllText(strSettingPath); if (string.IsNullOrWhiteSpace(strFilePath)) { Console.WriteLine("Please edit config file!"); Console.WriteLine("\n\nPress any key to exit..."); Console.ReadKey(); return; } if (strFilePath.Contains("{name}")) { ConsoleKeyInfo consoleKeyInfo; string strTemp; do {
    Console.WriteLine("Please enter a folder name that should create.\r\nYou can use: {year},{day},{month}\r\n"); strTemp = Console.ReadLine(); Console.WriteLine("You choose {0}, is this right?\r\nY/y = yes and N/n = no or Q/q = quit\r\n", strTemp); consoleKeyInfo = Console.ReadKey(false); } while (consoleKeyInfo.Key == ConsoleKey.N || consoleKeyInfo.Key == ConsoleKey.Q); if (consoleKeyInfo.Key == ConsoleKey.Q) return;
    strFilePath = strFilePath.Replace("{name}", strTemp); } strFilePath = strFilePath.Replace("{year}", DateTime.Now.Year.ToString()); strFilePath = strFilePath.Replace("{day}", DateTime.Now.Day.ToString()); strFilePath = strFilePath.Replace("{month}", DateTime.Now.Month.ToString()); strFilePath = strFilePath[strFilePath.Length - 1] == '\\' ? strFilePath : strFilePath + ""; } else { Console.WriteLine("Config file not found. Create new one."); File.Create(strSettingPath).Close(); File.WriteAllText(strSettingPath, Environment.CurrentDirectory + @"\Backup_{year}_{month}_{day}"); Console.WriteLine("Please edit! You can use: {year},{day},{month},{name}"); Console.WriteLine("\n\nPress any key to exit..."); Console.ReadKey(); return; } List<FileBackupModel> listFileBackupModel = new List<FileBackupModel>(); List<FolderBackupModel> listFolderBackupModel = new List<FolderBackupModel>(); if (!System.IO.Directory.Exists(strFilePath)) { System.IO.Directory.CreateDirectory(strFilePath); Console.WriteLine("\r\nCreate directory...\r\n{0}", strFilePath); } Console.WriteLine();
    for (int i = 0; i < args.Length; i++) { try { if (Directory.Exists(args[i])) { DirectoryInfo directoryInfoOld = new DirectoryInfo(args[i]); DirectoryInfo directoryInfoNew = new DirectoryInfo(GetUniquePathname(strFilePath + directoryInfoOld.Name));
    listFolderBackupModel.Add(new FolderBackupModel() { NewPath = directoryInfoNew.FullName, OldPath = directoryInfoOld.FullName }); Console.WriteLine("Add directory...\r\n{0}", args[i]); } else if (System.IO.File.Exists(args[i])) { System.IO.FileInfo fileInfoOld = new System.IO.FileInfo(args[i]); System.IO.FileInfo fileInfoNew = new System.IO.FileInfo(GetUniqueFilename(strFilePath + fileInfoOld.Name));
    listFileBackupModel.Add(new FileBackupModel() { OldFile = fileInfoOld.Name, OldPath = fileInfoOld.DirectoryName, NewFile = fileInfoNew.Name, NewPath = fileInfoNew.DirectoryName }); Console.WriteLine("Add file...\r\n{0}", args[i]); } } catch (Exception ex) { Console.WriteLine(ex.Message); } } Console.WriteLine();
    #region FileBackup try { for (int i = 0; i < listFileBackupModel.Count; i++) { try { System.IO.File.Copy(listFileBackupModel[i].getOldFilepath(), listFileBackupModel[i].getNewFilepath()); Console.WriteLine("Copy file...\r\n{0}", listFileBackupModel[i].OldFile); } finally { if (System.IO.File.Exists(listFileBackupModel[i].getNewFilepath())) listFileBackupModel[i].IsCopy = true; } } Console.WriteLine(); if (listFileBackupModel.Count(p => p.IsCopy == true) != listFileBackupModel.Count) Console.WriteLine("Not all files are copied?!"); Console.WriteLine(); for (int i = 0; i < listFileBackupModel.Count; i++) { System.IO.FileInfo fileInfoOld = new System.IO.FileInfo(listFileBackupModel[i].getOldFilepath()); System.IO.FileInfo fileInfoNew = new System.IO.FileInfo(listFileBackupModel[i].getNewFilepath()); Console.WriteLine("Check file by byte...{0} old {1} <-> new {2}", fileInfoOld.Name, fileInfoOld.Length, fileInfoNew.Length); if (fileInfoNew.Length.Equals(fileInfoOld.Length)) { fileInfoOld.Delete(); Console.WriteLine("Delete old file...{0}", fileInfoOld.Name); } } } catch (Exception ex) { Console.WriteLine(); Console.WriteLine(ex.Message); } #endregion
    #region FolderBackup try { for (int i = 0; i < listFolderBackupModel.Count; i++) { try { DirectoryCopy(listFolderBackupModel[i].OldPath, listFolderBackupModel[i].NewPath,true); Console.WriteLine("Copy file...\r\n{0}", listFolderBackupModel[i].OldPath); } finally { if (System.IO.Directory.Exists(listFolderBackupModel[i].NewPath)) listFolderBackupModel[i].IsCopy = true; } } Console.WriteLine(); if (listFolderBackupModel.Count(p => p.IsCopy == true) != listFolderBackupModel.Count) Console.WriteLine("Not all directories are copied?!"); Console.WriteLine(); for (int i = 0; i < listFolderBackupModel.Count; i++) { System.IO.DirectoryInfo fileInfoOld = new System.IO.DirectoryInfo(listFolderBackupModel[i].OldPath); System.IO.DirectoryInfo fileInfoNew = new System.IO.DirectoryInfo(listFolderBackupModel[i].NewPath); Console.WriteLine("Check directory by files...{0} old {1} <-> new {2}", fileInfoOld.Name, fileInfoOld.GetFiles().Count(), fileInfoNew.GetFiles().Count()); if (fileInfoNew.GetFiles().Count().Equals(fileInfoOld.GetFiles().Count())) { fileInfoOld.Delete(true); Console.WriteLine("Delete old directory...{0}", fileInfoOld.Name); } } } catch (Exception ex) { Console.WriteLine(); Console.WriteLine(ex.Message); } #endregion
    Console.WriteLine("\n\nPress any key to exit..."); Console.ReadKey(); }
    public static string GetUniqueFilename(string fullPath) { if (!Path.IsPathRooted(fullPath)) fullPath = Path.GetFullPath(fullPath); if (File.Exists(fullPath)) { String filename = Path.GetFileName(fullPath); String path = fullPath.Substring(0, fullPath.Length - filename.Length); String filenameWOExt = Path.GetFileNameWithoutExtension(fullPath); String ext = Path.GetExtension(fullPath); int n = 1; do { fullPath = Path.Combine(path, String.Format("{0}_{1}{2}", filenameWOExt, (n++), ext)); } while (File.Exists(fullPath)); } return fullPath; }
    public static string GetUniquePathname(string fullPath) { if (!Path.IsPathRooted(fullPath)) fullPath = Path.GetFullPath(fullPath); if (Directory.Exists(fullPath)) { String filename = Path.GetFileName(fullPath); String path = fullPath.Substring(0, fullPath.Length - filename.Length); String filenameWOExt = Path.GetFileNameWithoutExtension(fullPath); String ext = Path.GetExtension(fullPath); int n = 1; do { fullPath = Path.Combine(path, String.Format("{0}_{1}{2}", filenameWOExt, (n++), ext)); } while (File.Exists(fullPath)); } return fullPath; } public static void DirectoryCopy(string sourceDirName, string destDirName, bool copySubDirs) { DirectoryInfo dir = new DirectoryInfo(sourceDirName); DirectoryInfo[] dirs = dir.GetDirectories();
    if (!dir.Exists) { throw new DirectoryNotFoundException( "Source directory does not exist or could not be found: " + sourceDirName); }
    if (!Directory.Exists(destDirName)) { Directory.CreateDirectory(destDirName); }
    FileInfo[] files = dir.GetFiles(); foreach (FileInfo file in files) { string temppath = Path.Combine(destDirName, file.Name); file.CopyTo(temppath, false); }
    if (copySubDirs) { foreach (DirectoryInfo subdir in dirs) { string temppath = Path.Combine(destDirName, subdir.Name); DirectoryCopy(subdir.FullName, temppath, copySubDirs); } } } }}

    [/autoit]

    FileModel

    Spoiler anzeigen
    [autoit]


    namespace MoveTo{ public class FileBackupModel { private string oldFile;
    public string OldFile { get { return oldFile; } set { oldFile = value; } } private string newFile;
    public string NewFile { get { return newFile; } set { newFile = value; } } private string oldPath;
    public string OldPath { get { return oldPath; } set { oldPath = value; } } private string newPath;
    public string NewPath { get { return newPath; } set { newPath = value; } }
    private bool isCopy = false;
    public bool IsCopy { get { return isCopy; } set { isCopy = value; } }
    public string getOldFilepath() { return string.Format("{0}\\{1}",oldPath,oldFile); }
    public string getNewFilepath() { return string.Format("{0}\\{1}", newPath, newFile); } }}

    [/autoit]

    FolderModel

    Spoiler anzeigen
    [autoit]


    namespace MoveTo{ public class FolderBackupModel { private string oldPath;
    public string OldPath { get { return oldPath; } set { oldPath = value; } } private string newPath;
    public string NewPath { get { return newPath; } set { newPath = value; } } private bool isCopy = false;
    public bool IsCopy { get { return isCopy; } set { isCopy = value; } } }}

    [/autoit]

    Nehme allerdings keine Verantwortung für verloren gegangene Daten.
    Prüfe schon ob Byte Anzahl oder Dateianzahl über einstimmen und dann lasse ich die sachen erst löschen.
    Bisher gab es keine Probleme!