Touch a large number of files to update the last write time
suggest changeThis 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);
}
}
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents