我正在使用 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 中的每一個上進行測驗路徑,并且中間的那個不存在以再次下載它。
例如
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 相當陌生。
uj5u.com熱心網友回復:
這里的整個問題是對ForEach-Object回圈如何作業的理解。在回圈內,自動變數$_表示回圈的當前迭代。因此,正如 dugas 和 Santiago Squarzon 的評論所建議的那樣,您需要更改這一行:
$TileName = $MapTiles.Name -replace '.png',''
對此:
$TileName = $_.Name -replace '\.png',''
或者更簡單的這個(BaseName 屬性是沒有擴展名的檔案名):
$TileName = $_.BaseName
uj5u.com熱心網友回復:
由于您所有的 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
如果您的檔案名前導零,這將不起作用..
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/370390.html
上一篇:MicrosoftGraphAPI-Powershell-檢索Excel檔案附件
下一篇:Where-Object不過濾
