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

1
  • if you refer to cmd command then it's probably off-topic here because cmd runs on Windows only Commented Jul 25, 2022 at 4:47

1 Answer 1

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

1
  • Thank you so much for your help, I will try using pearl as you suggest the code to me Commented Jul 25, 2022 at 6:05

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.