我想从此字符串中提取“ .txt”之前的最后4位数字:
2017年9月14日12:00:27-mtbill_post_201709141058.txt 7577_交付:确定
这些代表创建该日志的时间,我想将其显示为10:58。我从一个具有多行类似于显示的行的文件中读取。
Get-Content file.txt | ForEach-Object {
$splitUp = $_ -split "_"
$SC=$splitUp[2] -split "_"
Write-Host $SC
$len = $SC.Length
$folder2 = $SC.Substring($len - 12, 42)
}
Run Code Online (Sandbox Code Playgroud)
我尝试用“ _”分隔字符串,然后计算获得的字符串中的字符,并尝试通过“ Substring”命令分隔,但是我收到以下错误。
异常调用带有“ 2”参数的“子字符串”:“ StartIndex不能小于零。参数名称:startIndex”
在第6行:char:5 + $ folder2 = $ SC.Substring($ len-12,42)
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- CategoryInfo:未指定:(:) [],MethodInvocationException
- FullyQualifiedErrorId:ArgumentOutOfRangeException
powershell ×1