Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell:在忽略字符串标记时递增数字_Powershell_Text Parsing - Fatal编程技术网

Powershell:在忽略字符串标记时递增数字

Powershell:在忽略字符串标记时递增数字,powershell,text-parsing,Powershell,Text Parsing,这是我的第一个堆栈问题,所以对我放松点 当前正在处理一个项目,通过增加以前文件夹的版本号,在网络驱动器上创建一个新文件夹 例如: 5.2.0.0110->5.2.0.0111 下面是我当前的powershell解决方案,它实现了以下功能: $SourceFolder = "\\corpfs1\setup\ProtectionSuite\Version 5.2.0.x\5.2.0.0001" $DestinationFolder = "\\corpfs1\setup\

这是我的第一个堆栈问题,所以对我放松点

当前正在处理一个项目,通过增加以前文件夹的版本号,在网络驱动器上创建一个新文件夹

例如:
5.2.0.0110
->
5.2.0.0111

下面是我当前的powershell解决方案,它实现了以下功能:

$SourceFolder = "\\corpfs1\setup\ProtectionSuite\Version 5.2.0.x\5.2.0.0001"
$DestinationFolder = "\\corpfs1\setup\ProtectionSuite\Version 5.2.0.x"
$msiSourceFolder = "\\SourceMsiPath"
$exeSourceFolder = "\\SourceExePath"
if (Test-Path $SourceFolder)
{
        $latest = Get-ChildItem -Path $DestinationFolder| Sort-Object Name -Descending | Select-Object -First 1 
        #split the latest filename, increment the number, then re-assemble new filename:
        $newFolderName = $latest.BaseName.Split('.')[0] + "." + $latest.BaseName.Split('.')[1] + "."+ $latest.BaseName.Split('.')[2] + "." + ([int]$latest.BaseName.Split('.')[3] + 1).ToString().PadLeft(4,"0")
        New-Item -Path $DestinationFolder"\"$newFolderName -ItemType Directory

        Copy-Item $msiSourceFolder -Destination $DestinationFolder"\"$newFolderName
        Copy-Item $exeSourceFolder -Destination $DestinationFolder"\"$newFolderName
}
然而,有一件事没有考虑到,那就是版本号的末尾带有字符串。此解决方案尝试转换失败的字符串->int。有些文件夹的字符串与内部版本相同,因此无法仅更改我的命名语义

例如:
5.2.0.1234(英文)
->
5.2.0.1235


我希望忽略最后四位数字和增量之后的任何文本,如上面的示例所示。如果有人提出建议,我洗耳恭听!谢谢。

假设您的文件夹名称只包含一个前导有
的4位序列,则使用基于-based和基于-based的替换更容易匹配和替换它:

更新:

  • 后来的澄清表明,输入字符串中的后版本后缀应该(b)从输出中删除,而不是(A)为了在输出中保留的同时增加而忽略-请参阅底部部分以了解(b)的解决方案

解决方案(a):如果应保留后期版本后缀

在PowerShell(核心)v6.1+中:

'5.2.0.1234 (eng)' -replace '\.(\d{4}).*', { '.{0:0000}' -f  (1 + $_.Groups[1].Value) }
#将示例值“5.2.0.1234(eng)”替换为代码中的以下内容:
#$newFolderName=$latest.BaseName[-replace…]
“5.2.0.1234(英文)”-替换“(?您可以执行以下操作:

$version = ($latest.BaseName -replace '^((?:\d+\.){3}\d{4}).*', '$1').Split('.')
$version[-1] = '{0:D4} -f ([int]$version[-1] + 1)
$newFolderName = $version -join '.'

# '5.2.0.0110 (eng)' --> '5.2.0.0111'

根据您的评论,您应该使用
连接路径
来构建完整的目标路径:

$targetPath = Join-Path -Path $DestinationFolder -ChildPath $newFolderName
$null = New-Item -Path $targetPath -ItemType Directory -Force

Copy-Item $msiSourceFolder -Destination $targetPath
Copy-Item $exeSourceFolder -Destination $targetPath

好的,这看起来不错。但是,格式有点不正确。它应该递增,并将四位数保留在最后一个“.”之后。你能为我指出进行编辑的正确方向吗?谢谢你的回答,西奥。@tunamaster我现在明白你的意思了。更新了答案。@tunamaster你需要指定要复制的实际文件或文件夹。现在,你的代码将源路径定义为
\\servername
,因此没有共享和wha的路径t无论你需要复制什么。这是不完整的,因此会出现错误。///顺便说一句,你的问题是关于捕获并增加文件夹名称,而不是关于代码中可能存在的其他错误。@tunamaster,重述Theo的论点:你的问题是关于转换文件夹名称,这个答案提供了一个解决方案,所以你应该这样做。如果你有一个f下面的问题,请创建一个新的问题帖子。将问题和答案集中在一个问题上,而不是将多个不相关的问题混合在一起,这对每个人都有好处。此外,请尝试在将来的问题中澄清所有要求:“忽略前缀”可能意味着两件事:(a)仅为递增而忽略或(b)在输出中删除。@tunamaster我现在在移动设备上,但请编辑您的问题,并提供FolderName的真实示例,以增加和匹配预期的新文件夹名示例。我明显感觉到,如果现在隐藏了很多信息,您会保留很多信息,因此在每次解决方案之后,您都会提出一个新的(以前未提及)文件夹名代码有问题。正如我前面所说的,您的问题是关于捕获并递增一个可以追加文本的foldername。我的答案解决了这个问题,mklement0的答案也解决了这个问题。