Powershell 脚本没有获取最新版本

问题描述 投票:0回答:1
#----------------------------------------------------------------------#
param(
    [STRING]$path = $PSScriptRoot
)

# Set the source and destination folders
# $srcFolder = "COPY PATH OF PDF FILES"
$srcFolder = $path
$destFolder = "$srcFolder\Latest Drawings"



# Get all files in the source folder that match the pattern
$files = Get-ChildItem -Path $srcFolder -Filter "*_*.*" -File
foreach ($fileName in $files.Name) {
    $name = $fileName.split('_')[0]
    
    $latestRevisionNo = 0
    $revisions = $files.Name | Where-Object { $_ -like ("{0}*") -f $name }
    foreach ($revision in $revisions) {
        $revision = $revision.split('_')[1].split('.')[0]
        if ($revision -gt $latestRevisionNo) {
            $latestRevisionNo = $revision
        }
    }

    # Copy Latest revision
    if (-not (Test-Path -path $destFolder)) {
        New-Item -path $destFolder -ItemType Directory
    }

    $latestRevision = $files.Name | Where-Object { $_ -like ("{0}_{1}.pdf") -f $name, $latestRevisionNo }
    Copy-Item -Path ("{0}\{1}" -f $srcFolder, $latestRevision) -Destination $destFolder -Force
}

#----------------------------------------------------------------------#

此处发布的代码大部分都有效。但是有一些问题和问题,我无法弄清楚两位数似乎是问题所在,并且在运行脚本时没有被拾取。

我们的文件格式类似于下面的测试/故障排除文件。

我将大量文件格式化为反映的格式,绘图 #_Revision #.pdf 我希望开发一个脚本来拥有一个包含最新版本的文件夹。基于 _ 和 .

之间的值

故障排除结果

例如:AE1.101A_1.pdf、AE1.101A_2.pdf、AE1.101A_3.pdf、AE1.101A_4.pdf、AE1.101A_25.pdf

拾取 AE1.101A_4.pdf,而不是 AE1.101A_25.pdf

例如:GI0-503_REL 35.pdf、GI0-503_REL 36.pdf、GI0-503_REL 41.pdf

拿起 GI0-503_REL 41.pdf

例如:AE1.101A_1.pdf、AE1.101A_2.pdf、AE1.101A_3.pdf、AE1.101A_4.pdf、AE1.101A_400.pdf

拿起AE1.101A_400.pdf

例如:GI0-503_REL 35.pdf、GI0-503_REL 36.pdf、GI0-503_REL 41.pdf、GI0-503_REL 410.pdf

拿起 GI0-503_REL 410.pdf

例如:GI0-503_REL 35.pdf、GI0-503_REL 36.pdf、GI0-503_REL 41.pdf、GI0-503_REL 210.pdf

拿起 GI0-503_REL 41.pdf,而不是 GI0-503_REL 210.pdf

例如:AE1.101A_1.pdf、AE1.101A_2.pdf、AE1.101A_3.pdf、AE1.101A_4.pdf、AE1.101A_10.pdf

拿起AE1.101A_10.pdf

例如:GI0-503_REL 35.pdf、GI0-503_REL 36.pdf、GI0-503_REL 41.pdf、GI0-503_REL 310.pdf

拿起 GI0-503_REL 41.pdf,而不是 GI0-503_REL 310.pdf

powershell sorting automation archive
1个回答
0
投票

展开所有评论,当您执行 string 比较时,它会执行字典式逐字符比较。

例如:

  • a
    <
    b
    <
    ba
    <
    c
  • 1
    <
    2
    <
    21
    <
    3

这意味着 strings 中有连续的数字 don't 按数字排序 - 它们根据字符在字符串中的位置排序。

例如:

  • AE1.101A_25.pdf
    <
    AE1.101A_4.pdf

因为两个字符串在字符 10 和

2
<
4
处不同,所以
AE1.101A_25.pdf
<
AE1.101A_4.pdf
也是如此。

如果你想对字符串中的连续数字进行排序numerically你必须做一些额外的工作来自己提取值,这样你就可以将它们作为numbers而不是characters

@Theo在评论中给你的答案如下:

$names = @(
    "AE1.101A_1.pdf",
    "AE1.101A_2.pdf",
    "AE1.101A_3.pdf",
    "AE1.101A_4.pdf",
    "AE1.101A_25.pdf"
);

$latestRevisionNo = 0;
foreach( $name in $names )
{
    # $name = "AE1.101A_4.pdf"  -> [int] "4"  -> $revision =  4
    # $name = "AE1.101A_25.pdf" -> [int] "25" -> $revision = 25
    $revision = [int] $name.Split("_")[1].Split(".")[0];
    if ($revision -gt $latestRevisionNo)
    {
        $latestRevisionNo = $revision;
    }
}
write-host "latest revision = $latestRevisionNo";

关键是用

[int]
将尾随数字转换成数字然后你可以做numeric比较而不是string比较,结果上面的输出是:

latest revision = 25
© www.soinside.com 2019 - 2024. All rights reserved.