Bawo ni lati ipele lorukọ apa ti awọn filename ni Windows PowerShell lilo Regex?

Ni yi apẹẹrẹ, a ni 6 awọn faili ni isalẹ pẹlu awọn ọjọ ati filename ni isalẹ:

Mode LastWriteTime Ipari Name
----                -------------         ------ ----
-a ----       10/06/2018     12:40              0 1 = 9.8.16 Bank Gbólóhùn 298.67.pdf
Olódì ----       10/06/2018     12:40              0 2 = 9.9.16 Bank Gbólóhùn 134.74.pdf
Olódì ----       10/06/2018     12:41              0 3 = 9.10.16 Bank Gbólóhùn 123.43.pdf
Olódì ----       10/06/2018     12:41              0 4 = 9.11.16 Bank Gbólóhùn 134.54.pdf
Olódì ----       10/06/2018     12:41              0 5 = 9.12.16 Bank Gbólóhùn 157.98.pdf
Olódì ----       10/06/2018     12:41              0 6 = 9.1.17 Bank Gbólóhùn 123.45.pdf

Nibi, awọn filename ni a ni tẹlentẹle # Ni ibere, atẹle nipa =, atẹle nipa 'Bank Gbólóhùn’ atẹle nipa awọn opin ti gbólóhùn iwontunwonsi.

Awọn wọnyi ni awọn faili ni o wa ni o daju Credit Card Gbólóhùn ati awọn ọrọ ti 'Bank’ nilo lati paarọ rẹ nipa awọn ọrọ 'Credit Card'. Gbogbo awọn iyokù ti awọn filename yẹ ki o wa kanna.

Ni ibere lati se aseyori yi, we will be using Windows PowerShell with Regex.

Lati akojö gbogbo awọn ifowo gbólóhùn, a lo ls pipaṣẹ, ki o si pipe awọn ti o wu

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

Awọn ẹtọ aṣẹ ti lo lẹhin ti paipu to lorukọ awọn faili. $_ ni awọn ohun ti o Oun ni awọn wu ki o to paipu, ati awọn ti a ti wa ni lilo awọn orukọ ohun ini ti $_ ohun. -replace is used on strings in PowerShell, eyi ti rọpo a okun pẹlu kan titun okun. -replace ni iṣẹ kan ti o gba 2 sile. Awọn 1st paramita ni atijọ okun ti a fẹ lati ropo ati awọn 2nd paramita ni titun okun.

Yiya awọn ẹgbẹ ni Regex gba wa pa awọn ẹya ti awọn okun. ni Regex, ohunkohun fi ni parenthesis wa ni sile lati lo nigbamii. fun apẹẹrẹ. (.+)Bank(.+) ya ohunkohun ṣaaju ki o si lẹhin ti awọn kikọ 'Bank'. A le lo awọn sile awọn ẹya ara, nipa lilo $ ati awọn nọmba kan. Awọn 1st parenthesis ya sinu $1 ati awọn 2nd parenthesis ya sinu $2 ati be be lo…

Ni awọn 1st paramita, a pato awọn atijọ okun ti a fẹ lati ropo, ati ni akoko kanna ti a Yaworan awọn ẹya ara ti awọn okun ti a fẹ lati tọju (Yaworan). Ni 2nd paramita, titun okun nlo awọn sile awọn ẹya ti a fẹ lati tọju, bi daradara bi awọn titun okun ara eg. '$1Credit Card$2'.

esi:

Mode LastWrite Time Ipari Name
----       ---------      ----            ------   ----
-a ----     10/06/2018     12:40              0 1 = 9.8.16 Credit Card Gbólóhùn 298.67.pdf
Olódì ----     10/06/2018     12:40              0 2 = 9.9.16 Credit Card Gbólóhùn 134.74.pdf
Olódì ----     10/06/2018     12:41              0 3 = 9.10.16 Credit Card Gbólóhùn 123.43.pdf Olódì ----     10/06/2018     12:41              0 4 = 9.11.16 Credit Card Gbólóhùn 134.54.pdf Olódì ----     10/06/2018     12:41              0 5 = 9.12.16 Credit Card Gbólóhùn 157.98.pdf Olódì ----     10/06/2018     12:41              0 6 = 9.1.17 Credit Card Gbólóhùn 123.45.pdf

Fi kan Fesi