如何以递归方式自动创建 Windows 缩略图?

dud*_*ude 6 video windows thumbnails windows-explorer images

我在 Windows 资源管理器中使用 Windows 8.1。当我访问包含视频、照片或其他此类文件的文件夹时,会创建图像缓存(“thumbs.db”)。我有这个问题,因为我有多个文件夹,其中包含不断变化的大型视频和照片。每次打开包含这些文件的文件夹时,我都必须等待几秒钟,直到创建缩略图缓存。

一种解决方法是禁用缩略图缓存。但我喜欢看缩略图,所以这对我来说不是一个解决方案。相反,我想每隔 x 秒调用一个批处理或另一个程序,以递归方式创建 Windows 缩略图。然后我可以毫不拖延地打开文件夹。

我怎样才能做到这一点?

Lee*_*son 5

这是我编写的 PowerShell 脚本,它应该可以满足您的需求。

我从这个线程中获取了逻辑:https : //stackoverflow.com/questions/3555799/how-do-i-refresh-a-files-thumbnail-in-windows-explorer,并将其制作成可以运行的脚本作为 Windows 中的计划任务。

您需要安装 .net4.0 和 PowerShell 3.0 才能使用它,否则会出现错误。此时您可能拥有 .net4.0,但您可能需要PowerShell 3.0

将以下内容保存到一个名为 thumb_generate.ps1 的文件中

param ([string]$path,[string]$ext)
function Refresh-Explorer 
{  
   $code = @'  
   [System.Runtime.InteropServices.DllImport("Shell32.dll")]   
   public static extern Int32 SHParseDisplayName([MarshalAs(UnmanagedType.LPWStr)] String pszName, IntPtr pbc, out IntPtr ppidl, UInt32 sfgaoIn, out UInt32 psfgaoOut);

   [System.Runtime.InteropServices.DllImport("Shell32.dll")]   
   private static extern int SHChangeNotify(int eventId, int flags, IntPtr item1, IntPtr item2);

   [System.Runtime.InteropServices.DllImport("Shell32.dll")]   
   private static extern int ILFree(IntPtr pidl);    

   public static void Refresh(string path)  {
    uint iAttribute;
    IntPtr pidl;
    SHParseDisplayName(path, IntPtr.Zero, out pidl, 0, out iAttribute);  
    SHChangeNotify(0x00002000, 0x1000, IntPtr.Zero, IntPtr.Zero); 
    ILFree(pidl); 
}  
'@  

    Add-Type -MemberDefinition $code -Namespace MyWinAPI -Name Explorer   
    [MyWinAPI.Explorer]::Refresh($path)  
} 

cls
if([System.String]::IsNullOrEmpty($path))
{
   Write-Host "Path cannot be empty."
   Write-Host "Example:  .\thumb_generate.ps1 -path ""C:\"" -ext ""jpg"""
   return
}
if([System.String]::IsNullOrEmpty($path))
{
   Write-Host "Extension cannot be empty."
   Write-Host "Example:  .\thumb_generate.ps1 -path ""C:\"" -ext ""jpg"""
   return
}

$fileExtension = "*." + $ext
Write-Host -ForegroundColor Green "---Thumbnail generation for Windows 7/8---"
Write-Host -ForegroundColor Green "----PowerShell 3.0 & .Net 4.0 required----"
Write-Host "Path: "  $path
Write-Host "Extension: " $fileExtension
Write-Host 
if (Test-Path $path) 
{
   Write-Host "Path Exists, begin generation thumbnails"

   $images = [System.IO.Directory]::EnumerateFiles($path,$fileExtension,"AllDirectories")
   Foreach($image in $images)
   {
       try
       {
          $file = New-Object System.IO.FileInfo($image)
          Write-Host $file.FullName
          $fStream = $file.Open([System.IO.FileMode]::Open,[System.IO.FileAccess]::Read,[System.IO.FileShare]::None)

          $firstbyte = New-Object Byte[] 1
          $result = $fStream.Read($firstbyte,0,1)
       }
       catch
       {
          Write-Host -ForegroundColor Red "An error occured on file: " + $file.FullName
       }
       $fStream.Close();
   }
    Refresh-Explorer
}
else
{
  "Path Doesn't Exist, Exiting..."
}
Run Code Online (Sandbox Code Playgroud)

然后使用以下参数从 PowerShell 命令行执行它:

.\thumb_generate.ps1 -path "C:\PathToImages\"  -ext "jpg"
Run Code Online (Sandbox Code Playgroud)

实际上,任何文件扩展名都应该有效。它将递归地查看所有目录。一个缺点是一次只有一种文件类型,但可以运行多个作业,只需使用不同的文件扩展名。基本上,脚本打开每个文件并只读取第一个字节,这足以强制更新 thumbs.db 文件。

编辑我修改了脚本以包含上面发布的 shell 更新部分。它似乎在我的系统上运行,但我没有成千上万的图像可供测试。这结合了文件前几个字节的读取,然后强制刷新缩略图。