【问题标题】:How to modify my powershell script to search for all jpg/jpeg on a machine如何修改我的 powershell 脚本以搜索机器上的所有 jpg/jpeg
【发布时间】:2013-01-14 20:44:06
【问题描述】:

我正在构建一个 PowerShell 脚本,该脚本将查找机器上的所有 jpeg/jpg 文件。这就是我目前所拥有的——

# PowerShell script to list the DLL files under the C drive
$Dir = get-childitem C:\ -recurse
# $Dir |get-member
$List = $Dir | where {$_.extension -eq ".jpg"}
$List |ft fullname |out-file C:\Users\User1\Desktop\dll.txt
# List | format-table name

唯一的问题是我正在寻找的一些文件没有扩展名 jpg/jpeg。我知道您可以查看文件的标题,如果它显示 ÿØÿà 那么它是 jpeg/jpg 但我不知道如何将其合并到脚本中。

任何帮助将不胜感激。非常感谢!

【问题讨论】:

    标签: powershell powershell-2.0


    【解决方案1】:

    我不知道如何使用 powershell 本机命令来查看文件头,我会做一些研究,因为它听起来很有趣。在此之前,我可以建议您的初始命令的更短版本,将其缩减为单行。

    Get-ChildItem -Recurse -include *.jpg | Format-table -Property Fullname | Out-file C:\Users\User1\Desktop\Jpg.txt
    

    ls -r -inc *.jpg | ft Fullname
    

    已编辑:删除了多余的代码,感谢@nick。

    如果我找到任何东西,我会告诉你我找到了什么。

    克里斯

    【讨论】:

    • Where { $_.Extension -eq ".jpg" } 是多余的。 -include 将过滤掉不是.jpg 的所有内容。另一个很酷的事情是你可以做多个过滤器,所以他可以使用-include *.jpg,*.jpeg 来获得他想要的两个扩展。
    • @Ansgar Wiechers 有最好的解决方案。
    【解决方案2】:

    以下将检索扩展名为 .jpg/.jpeg 或在前四个字节中包含 JPEG 标头的文件:

    [Byte[]] $jpegHeader = 255, 216, 255, 224;
    
    function IsJpegFile([System.IO.FileSystemInfo] $file)
    {
        # Exclude directories
        if ($file -isnot [System.IO.FileInfo])
        {
            return $false;
        }
    
        # Include files with either a .jpg or .jpeg extension, case insensitive
        if ($file.Extension -match '^\.jpe?g$')
        {
            return $true;
        }
    
        # Read up to the first $jpegHeader.Length bytes from $file
        [Byte[]] $fileHeader = @(
            Get-Content -Path $file.FullName -Encoding Byte -ReadCount 0 -TotalCount $jpegHeader.Length
        );
    
        if ($fileHeader.Length -ne $jpegHeader.Length)
        {
            # The length of the file is less than the JPEG header length
            return $false;
        }
    
        # Compare each byte in the file header to the JPEG header
        for ($i = 0; $i -lt $fileHeader.Length; $i++)
        {
            if ($fileHeader[$i] -ne $jpegHeader[$i])
            {
                return $false;
            }
        }
    
        return $true;
    }
    
    [System.IO.FileInfo[]] $jpegFiles = @(
        Get-ChildItem -Path 'C:\' -Recurse `
            | Where-Object { IsJpegFile $_; }
    );
    
    $jpegFiles | Format-Table 'FullName' | Out-File 'C:\Users\User1\Desktop\dll.txt';
    

    注意Get-Content cmdlet-Encoding-TotalCount 参数仅用于读取每个文件的前四个字节,而不是整个文件。这是一项重要的优化,因为它基本上避免了读取C: 驱动器上文件数据的每个字节。

    【讨论】:

      【解决方案3】:

      这应该会给你所有以“ÿØÿà”开头的文件:

      $ref = [byte[]]@(255, 216, 255, 224)
      
      Get-ChildItem C:\ -Recurse | ? { -not $_.PSIsContainer } | % {
        $header = [System.IO.File]::ReadAllBytes($_.FullName)[0..3]
        if ( (compare $ref $header) -eq $null ) {
          $_.FullName
        }
      } | Out-File "C:\Users\User1\Desktop\dll.txt"
      

      【讨论】:

      • 虽然这在理论上可行,但对ReadAllBytes 的调用会将文件的每个字节读入内存,即使您只对前四个字节感兴趣。因此,这需要做很多工作来读取将被丢弃的数据。最好的情况是,这个脚本需要很长时间才能完成(无论读取C: 上的每个文件的全部内容都需要很长时间),最坏的情况是,如果脚本遇到一个巨大的视频、虚拟磁盘映像,@987654324 @、.zip 等可能会耗尽系统内存。
      【解决方案4】:

      要查找标题是否以 ÿØÿà 开头,请使用:

      [System.String]$imgInfo = get-content $_ # where $_ is a .jpg file such as "pic.jpg"
      if($imgInfo.StartsWith("ÿØÿà"))
      {
            #It's a jpeg, start processing...
      }
      

      希望对你有帮助

      【讨论】:

        【解决方案5】:

        我建议查询 jpeg 的 Windows 搜索索引,而不是尝试嗅探文件内容。使用文件名搜索系统索引非常快,缺点是您必须搜索索引位置。

        我使用 windows sdk \samples\windowssearch\oledb 编写了一个 windows 搜索查询脚本,您可能希望使用成像属性进行查询。但是,我不确定搜索索引是否使用映像 API 来查看未知文件或没有扩展名的文件。 Explorer 似乎知道我的 jpeg 缩略图和没有 jpg 扩展名的元数据,所以我猜索引器会像 explorer 一样聪明。

        【讨论】:

          【解决方案6】:

          这个怎么样?

          jp*g 用于匹配 jpg 和 jepg 图像。

          $List = Get-ChildItem "C:\*.jp*g" -Recurse
          $List |ft fullname |out-file C:\Users\User1\Desktop\dll.txt
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2021-08-15
            • 2021-04-16
            • 2021-06-03
            • 2015-11-22
            • 2017-11-29
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多