@manhng

Welcome to my blog!

Loop all files in a folder

July 26, 2018 15:37

Loop all files in a folder (edit)

Duyệt tất cả các files với điều kiện lọc là các files có đuôi là *.cs, *.cshtml, *.html

http://www.beansoftware.com/ASP.NET-FAQ/Multiple-Filters-Directory.GetFiles-Method.aspx

https://stackoverflow.com/questions/3527203/getfiles-with-multiple-extensions/7097562

public static class DirectoryInfoExt
{
    public static IEnumerable GetFilesByExtensions(this DirectoryInfo dir, params string[] extensions)
    {
        if (extensions == null)
            throw new ArgumentNullException("extensions");
        IEnumerable files = dir.EnumerateFiles("*.*", SearchOption.AllDirectories);
        return files.Where(f => extensions.Contains(f.Extension));
    }
}

class Program
{
    ///
    /// Read more: http://manhng.com/blog/ma-hoa-du-lieu/
    ///
    ///
    private static void Main(string[] args)
    {
        //https://stackoverflow.com/questions/5750203/how-to-write-unicode-characters-to-the-console
        System.Console.OutputEncoding = System.Text.Encoding.Unicode;

        //MaHoaBatDoiXung();

        //EncodeDecodeHTMLUsingSystemNetWebUtility();

        //EncodeDecodeHTMLUsingSystemWebHttpUtility();

        //EncodeDecodeCharacter();

        //SaveAllFilesUTF8();

        SaveAllFilesEncodingUTF8();
    }

    ///
    /// Returns file names from given folder that comply to given filters
    ///
    ///Folder with files to retrieve
    ///Multiple file filters separated by | character
    ///File.IO.SearchOption,
    /// could be AllDirectories or TopDirectoryOnly
    /// Array of FileInfo objects that presents collection of file names that
    /// meet given filter
    public static string[] getFiles(string SourceFolder, string Filter,
        System.IO.SearchOption searchOption)
    {
        // ArrayList will hold all file names
        ArrayList alFiles = new ArrayList();

        // Create an array of filter string
        string[] MultipleFilters = Filter.Split('|');

        // for each filter find mathing file names
        foreach (string FileFilter in MultipleFilters)
        {
            // add found file names to array list
            alFiles.AddRange(Directory.GetFiles(SourceFolder, FileFilter, searchOption));
        }

        // returns string array of relevant file names
        return (string[])alFiles.ToArray(typeof(string));
    }

    private static void SaveAllFilesEncodingUTF8()
    {
        var folderPath = @"D:\Prj\VASJ.BI\VASJ.BI\VASJ.BI";
        var di = new DirectoryInfo(folderPath);
        string[] sFiles = getFiles(folderPath, "*.cs|*.cshtml|*.html", SearchOption.AllDirectories);
        foreach (var sFile in sFiles)
        {
            var f = new FileInfo(sFile);
            string s = File.ReadAllText(f.FullName);
            File.WriteAllText(f.FullName, s, Encoding.UTF8);
        }
    }

    private static void SaveAllFilesUTF8()
    {
        var di = new DirectoryInfo(@"D:\Prj\VASJ.BI\VASJ.BI\VASJ.BI");
        foreach (var f in di.GetFilesByExtensions(new string[] { ".cs", ".cshtml", ".html" }))
        {
            string s = File.ReadAllText(f.FullName);
            File.WriteAllText(f.FullName, s, Encoding.UTF8);
        }
    }
}

Categories

Recent posts