2013-06-20 63 views
1

我有一個文本文件,用於存儲我的測試名。測試名稱有時會包含14位數的時間戳。該測試名可以是:刪除字符串中的最後14位數字(如果有數字)

data-c(festo1-small1);divider-bin-1.4.4;divider-conf-1.3.3-w(1,16);storage-bin-1.5.4;storage-conf-1.5.0-w(1);worker-bin-4.5.1;worker-conf-4.4.1-c(festo1)-win1

data-c(festo1-small1);divider-bin-1.4.4;divider-conf-1.3.3-w(1,16);storage-bin-1.5.4;storage-conf-1.5.0-w(1);worker-bin-4.5.1;worker-conf-4.4.1-c(festo1)-win1_2013062

我想重新使用這個文件/測試名稱。我需要去掉時間戳,如果已經有了,添加當前時間戳。這是我到目前爲止:

#rescue testname for another run                                                   
$testname = Get-Content "output\testname" 

Write-Host Old: $testname 

#strip date from testname                                                     
$testname = $testname.Substring(0,$string.Length - 14) 
$olddate = $testname.Substring($string.Length - 14) 

Write-Host OldDate: $olddate 

#add new Date                                                        
$startTime = Get-Date -format yyyyMMddHHmmss 
$testname += "_" 
$testname += $startTime 

Write-Host New: $testname 

如何檢查是否有時間戳/如果最後14個字符是數字?如何從字符串中刪除測試名?

回答

2

這個?

$filename = "data-c(festo1-small1);divider-bin-1.4.4;divider-conf-1.3.3-w(1,16);storage-bin-1.5.4;storage-conf-1.5.0-w(1);worker-bin-4.5.1;worker-conf-4.4.1-c(festo1)-win1_2013062" 

$filename -replace '\d{14}$' 
+0

我認爲你需要考慮(放下)'win1'和14位數字之間的下劃線。 – alroc

+0

Yepp。我剛剛添加了另一個替換'$ testname = $ testname -replace'_''。擺脫下劃線。感謝這個非常短的解決方案! – mles

+0

@ mles很高興幫助! –

相關問題