Yuav ua li cas batch rename ib feem ntawm lub filename nyob rau hauv lub qhov rais PowerShell siv regex?

Nyob rau hauv qhov kev piv txwv, peb muaj 6 cov ntaub ntawv hauv qab no nrog rau lub hnub thiab filename hauv qab no:

Hom LastWriteTime Length Lub npe
----                -------------         ------ ----
-ib tug ----       10/06/2018     12:40              0 1 = 9.8.16 Bank Statement 298.67.pdf
piv ----       10/06/2018     12:40              0 2 = 9.9.16 Bank Statement 134.74.pdf
piv ----       10/06/2018     12:41              0 3 = 9.10.16 Bank Statement 123.43.pdf
piv ----       10/06/2018     12:41              0 4 = 9.11.16 Bank Statement 134.54.pdf
piv ----       10/06/2018     12:41              0 5 = 9.12.16 Bank Statement 157.98.pdf
piv ----       10/06/2018     12:41              0 6 = 9.1.17 Bank Statement 123.45.pdf

Ntawm no, lub filename muaj ib tug serial # thaum pib, ua raws li los ntawm =, ua raws li los ntawm 'Bank Statement’ ua raws li los ntawm tus kawg ntawm daim ntawv qhia nqi koj tshuav.

Cov ntaub ntawv muaj nyob rau hauv qhov tseeb Credit Card lus thiab cov lo lus 'Bank’ xav tau yuav tsum tau hloov los ntawm cov lus 'Credit Card'. Tag nrho cov so ntawm lub filename yuav tsum nyob twj ywm rau hauv tib lub.

Nyob rau hauv thiaj li yuav ua tau qhov no, we will be using Qhov rais PowerShell with Regex.

Yuav kom sau tag nrho cov sij hawm thaum tsis nqe lus, peb siv lub ls hais kom ua, ces cov yeeb nkab cov zis

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

Lub rename hais kom yog siv tom qab cov yeeb nkab rau rename cov ntaub ntawv. $_ yog tus kwv uas tuas cov qhov tso zis ua ntej cov yeeb nkab, thiab peb yuav siv lub npe cuab yeej ntawm tus $_ yam khoom. -replace is used on strings in PowerShell, uas xyoo ib txoj hlua nrog ib tug tshiab txoj hlua. -replace yog ib tug muaj nuj nqi uas yuav siv sij hawm 2 tsis. Lub 1 parameter yog cov laus lub hlua uas peb xav kom hloov thiab lub 2nd parameter yog lub tshiab txoj hlua.

Capturing pawg nyob rau hauv regex pub peb kom qhov chaw ntawm txoj hlua. Nyob rau hauv regex, dab tsi muab tso rau hauv parenthesis no yuav siv tom qab. piv txwv li. (.+)Bank(.+) captures dab tsi ua ntej thiab tom qab lub cim 'Bank'. Peb yuav siv tau cov no yog tej qhov chaw, los ntawm kev siv $ thiab ib tug xov tooj. Lub 1 parenthesis captures rau hauv $1 thiab lub 2nd parenthesis captures rau hauv $2 thiab lwm yam…

Nyob rau hauv lub 1st parameter, peb qhia kom meej rau cov laus txoj hlua uas peb xav kom hloov, thiab nyob rau tib lub sij hawm peb ntes tus qhov chaw ntawm txoj hlua hais tias peb xav kom (capture). Nyob rau hauv lub 2nd parameter, lub tshiab txoj hlua siv lub ntes qhov chaw uas peb xav kom, raws li zoo raws li lub tshiab txoj hlua nws tus kheej xws li. '$1Credit Card$2'.

Tshwm:

Hom LastWrite sij hawm Length Lub npe
----       ---------      ----            ------   ----
-ib tug ----     10/06/2018     12:40              0 1 = 9.8.16 Credit Card Statement 298.67.pdf
piv ----     10/06/2018     12:40              0 2 = 9.9.16 Credit Card Statement 134.74.pdf
piv ----     10/06/2018     12:41              0 3 = 9.10.16 Credit Card Statement 123.43.pdf piv ----     10/06/2018     12:41              0 4 = 9.11.16 Credit Card Statement 134.54.pdf piv ----     10/06/2018     12:41              0 5 = 9.12.16 Credit Card Statement 157.98.pdf piv ----     10/06/2018     12:41              0 6 = 9.1.17 Credit Card Statement 123.45.pdf

Sau ntawv cia Ncua