【问题标题】:Powershell Scan for missing files in multiple foldersPowershell扫描多个文件夹中丢失的文件
【发布时间】:2022-01-06 13:41:57
【问题描述】:

我正在使用 Powershell 检查丢失的 XYZ 地图图块,但在嵌套循环中出现问题。本质上,地图瓦片存在于“基础”文件夹中,在该基础文件夹中有多个目录。每个目录中都有地图图块。

例如

C:\My Map\17\       # this is the Base folder, zoom level 17
C:\My Map\17\1234\  # this is a folder containing map tiles
C:\My Map\17\1234\30200.png  # this is a map tile
C:\My Map\17\1234\30201.png  # this is a map tile
C:\My Map\17\1234\30203.png  # this is a map tile, but we're missing 30202.png (have tiles either side)
C:\My Map\17\1234\30204.png  # this is a map tile
C:\My Map\17\1235\  # this is another folder containing map tiles [...]

所以我的想法是针对每个文件夹,扫描每边都有瓷砖的间隙并尝试下载它们。

这是我目前所拥有的:

$BasePath = "C:\_test\17\"

$ColumnDirectories = Get-ChildItem $BasePath -Directory

$ColumnDirectories | ForEach-Object {
    $ColumnDirectory = $ColumnDirectories.FullName 
    $MapTiles =  Get-ChildItem -Path $ColumnDirectory -Filter *.png -file
    $MapTiles | ForEach-Object {
        #Write-Host $MapTiles.FullName
        $TileName = $MapTiles.Name -replace '.png',''
        $TileNamePlus1 = [int]$TileName + 1
        $TileNamePlus2 = [int]$TileName + 2
        Write-Host $TileName
    }
}

但我无法将“System.Object[]”类型的“System.Object[]”值转换为“System.Int32”类型。

最终我想在 $TileName、TileNamePlus1、$TileNamePlus2 中的每一个上进行 Test-Path,并且中间不存在再次下载它。

例如

C:\My Map\17\1234\30201.png -- Exists
C:\My Map\17\1234\30202.png -- Not exists, download from https://somemapsrv.com/17/1234/30202.png
C:\My Map\17\1234\30203.png -- Exists

任何帮助表示赞赏!我是 Powershell 的新手。

【问题讨论】:

  • 您需要在 ForEach-Object 的 Scriptblock 中使用 $_ 来引用当前对象,您正在引用您正在迭代的集合变量。
  • 每个文件夹的地图图块数量是否始终相同?
  • $ColumnDirectories.FullName 应该是 $_.FullName,正如杜加斯所指出的,同样的事情也适用于嵌套的 $MapTiles 内的 $MapTiles。
  • @Hazrelle 不,它因文件夹而异,具体取决于有多少土地。每个文件夹就像是经过碎纸机的垂直“条带”地图,然后每个地图图块都是该条带的一部分。
  • 但是如果最后一个数字地图瓦片丢失,脚本如何知道这个文件夹应该在 7 停止?

标签: powershell automation gis


【解决方案1】:

这里的全部问题是理解ForEach-Object 循环是如何工作的。在循环内,自动变量$_ 表示循环的当前迭代。因此,根据 dugas 和 Santiago Squarzon 的 cmets 建议,您需要更改此行:

        $TileName = $MapTiles.Name -replace '.png',''

到这里:

        $TileName = $_.Name -replace '\.png',''

或者更简单地说(BaseName 属性是不带扩展名的文件名):

        $TileName = $_.BaseName

【讨论】:

    【解决方案2】:

    由于所有 png 文件的基本名称都是整数,因此您可以执行以下操作:

    $BasePath = 'C:\_test\17'
    $missing = Get-ChildItem -Path $BasePath -Directory | ForEach-Object {
        $ColumnDirectory = $_.FullName 
        # get an array of the files in the folder, take the BaseName only
        $MapTiles = (Get-ChildItem -Path $ColumnDirectory -Filter '*.png' -File).BaseName
        # create an array of integer numbers taken from the files BaseName
        $sequence = $MapTiles | ForEach-Object { [int]$_ } | Sort-Object
    
        $sequence[0]..$sequence[-1] | Where-Object { $MapTiles -notcontains $_ } | ForEach-Object {
            Join-Path -Path $ColumnDirectory -ChildPath ('{0}.png' -f $_)
        }
    }
    
    # missing in this example has only one file, but could also be an array of missing sequential numbered files
    
    $missing  # --> C:\_test\17\1234\30202.png
    

    如果您的文件名有前导零,这将不起作用..

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-10-20
      • 1970-01-01
      • 2016-12-30
      • 1970-01-01
      • 1970-01-01
      • 2016-08-23
      • 2012-03-16
      • 1970-01-01
      相关资源
      最近更新 更多