MMA
MMA

Reputation: 458

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)

Upvotes: 0

Views: 142

Answers (1)

Paweł Dyl
Paweł Dyl

Reputation: 9143

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

Upvotes: 1

Related Questions