0

How to Rename multiple files that I have from 1.txt, 2.txt to something like 1_A.txt 2_A.txt I tried the code below but it didn't work

dir | rename-item -NewName {$_.name -replace ".txt ","_A.txt"}

Can someone give me some guidance how to do this, since I have 100 files to rename

Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206
ValtZ
  • 1
  • 1
  • 1
    There's a trailing space in your search regex. Also, for robustness you should escape `.` as `\.` and anchor the match at the end of the string (`$`): `dir | rename-item -NewName { $_.name -replace '\.txt$', '_A.txt' }` – mklement0 Oct 21 '20 at 22:41

2 Answers2

1

…and based upon your tag:

for %I in ("*.txt") do @ren "%I" "%~nI_A%~xI"
Compo
  • 36,585
  • 5
  • 27
  • 39
0

You can use PowerShell:

 Replace:
             $_.name -replace ".txt ","_A.txt"
      To: 
             ($_.basename+'_A.txt')

 ls *.txt | ? {ren $_.fullname -New ($_.basename+'_A.txt')}
Io-oI
  • 2,514
  • 3
  • 22
  • 29