Sida loo Dufcaddii qayb ka mid ah filename ee Windows PowerShell isticmaalaya Regex magaca?

Tusaalahaan, waxaan leenahay 6 files hoos ku leh taariikhda iyo filename hoos:

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

halkan, filename ku jira serial ah # bilowga, raacay by =, ku xiga 'Statement Bank’ raacay by dhamaadka dheelitirka hadal.

files Kuwanu waa dhab ahaantii Credit Card Statements iyo erayga 'Bank’ baahan yahay in la badalay erayada 'Credit Card'. Dhamaan inta kale ee filename waa in isku sii.

Si taas loo gaaro, we will be using Windows PowerShell with Regex.

Si aad u qor dhammaan statements bangiga, aan u isticmaalno ee ls amarka, markaas biibiile wax soo saarka

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

Amarka magaca waxaa loo isticmaalaa ka dib markii biibiile magaca faylasha. $_ waa shayga in uu haystaa saarka ka hor biibiile, waxaana isticmaalka hantida magaca ah $_ walax. -replace is used on strings in PowerShell, kaas oo badalay string la string cusub. -replace waa shaqo ah in qaadataa 2 xuduudaheedu. Halbeegga 1aad waa string hore waxaan rabnaa in aan ka bedelo oo dhimaya 2aad waa string cusub.

Qabsashada kooxo Regex u ogolaanaya noo sii qaybo ka mid ah xarig ah. In Regex, wax ku riday qaws waxaa qabsaday in ka dib isticmaali. tusaale. (.+)Bank(.+) qabsada wax ka hor iyo ka dib characters 'Bank'. Waxaan u isticmaali kartaa qaybo ka qabsadeen, iyadoo la isticmaalayo $ iyo tiro ka mid ah. qaws 1aad qabsatay galay $1 iyo qaws 2aad qabsatay galay $2 iwm…

In dhimaya 1aad, waxaan sheeg string hore waxaan rabnaa in aan ka bedelo, iyo waqti isku mid ah waxaan qabsan ee qaybo ka mid ah string ah in aan doonayaa inaan sii wado (qabtay). In dhimaya 2aad, string cusub isticmaalaa qaybo ka qabtay in aan dooneyno in aan ku hayn, iyo sidoo kale string cusub laftiisa tusaale. '$1Credit Card$2'.

Natiijo:

Mode LastWrite Time Length Name
----       ---------      ----            ------   ----
-a ----     10/06/2018     12:40              0 1 = 9.8.16 Card Credit 298.67.pdf Statement
Xarafka ----     10/06/2018     12:40              0 2 = 9.9.16 Card Credit 134.74.pdf Statement
Xarafka ----     10/06/2018     12:41              0 3 = 9.10.16 Card Credit Xarafka 123.43.pdf Statement ----     10/06/2018     12:41              0 4 = 9.11.16 Card Credit Xarafka 134.54.pdf Statement ----     10/06/2018     12:41              0 5 = 9.12.16 Card Credit Xarafka 157.98.pdf Statement ----     10/06/2018     12:41              0 6 = 9.1.17 Card Credit 123.45.pdf Statement

Leave a Reply