简体   繁体   中英

Powershell script to get last build directory

I want to create powershell script/command to get the latest build directory name. Say here are the list of folders i have build_01_01_2018.1 build_01_01_2018.2 build_01_01_2018.3 and so on.... I want to get the latest build number.

I was able to get it thorugh following. But if any older builds are deleted i wont get exact last build. Any better approach ?

$currentTime=$(Get-Date -Format o)
$getDate=$currentTime.split("T")
$getDate = $getDate[0].replace('-','_')
$previousBuildCount= (Get-ChildItem build_2018_08_16.* -Path C:\builds\).Name.count
$buildVersion='build' + '_' +  $getDate + '.' + ($previousBuildCount + 1)

You can try simple regex parser:

#demo data
1 | out-file build_01_01_2018.1
2 | out-file build_01_01_2018.2
4 | out-file build_01_01_2018.4

#solution
Get-ChildItem build_01_01_2018.* | % {
  [int][Regex]::Match($_.Name, '(?<=build_01_01_2018\.).*').Value
} | Measure -Maximum | select -ExpandProperty Maximum

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM