I copied a folder created using windows 8 file history from a drive to a computer running windows 7. Since I couldn't use a restore function all the files have time stamps like 01 Disappearing World (2015_01_09 04_57_38 UTC).mp3. Is there an easy way to remove these time stamps?
3 Answers
Here's a Powershell version
Get-ChildItem -Recurse | Where-Object { $_.Name -match " ?\(\d\d\d\d_\d\d_\d\d \d\d_\d\d_\d\d UTC\)" } | Rename-Item -NewName { $_.Name -replace " ?\(\d\d\d\d_\d\d_\d\d \d\d_\d\d_\d\d UTC\)", ""} Here's RainingChain's script tweaked a little and converted to a full C# console app. Just compile and enjoy, tested in my own system.
Added functions:
Subdirectory scan and exception swallow when access is denied.
Operates only on timestamped files, this avoids affecting other data.
If timestamped file has another copy without the timestamp in the same folder, it deletes the timestamped one. This is for example when a program has been reinstalled and created the file again without the timestamp.
-
using System.Text.RegularExpressions;
using System;
using System.IO;
namespace ConsoleApp1
{ class Program { static void Main(string[] args) { Console.WriteLine("FileHistory timestamp remover V1.0 - HamCode 2018"); Console.WriteLine("Based on a simple C# script by user RainingChain @ Superuser.com"); Console.WriteLine("--------------------------------------------------------------------"); Console.WriteLine(); ReRun:; Console.WriteLine("Please enter files path, subdirectories will be scanned: "); string input = Console.ReadLine(); try { foreach (string SubDir in Directory.GetDirectories(input)) { try { string[] files = Directory.GetFiles(@SubDir, "*(????_??_?? ??_??_?? UTC).*", SearchOption.TopDirectoryOnly); Console.WriteLine(); Console.Write("Current directory: "); Console.Write(@SubDir); Console.WriteLine(" The following files have been identified as containing a FileHistory timestamp:"); foreach (var value in files) { Console.Write(value); } Console.WriteLine(); Console.WriteLine(); for (int i = 0; i < files.Length; i++) { string newName = Regex.Replace(files[i], @" ?\(\d\d\d\d_\d\d_\d\d \d\d_\d\d_\d\d UTC\)", ""); //System.Diagnostics.Debug.WriteLine(files[i], newName); File.SetAttributes(files[i], File.GetAttributes(files[i]) & ~FileAttributes.ReadOnly); if (File.Exists(newName)) { File.Delete(files[i]); Console.Write("File deleted: "); Console.Write(files[i]); Console.WriteLine(); goto DoNothing; } System.IO.File.Move(files[i], newName); Console.Write("Backup file renamed: "); Console.Write(newName); Console.WriteLine(); DoNothing:; } } catch (Exception ds) { Console.WriteLine(ds); } } } catch (Exception ex) { Console.WriteLine(ex); goto ReRun; } Console.WriteLine("Completed, press any key to exit."); Console.Read(); } }
} Here is a C# script. It fetches the list of all files. Then for each of them, remove substring matching something similar to " (2016_04_19 21_26_33 UTC)".
string[] files = Directory.GetFiles(@"C:\Users\Sam\NODEJS\node_modules", "*.*", SearchOption.AllDirectories);
for (int i = 0; i < files.Length; i++) { string newName = Regex.Replace(files[i], @" ?\(\d\d\d\d_\d\d_\d\d \d\d_\d\d_\d\d UTC\)", ""); //System.Diagnostics.Debug.WriteLine(files[i], newName); //File.SetAttributes(files[i], File.GetAttributes(files[i]) & ~FileAttributes.ReadOnly); System.IO.File.Move(files[i], newName);
}
System.Diagnostics.Debug.WriteLine("Completed");