簡體   English   中英

根據文件名中間的版本號對文件進行排序

[英]Sorting of files based on version number in the middle of the file name

我是電源外殼的新手。 我正在嘗試對名稱中間有版本號的文件列表進行排序。

這是文件列表:

sample-file-1.1.0-patch.zip
sample-file-1.1.1-patch.zip
sample-file-1.1.2-patch.zip
sample-file-1.1.2.1-fix-patch.zip
sample-file-1.1.3-patch.zip
sample-file-1.1.3.1-fix-patch.zip
..
sample-file-1.1.15-patch.zip
sample-file-1.1.15.1-fix-patch.zip
sample-file-1.1.15.2-fix-patch.zip
..
sample-file-2.0.0-patch.zip
sample-file-2.0.1-patch.zip
..

我會請你幫我分類邏輯。

謝謝

您的樣本輸入最簡單的方法如下,它假定后面的第一個數字-開始版本號,並以后續的最后一個數字結束-

Get-ChildItem *.zip | Sort-Object { [version]($_.Name -replace '^[^\d]+-(.*\d)-.*', '$1') } 
  • $_.Name -replace '^[^\\d]+-(.*\\d)-.*', '$1'從每個文件名中提取版本號作為字符串。

  • [version]將該字符串強制轉換為版本號對象( System.Version[1] ,該對象可根據版本號組件進行排序。

  • 將整個表達式作為腳本塊{ ... }傳遞給Sort-Object意味着排序基於腳本塊返回的[version]實例,針對每個輸入文件名進行評估。


[1] PSv5.1引入了一種基於語義版本控制的相關類型[System.Management.Automation.SemanticVersion] ,但這不適用於此,因為它只支持3個版本號組件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM