Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.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 使用从文件名提取的字符串重命名文件_Powershell - Fatal编程技术网

Powershell 使用从文件名提取的字符串重命名文件

Powershell 使用从文件名提取的字符串重命名文件,powershell,Powershell,我在powershell脚本中有以下代码: #Call Bluezone to do file transfer #start-process "\\fhnsrv01\home\aborgetti\Documentation\Projects\Automation\OpenBZ.bat" #Variable Declarations $a = Get-Date $b = $a.ToString('MMddyy') $source = "\\fhnsrv01\home\aborgetti\Docu

我在powershell脚本中有以下代码:

#Call Bluezone to do file transfer
#start-process "\\fhnsrv01\home\aborgetti\Documentation\Projects\Automation\OpenBZ.bat"
#Variable Declarations
$a = Get-Date
$b = $a.ToString('MMddyy')
$source = "\\fhnsrv01\home\aborgetti\Documentation\Stage\"
$dest = "\\fhnsrv01\home\aborgetti\Documentation\Stage\orig"
#Find all the files that have EDIPROD extension and proceed to process them
#First copy the original file to the orig folder before any manipulation takes place
Copy-item $source\*.EDIPROD $dest
# Now we must rename the items that are in the table
Switch(GCI \\fhnsrv01\home\aborgetti\Documentation\Stage\*.EDIPROD){
    {(GC $_|Select -first 1).substring(176) -match "^834"}{$_ | Rename-Item -NewName {"834Dailyin$b"};Continue}
    {(GC $_|Select -first 1).substring(176) -match "^820"}{$_ | Rename-Item -NewName {"820Dailyin$b"};Continue}
}
我关心的部分是:

Switch(GCI \\fhnsrv01\home\aborgetti\Documentation\Stage\*.EDIPROD){
    {(GC $_|Select -first 1).substring(176) -match "^834"}{$_ | Rename-Item -NewName {"834Dailyin$b"};Continue}
    {(GC $_|Select -first 1).substring(176) -match "^820"}{$_ | Rename-Item -NewName {"820Dailyin$b"};Continue}
}
我将变量
$b
附加到文件名的末尾。这就是日期。然而,我感兴趣的真正日期是文件名本身,看起来是这样的:

AIDOCCAI.D051414.T025848.MO.EDIPROD

我需要powershell提取
D051414
以获取日期
051414
,并将其附加到文件末尾

否则我会反复出现以下错误:无法重命名项,因为项已存在

有人能帮忙吗?我希望保持switch语句的原始结构,因为它工作得非常好。但是,在上面写上日期并不重要。但是这个文件一天只来一次,所以每个文件都有不同的日期,这很有效,只是需要一些关于如何到达那里的帮助

更新

Switch(GCI \\fhnsrv01\home\aborgetti\Documentation\Stage\*.EDIPROD){
    {(GC $_|Select -first 1).substring(176) -match "^834"}{$_ | {$_ | ?{$_.Name -match "^.+?\.D(\d{6}).*"} | Rename-Item -NewName {"834Dailyin$($Matches[1])"}}
    {(GC $_|Select -first 1).substring(176) -match "^820"}{$_ | {$_ | ?{$_.Name -match "^.+?\.D(\d{6}).*"} | Rename-Item -NewName {"820Dailyin$($Matches[1])"}}
}

更新834重命名行以将交换机的scripteblock替换为:

{$_ | ?{$_.Name -match "^.+?\.D(\d{6}).*"} | Rename-Item -NewName {"834Dailyin$($Matches[1]).txt"};Continue}

说表达式只允许在管道的开头给出名为AIDOCCAI.D051414.T025848.MO.EDIPROD的原始文件如果是834文件应该是什么样子,如果是820文件应该是什么样子?好问题。实际上,通过文件名没有识别特征。这是x.x.T.MO.EDIPROD中的一堆数字。唯一的辨别方法是从文件的第一行开始…这有帮助吗?这并没有告诉我重命名后具体的文件名是什么。给定该文件名,它会是
AIDOCCAI.D051414.T025848.MO.EDIPROD.843Dailyin051414
?或者
AIDOCCAI.D051414.T025848.MO.834Dailyin051414
?如果它是一个820文件,除了用820代替834之外,它的名称是否相同?正确…对不起,我误解了。它实际上根本没有保留原始文件名。它应该是834dailyin051414.txt或820051414.txt