0

I have a large number of files with no particular naming pattern. I'll like to name them in double digit numerical order, for example:

test1.txt --> 01.txt
1test.txt --> 02.txt
te1st.txt --> 03.txt
t1est.txt --> 04.txt
tes1t.txt --> 05.txt
_test1.txt --> 06.txt
_1test.txt --> 07.txt
_te1st.txt --> 08.txt
_t1est.txt --> 09.txt
_tes1t.txt --> 10.txt

How can I do this using a PowerShell command?

GTS Joe
  • 3,612
  • 12
  • 52
  • 94

1 Answers1

2

You can use Get-ChildItem to enumerate the files piping to Rename-Item with delay-bind script block:

$counter = @{ value = 0 }
Get-ChildItem path\to\files -Filter *.txt -File |
    Rename-Item -NewName { '{0:D2}{1}' -f $counter['value']++, $_.Extension }
Santiago Squarzon
  • 41,465
  • 5
  • 14
  • 37