Monday, 13 January 2020

C# - Directory.GetFiles() in Created Date/Time Order



Create 3 file (.txt) in C:\File directory in different time to have 3 textfile in different created datetime as below.


using System;
using System.Collections.Generic;
using System.Text;


using System.IO;
using System.Collections;

namespace Console
{
    public partial class clsCompareFileInfo : IComparer
    {
        public int Compare(object x, object y)
        {
            int CompareRet = default(int);
            FileInfo File1;
            FileInfo File2;

            File1 = (FileInfo)x;
            File2 = (FileInfo)y;

            CompareRet = DateTime.Compare(File1.LastWriteTime, 
                                          File2.LastWriteTime);
            return CompareRet;
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            DirectoryInfo dirinfo;
            FileInfo[] allFiles;

            dirinfo = new DirectoryInfo("C:\File");
            allFiles = dirinfo.GetFiles("*.txt");

            Array.Sort(allFiles, new clsCompareFileInfo());

            foreach (FileInfo fl in allFiles)
                System.Console.WriteLine(fl.FullName.ToString());

        }
    }
}


Output

DocumentThree.txt
DocumentOne.txt
DocumentTwo.txt

Wednesday, 8 January 2020

C# - FileSystemWatcher

Example 1


using System;

using System.IO;

namespace TestFileSystemWatcher
{
    class Program
    {
        static void Main(string[] args)
        {
            string path = @"E:\FileWatcher";
            MonitorDirectory(path);
            Console.ReadKey();
        }

        private static void MonitorDirectory(string path)
        {
            FileSystemWatcher fileSystemWatcher = new FileSystemWatcher();
            fileSystemWatcher.Filter = "*.dat";
            fileSystemWatcher.Path = path;
            fileSystemWatcher.Created += FileSystemWatcher_Created;
            fileSystemWatcher.Renamed += FileSystemWatcher_Renamed;
            fileSystemWatcher.Deleted += FileSystemWatcher_Deleted;
            fileSystemWatcher.EnableRaisingEvents = true;
        }

        private static void FileSystemWatcher_Created(object sender, FileSystemEventArgs e)
        {
            Console.WriteLine("File created: {0}", e.Name);
        }

        private static void FileSystemWatcher_Renamed(object sender, FileSystemEventArgs e)
        {
            Console.WriteLine("File renamed: {0}", e.Name);
        }

        private static void FileSystemWatcher_Deleted(object sender, FileSystemEventArgs e)
        {
            Console.WriteLine("File deleted: {0}", e.Name);
        }

    }

}

Example 2



using System;
using System.IO;
using System.Security.Permissions;


namespace TestFileSystemWatcher
{
    public class Watcher
    {
        public static void Main(string[] args)
        {
            Run();
        }

        [PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
        private static void Run()
        {
            string[] args = Environment.GetCommandLineArgs();

            // If a directory is not specified, exit program.
            if (args.Length != 2)
            {
                // Display the proper way to call the program.
                Console.WriteLine("Usage: Watcher.exe (directory)");
                return;
            }

            // Create a new FileSystemWatcher and set its properties.
            using (FileSystemWatcher watcher = new FileSystemWatcher())
            {
                watcher.Path = args[1];

                // Watch for changes in LastAccess and LastWrite times, and
                // the renaming of files or directories.
                watcher.NotifyFilter = NotifyFilters.LastAccess
                                     | NotifyFilters.LastWrite
                                     | NotifyFilters.FileName
                                     | NotifyFilters.DirectoryName;

                // Only watch text files.
                watcher.Filter = "*.dat";

                // Add event handlers.
                watcher.Changed += OnChanged;
                watcher.Created += OnCreated;
                watcher.Deleted += OnDeleted;
                watcher.Renamed += OnRenamed;

                // Begin watching.
                watcher.EnableRaisingEvents = true;

                // Wait for the user to quit the program.
                Console.WriteLine("Press 'q' to quit the sample.");
                while (Console.Read() != 'q') ;
            }
        }

        // Define the event handlers.
        private static void OnChanged(object source, FileSystemEventArgs e)
        {
            // Specify what is done when a file is changed, created, or deleted.
            Console.WriteLine("File: " + e.FullPath + e.ChangeType);
        }


        private static void OnCreated(object source, FileSystemEventArgs e)
        {
            // Specify what is done when a file is changed, created, or deleted.
            Console.WriteLine("File: " + e.FullPath + e.ChangeType);
        }

        private static void OnDeleted(object source, FileSystemEventArgs e)
        {
            // Specify what is done when a file is changed, created, or deleted.
            Console.WriteLine("File: " + e.FullPath + e.ChangeType);
        }

        private static void OnRenamed(object source, RenamedEventArgs e)
        {
            // Specify what is done when a file is renamed.
            Console.WriteLine("File: " + e.OldFullPath +  "renamed to" + e.FullPath);
        }
    }
}

Monday, 6 January 2020

VBA - Determine if Cells Contain a Specific Value in Excel


Excel Function: COUNTIF()

Code with the IF statement included:

=IF(COUNTIF(A1,"*Yellow*")>0,"Value Found", "Value Not Found")


Code without the IF statement:

= COUNTIF(A1,"*Yellow*")


Find if a Value is in a Range of Cells

= COUNTIF(A1:A5,"*Yellow*")