拆分并添加 text/number 到文件名
Split and add text/number to the filename
我一直在尝试编写一个脚本来删除文件名的结尾部分并将其替换为构建的版本号。我试过 trim 和拆分,但由于额外的点和不适合正则表达式,我遇到了问题。
这些是文件示例:
Filename.Api.sow.0.1.1856.nupkg
something.Customer.Web.0.1.1736.nupkg
我想从这些文件名中删除 0.1.18xx
并从变量中添加内部版本号。类似于 1.0.1234.1233
(major.minor.build.revision)
所以最终结果应该是:
Filename.Api.sow.1.0.1234.1233.nupkg
something.Customer.Web.1.0.112.342.nupkg
这是我尝试拆分然后重命名的尝试。但是没用。
$files = Get-ChildItem -Recurse | where {! $_.PSIsContainer}
foreach ($file in $files)
{
$name,$version = $file.Name.Split('[0-9]',2)
Rename-Item -NewName "$name$version" "$name".$myvariableforbuild
}
可能不是最简洁的方法,但我会根据“.”拆分字符串,获取数组的最后一个元素(文件扩展名),然后遍历每个数组元素。如果它是非数字的,则将其附加到新字符串,如果是数字,则中断循环。然后将新版本和文件扩展名附加到新字符串。
$str = "something.Customer.Web.0.1.1736.nupkg"
$arr = $str.Split(".")
$extension = $arr[$arr.Count - 1]
$filename = ""
$newversion = "1.0.112.342"
for ($i = 0 - 1; $i -lt $arr.Count; $i++)
{
if ($arr[$i] -notmatch "^[\d\.]+$")
{
# item is not numeric - add to string
$filename += $arr[$i] + "."
}
else
{
# item is numeric - end loop
break
}
}
# add the new version
$filename += $newversion + "."
# add the extension
$filename += $extension
显然这不是您问题的完整解决方案,但已经足够您开始使用了。
你快到了。这是一个使用正则表达式的解决方案:
$myVariableForBuild = '1.0.1234.1233'
Get-ChildItem 'c:\your_path' -Recurse |
where {! $_.PSIsContainer} |
Rename-Item -NewName { ($_.BaseName -replace '\d+\.\d+\.\d+$', $myVariableForBuild) + $_.Extension }
我一直在尝试编写一个脚本来删除文件名的结尾部分并将其替换为构建的版本号。我试过 trim 和拆分,但由于额外的点和不适合正则表达式,我遇到了问题。
这些是文件示例:
Filename.Api.sow.0.1.1856.nupkg
something.Customer.Web.0.1.1736.nupkg
我想从这些文件名中删除 0.1.18xx
并从变量中添加内部版本号。类似于 1.0.1234.1233
(major.minor.build.revision)
所以最终结果应该是:
Filename.Api.sow.1.0.1234.1233.nupkg
something.Customer.Web.1.0.112.342.nupkg
这是我尝试拆分然后重命名的尝试。但是没用。
$files = Get-ChildItem -Recurse | where {! $_.PSIsContainer}
foreach ($file in $files)
{
$name,$version = $file.Name.Split('[0-9]',2)
Rename-Item -NewName "$name$version" "$name".$myvariableforbuild
}
可能不是最简洁的方法,但我会根据“.”拆分字符串,获取数组的最后一个元素(文件扩展名),然后遍历每个数组元素。如果它是非数字的,则将其附加到新字符串,如果是数字,则中断循环。然后将新版本和文件扩展名附加到新字符串。
$str = "something.Customer.Web.0.1.1736.nupkg"
$arr = $str.Split(".")
$extension = $arr[$arr.Count - 1]
$filename = ""
$newversion = "1.0.112.342"
for ($i = 0 - 1; $i -lt $arr.Count; $i++)
{
if ($arr[$i] -notmatch "^[\d\.]+$")
{
# item is not numeric - add to string
$filename += $arr[$i] + "."
}
else
{
# item is numeric - end loop
break
}
}
# add the new version
$filename += $newversion + "."
# add the extension
$filename += $extension
显然这不是您问题的完整解决方案,但已经足够您开始使用了。
你快到了。这是一个使用正则表达式的解决方案:
$myVariableForBuild = '1.0.1234.1233'
Get-ChildItem 'c:\your_path' -Recurse |
where {! $_.PSIsContainer} |
Rename-Item -NewName { ($_.BaseName -replace '\d+\.\d+\.\d+$', $myVariableForBuild) + $_.Extension }