我在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])"}}
}
答案 0 :(得分:1)
更新834重命名行以将交换机的scripteblock替换为:
{$_ | ?{$_.Name -match "^.+?\.D(\d{6}).*"} | Rename-Item -NewName {"834Dailyin$($Matches[1]).txt"};Continue}