Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell 从文件名中提取特定的10位字符串_Powershell - Fatal编程技术网

Powershell 从文件名中提取特定的10位字符串

Powershell 从文件名中提取特定的10位字符串,powershell,Powershell,我们的大多数文件都以统一的语法命名: B0????????.????.??????.jpg 然而,我们偶尔会看到: ?????.B0????????.????.jpg 或 我需要一个PowerShell脚本来提取“B0”和接下来的8位数字。传统上,我们只是在文件名一致的情况下修剪字符串,但这种做法在这些变化中失败了 是否有人有一点PS逻辑,可以从字符串/文件名中提取“B0”和接下来的8位数字 谢谢 -replace.*(B0\d{8})。*”、“$1”@AnsgarWiechers我想他不是

我们的大多数文件都以统一的语法命名:

B0????????.????.??????.jpg
然而,我们偶尔会看到:

?????.B0????????.????.jpg

我需要一个PowerShell脚本来提取“B0”和接下来的8位数字。传统上,我们只是在文件名一致的情况下修剪字符串,但这种做法在这些变化中失败了

是否有人有一点PS逻辑,可以从字符串/文件名中提取“B0”和接下来的8位数字


谢谢

-replace.*(B0\d{8})。*”、“$1”
@AnsgarWiechers我想他不是想重命名文件。@不可更正的1没有重命名它-你只是修改字符串。@ConnorLSW对,但他只是想从字符串中提取数据,所以
-match
会suffice@TheIncorrigible1您需要执行
-match
然后访问
$Matches
-这在一条语句中执行完全相同的操作。这可以缩短为
$File=“??.B0??????.jpg”-replace.*(B0.{8})。*”,“$1.jpg'
$file = "?????.B0????????.????.jpg"
$index = ($file).IndexOf("B0")
$yourCharacters = ($file).Substring($index, 10)
$file.Replace($yourCharacters, "")
$file = "?????.B0????????.????.jpg"
$index = ($file).IndexOf("B0")
$yourCharacters = ($file).Substring($index, 10)
$file.Replace($yourCharacters, "")