如何一次性更改多个文件的中间名称?

如何一次性更改多个文件的中间名称?

例如我有 190 个文件,我想L001在中间添加。我该如何使用 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
...

我想在和L001之间添加它们中的每一个。例如S....RPP-SD039_S22_L001_R1.fastq.gz

答案1

只需使用正则表达式来匹配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进行真正的重命名

相关内容