Indlela batch ukuqamba kabusha ingxenye lefayela Windows PowerShell usebenzisa Regex?

Kulesi sibonelo, sine 6 amafayela ngezansi nge usuku lefayela ngezansi:

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

Lapha, igama lefayela iqukethe serial # ekuqaleni, kulandele =, kulandele 'Isitatimende Bank’ kulandele ekupheleni isitatimende ibhalansi.

Lawa mafayela eqinisweni Credit Card Izitatimende anakho kanti elithi 'Bank’ izidingo ukuba kubekwe amagama 'Credit Card'. Zonke lonke lefayela kufanele akukashintshi.

Ukuze kuzuzwe lokhu, we will be using Windows PowerShell with Regex.

Ukuze kwenziwe uhlu lwazo zonke izitatimende zasebhange, sisebenzisa ls umyalo, ke ipayipi okukhiphayo

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

Umyalo ukuqamba kabusha isetshenziswa ngemva ipayipi ukuqamba kabusha amafayela. $_ iyona into ebamba okukhiphayo phambi ipayipi, futhi sisebenzisa igama impahla $_ into. -replace is used on strings in PowerShell, okuyinto ingena esikhundleni yezinhlamvu ngentambo entsha. -replace umsebenzi kuthatha 2 amapharamitha. Umngcele we 1st liwuchungechunge ubudala ukuthi sifuna esikhundleni futhi ipharamitha 2nd liwuchungechunge entsha.

Ukuthwebula izithombe amaqembu Regex ivumela kithi ukugcina izingxenye string. Ngo Regex, yini efakwa abakaki ishuthiwe ukusebenzisa kamuva. isib. (.+)Bank(.+) ithumba lutho ngaphambi nangemva izinhlamvu 'Bank'. Singasebenzisa lezi izingxenye ethumba, ngokusebenzisa $ kanye nenombolo. I abakaki 1st ithumba ku $1 kanye abakaki 2nd ithumba ku $2 njll…

Ngo ipharamitha 1st, thina ucacise string ubudala ukuthi sifuna esikhundleni, futhi ngesikhathi esifanayo senza awuthumbe izingxenye string ukuthi sifuna ukugcina (bamba). Ngo ipharamitha 2nd, string entsha isebenzisa izingxenye ethumba ukuthi sifuna ukugcina, kanye string esisha uqobo isib. '$1Credit Card$2'.

Umphumela:

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

Shiya impendulo