0

For example I have 190 files and I want to add L001 in the middle. How can I do that using cmd command, or PowerShell if it is better? Also some numbers are 2 digits some are 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
...

I want to add L001 on each one of them between S.. and ..R. For example PP-SD039_S22_L001_R1.fastq.gz

phuclv
  • 2,001
  • 1
  • 16
  • 41
  • if you refer to `cmd command` then it's probably off-topic here because cmd runs on Windows only – phuclv Jul 25 '22 at 04:47

1 Answers1

0

Just use a regex to match the S... and the R... parts then insert L001 between them

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>

Shorter version:

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

After verifying that the names are correct, remove -WhatIf/-wi to do the real renaming

phuclv
  • 2,001
  • 1
  • 16
  • 41