powershell - Renaming files with string extracted from filename -
i have following code in powershell script:
#call bluezone 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 files have ediprod extension , proceed process them #first copy original file orig folder before manipulation takes place copy-item $source\*.ediprod $dest # must rename items in 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} }
the part i'm concerned here:
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} }
i appending variable $b
@ end of filename. holds date. however, real date i'm interested in in filename itself, , looks this:
aidoccai.d051414.t025848.mo.ediprod
i need have powershell extract d051414
date 051414
, append end of file.
otherwise repeatedly error: cannot rename-item because item exists.
can this? keep original structure of switch statement works well. having date on there however, not. file comes once day, every file have different date , work, need on how there.
update
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])"}} }
update 834 rename line replace switch's scripteblock read:
{$_ | ?{$_.name -match "^.+?\.d(\d{6}).*"} | rename-item -newname {"834dailyin$($matches[1]).txt"};continue}
Comments
Post a Comment