删除文件名中的 Windows 文件历史时间戳

删除文件名中的 Windows 文件历史时间戳

我将使用 Windows 8 文件历史记录创建的文件夹从驱动器复制到运行 Windows 7 的计算机。由于我无法使用还原功能,因此所有文件都有时间戳,如01 Disappearing World (2015_01_09 04_57_38 UTC).mp3。有没有简单的方法可以删除这些时间戳?

答案1

这是 Powershell 版本

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\)", ""}

答案2

这是经过少许调整并转换为完整 C# 控制台应用程序的 RainingChain 脚本。只需编译并享用,已在我自己的系统中进行了测试。

新增功能:

  • 当访问被拒绝时,子目录扫描和异常吞噬。

  • 仅对带有时间戳的文件进行操作,这样可以避免影响其他数据。

  • 如果带时间戳的文件在同一文件夹中有另一个没有时间戳的副本,则会删除带时间戳的文件。例如,当重新安装某个程序并再次创建没有时间戳的文件时。

-

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();
        }
    }
}

答案3

这是一个 C# 脚本。它获取所有文件的列表。然后针对每个文件,删除与“ (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");

相关内容