Touch a large number of files to update the last write time

suggest change

This example updates the last write time of many files. Directory.EnumerateFiles yields paths instead of building the full list the way Directory.GetFiles does. Both overloads used here were added in .NET Framework 4. The search pattern defaults to "*.*". Pass SearchOption.AllDirectories to include subdirectories.

public static void Touch(string path,
                         string searchPattern = "*.*",
                         SearchOption options = SearchOption.TopDirectoryOnly)
{
    var now = DateTime.Now;

    foreach (var filePath in Directory.EnumerateFiles(path, searchPattern, options))
    {
        File.SetLastWriteTime(filePath, now);
    }
}

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents