如果有数字,请从字符串中删除最后14位数字

时间:2013-06-20 16:52:35

标签: powershell

我有一个文本文件,用于存储我的testname。 testname有时会有一个由14位数时间戳组成的日期。 testname可以是:

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_20130620123306

我想重用此文件/ testname。我需要剥离时间戳,如果已经存在,则添加当前时间戳。这是我到目前为止所得到的:

#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个字符是否为数字?如何从字符串中剪切testname?

1 个答案:

答案 0 :(得分: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_20130620123306"

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