Nur *.txt Dateiformate kopieren



  • Man man man wie kann man sich da so schwer tun ?!

    using System;
    using System.Text;
    using System.IO;
    
    namespace Copy
    {
    
        class Program
        {
    
            //============================================================================
            //                                                                      
            //============================================================================
            private static void Verify(string[] args)
            {
                if (args.Length < 3)
                    throw new ArgumentException("Parameter: Quellpfad Zielpfad Maske");
    
                if (!Directory.Exists(args[0]))
                    throw new DirectoryNotFoundException(String.Format("{0} existiert nicht", args[0]));
            }
    
            //============================================================================
            //                                                                      
            //============================================================================
            static int Main(string[] args)
            {
                try
                {
                    Verify(args);
    
                    //FileHelper.CopyFiles(args[0], args[1], args[2], SearchOption.TopDirectoryOnly, true);
                    FileHelper.CopyFilesInfo(args[0], args[1], args[2],SearchOption.TopDirectoryOnly, true);
                    return 0;
                }
                catch (System.Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    return 20;            	
                }
            }
        }
    
        class FileHelper
        {
            #region Helper
            //============================================================================
            //                                                                      
            //============================================================================
            private static void CreateDirectoryIfNotExist(string path)
            {
                if (!Directory.Exists(path))
                    Directory.CreateDirectory(path);
            }
    
            //============================================================================
            //                                                                      
            //============================================================================
            static string FileSizeHumanReadable(long laenge)
            {
                string[] sizes = { "B", "KB", "MB", "GB" };
                int order = 0;
                while (laenge >= 1024 && order + 1 < sizes.Length)
                {
                    order++;
                    laenge /= 1024;
                }
                return String.Format("{0:0.##} {1,2}", laenge, sizes[order]);
            }
            #endregion
    
            //============================================================================
            //                                                                      
            //============================================================================
            public static void CopyFiles(string sourcePath, string targetPath, string wildCard, SearchOption recursive, bool overwrite)
            {
                CreateDirectoryIfNotExist(targetPath);
    
                string[] files = Directory.GetFiles(sourcePath, wildCard, recursive);
    
                foreach (string filename in files)
                    File.Copy(filename, Path.Combine(targetPath, Path.GetFileName(filename)), overwrite);
            }
    
            //============================================================================
            //                                                                      
            //============================================================================
            public static void CopyFilesInfo(string sourcePath, string targetPath, string wildCard, SearchOption recursive, bool overwrite)
            {
                CreateDirectoryIfNotExist(targetPath);
    
                DirectoryInfo dirInf = new DirectoryInfo(sourcePath);
                FileInfo[] fileInfos = dirInf.GetFiles(wildCard, recursive);
    
                foreach (FileInfo file in fileInfos)
                {
                    string targetFileName = Path.GetFileName(file.Name);
                    Console.WriteLine(String.Format("({1,9}) {0,-20} => {2}", file.Name, FileSizeHumanReadable(file.Length), targetPath));
                    file.CopyTo(targetFileName , overwrite);
                }
            }
        }
    }
    

    sogar das RenameFile funktz nicht ich kack hier voll ab :,(

    Gib Dir bitte mal Mühe beim Posten von Fehlermeldungen.



  • Benutze die System:IO:Path::GetExtention methode. Ist Statisch. Du kannst du hinzufügen von ".txt" nur txt files erfassen.



  • 😕


Anmelden zum Antworten