Olee otú ogbe ịnyegharị akụkụ nke filename na Windows PowerShell iji Regex?

Na nke a atụ, anyị nwere 6 files n'okpuru na ụbọchị na filename n'okpuru:

Mode LastWriteTime Ogologo Name
----                -------------         ------ ----
-a ----       10/06/2018     12:40              0 1 = 9.8.16 Bank Statement 298.67.pdf
-bụ ----       10/06/2018     12:40              0 2 = 9.9.16 Bank Statement 134.74.pdf
-bụ ----       10/06/2018     12:41              0 3 = 9.10.16 Bank Statement 123.43.pdf
-bụ ----       10/06/2018     12:41              0 4 = 9.11.16 Bank Statement 134.54.pdf
-bụ ----       10/06/2018     12:41              0 5 = 9.12.16 Bank Statement 157.98.pdf
-bụ ----       10/06/2018     12:41              0 6 = 9.1.17 Bank Statement 123.45.pdf

Ebe a, na filename nwere a Oghere Usoro # na mmalite, sochiri =, sochiri 'Bank Statement’ soro site na njedebe nke na nkwupụta itule.

A faịlụ bụ n'ezie kaadị okwu na okwu 'Bank’ kwesịrị ịbụ dochie anya okwu 'E Si Nweta Kaadị'. All na ike nke filename kwesịrị ịnọgide na otu.

Iji mezuo nke a, we will be using Windows PowerShell with Regex.

Depụta niile akụ okwu, anyị na-eji ls iwu, mgbe anwụrụ mmepụta

ls | ren -newname {$_.name -replace('(.+)Bank(.+)','$ 1Credit Card $ 2')}

The Gbanwé aha iwu a na-eji mgbe ọjà na nyegharịa faịlụ. $_ bụ ihe na esetịpụ mmepụta tupu anwụrụ, na na anyị na-eji aha onwunwe nke $_ ihe. -replace is used on strings in PowerShell, nke na-anọchi a eriri na a ọhụrụ eriri. -replace bụ a ọrụ na-ewe 2 parameters. The 1st oke ochie eriri na anyị chọrọ iji dochie na 2nd oke ọhụrụ eriri.

Akada iche iche na Regex na-enye ohere anyị na-akụkụ nke eriri. na Regex, ihe ọ bụla na-etinye na parenthesis weghaara iji mesịrị. dịka. (.+)Bank(.+) were ihe ọ bụla tupu mgbe odide 'Bank'. Anyị nwere ike iji ndị a weghaara akụkụ, site na iji $ na a ọnụ ọgụgụ. The 1st parenthesis were n'ime $1 na 2nd parenthesis were n'ime $2 etc…

Na 1st oke, anyị dee ochie eriri na anyị chọrọ iji dochie, ma n'otu oge anyị weghara akụkụ nke eriri na anyị chọrọ ịnọgide na- (kpọọ). Na 2nd oke, ọhụrụ eriri eji weghaara akụkụ na anyị chọrọ ịnọgide na-, nakwa dị ka ọhụrụ eriri onwe ya eg. '$1Credit Card$2'.

n'ihi:

Mode LastWrite Time Ogologo Name
----       ---------      ----            ------   ----
-a ----     10/06/2018     12:40              0 1 = 9.8.16 Ebe E Si Nweta Kaadị Statement 298.67.pdf
-bụ ----     10/06/2018     12:40              0 2 = 9.9.16 Ebe E Si Nweta Kaadị Statement 134.74.pdf
-bụ ----     10/06/2018     12:41              0 3 = 9.10.16 Ebe E Si Nweta Kaadị Statement 123.43.pdf -bụ ----     10/06/2018     12:41              0 4 = 9.11.16 Ebe E Si Nweta Kaadị Statement 134.54.pdf -bụ ----     10/06/2018     12:41              0 5 = 9.12.16 Ebe E Si Nweta Kaadị Statement 157.98.pdf -bụ ----     10/06/2018     12:41              0 6 = 9.1.17 Ebe E Si Nweta Kaadị Statement 123.45.pdf

Nkume a-aza