我在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}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句