Rename

一次如何在多個文件中更改名稱

  • July 25, 2022

例如,我有 190 個文件,我想在中間添加M001,如果更好的話,我該如何使用 cmd 命令或 powershell 來做到這一點,而且有些數字是 2 位數,有些是 1。

PP-SD01_S1_R1.fastq.gz PP-SD05_S1_R2.fastq.gz

PP-SD09_S20_R1.fastq.gz PP-SD025_S20_R2.fastq.gz

PP-SD039_S22_R1.fastq.gz PP-SD039_S22_R2.fastq.gz …

我想在“S..“L001”..R.之間的每一個上添加“L001” 前任。PP-SD039_S22_L001_R1.fastq.gz

只需使用正則表達式來匹配S...和部分然後在它們之間R...插入L001

PS /tmp> Get-ChildItem -File -Filter *.fastq.gz | Rename-Item -NewName { $_.Name -replace '(S\d+)_(R\d+)', '$1_L001_$2' } -WhatIf
What if: Performing the operation "Rename File" on target "Item: /tmp/PP-SD01_S1_R1.fastq.gz Destination: /tmp/PP-SD01_S1_L001_R1.fastq.gz".
What if: Performing the operation "Rename File" on target "Item: /tmp/PP-SD025_S20_R2.fastq.gz Destination: /tmp/PP-SD025_S20_L001_R2.fastq.gz".
What if: Performing the operation "Rename File" on target "Item: /tmp/PP-SD039_S22_R1.fastq.gz Destination: /tmp/PP-SD039_S22_L001_R1.fastq.gz".
What if: Performing the operation "Rename File" on target "Item: /tmp/PP-SD039_S22_R2.fastq.gz Destination: /tmp/PP-SD039_S22_L001_R2.fastq.gz".
What if: Performing the operation "Rename File" on target "Item: /tmp/PP-SD05_S1_R2.fastq.gz Destination: /tmp/PP-SD05_S1_L001_R2.fastq.gz".
What if: Performing the operation "Rename File" on target "Item: /tmp/PP-SD09_S20_R1.fastq.gz Destination: /tmp/PP-SD09_S20_L001_R1.fastq.gz".
PS /tmp>

較短的版本:

dir *.fastq.gz | ren -Ne { $_.Name -replace '(S\d+)_(R\d+)', '$1_L001_$2' } -wi

驗證名稱正確後,刪除-WhatIf/-wi進行真正的重命名

引用自:https://unix.stackexchange.com/questions/711141