Regex renaming

Jan 19, 2011
Norman, OK
I have 20 files, 1.txt, 2.txt, ... 19.txt, 20.txt.
I want to rename 1-9 as 01-09.

Why does this regex rename change not only the single digit text files, but the double digits also?

The rename command is shown and defined as follows...
*ren /n ::^(\d)\.txt$ ::0\1.txt
*ren - rename command overriding aliases

/n   - just show me what would happen

::   - it's the source filename regex
^    - beginning of line
(    - start capture group
\d   - a single digit
)    - end capture group
\.   - escaped period overriding the wildcard any character effect
txt  - the text string "txt"
$    - end of line

::   - it's the target file regex
0    - the text string "0"
\1   - use the first matching group from the source filename regex
.txt - the text string ".txt"
And the result is...
12:20:45 $ *ren /n ::^(\d)\.txt$ ::0\1.txt
C:\temp\1.txt -> C:\temp\01.txt
C:\temp\10.txt -> C:\temp\100.txt
C:\temp\11.txt -> C:\temp\101.txt
C:\temp\12.txt -> C:\temp\102.txt
C:\temp\13.txt -> C:\temp\103.txt
C:\temp\14.txt -> C:\temp\104.txt
C:\temp\15.txt -> C:\temp\105.txt
C:\temp\16.txt -> C:\temp\106.txt
C:\temp\17.txt -> C:\temp\107.txt
C:\temp\18.txt -> C:\temp\108.txt
C:\temp\19.txt -> C:\temp\109.txt
C:\temp\2.txt -> C:\temp\02.txt
C:\temp\20.txt -> C:\temp\200.txt
C:\temp\3.txt -> C:\temp\03.txt
C:\temp\4.txt -> C:\temp\04.txt
C:\temp\5.txt -> C:\temp\05.txt
C:\temp\6.txt -> C:\temp\06.txt
C:\temp\7.txt -> C:\temp\07.txt
C:\temp\8.txt -> C:\temp\08.txt
C:\temp\9.txt -> C:\temp\09.txt
    20 files would be renamed
May 20, 2008
Syracuse, NY, USA
The '^' is getting lost. Either use '^^' or "quote" the regex.

t:\> *ren /n ::^(\d)\.txt$ ::0\1.txt
T:\1.txt -> T:\01.txt
T:\10.txt -> T:\100.txt
     2 files would be renamed

t:\> *ren /n ::^^(\d)\.txt$ ::0\1.txt
T:\1.txt -> T:\01.txt
     1 file would be renamed

t:\> *ren /n ::"^(\d)\.txt$" ::0\1.txt
T:\1.txt -> T:\01.txt
     1 file would be renamed
Similar threads
Thread starter Title Forum Replies Date
P Renaming with a RegEx Support 1
P Renaming files with regex. Support 6
F %@regex["^-","-a"] returns 0, "^-" =~ "-a" is false (no match) Support 4
vefatica TPIPE: unbalanced escaped quotes in a regex? Support 5
rps Regex problem: \xnn not recognized as a hex character Support 0
old coot Regex problem: \xnn not recognized as a hex character Support 12
R Regex using ^ Support 2
T Regex engine doesn't recognize native DOS line endings Support 2
P Simple RegEx copy Support 9
samintz WAD Regex Analyzer Support 1
D How to? Use typed envars using regex. Support 3
R How to? use @everything perl regex Support 2
C v18 regex help please Support 1
C Font of RegEx Analyzer Support 0
D Regex problem Support 17
mikea How to? Regex match when there shouldn't be (?) Support 18
JohnQSmith Fixed Copying with regex (several issues) Support 7
D Help needed to get a regex to work Support 3
thedave WAD Regex match on \h Support 5
Ville Regex & conditionals Support 9
samintz Regex Rename Support 2
vefatica @REGEX: behavior vs. documentation Support 2
vefatica @REGEX revisited Support 4
vefatica @REGEX question Support 6
vefatica Regex question Support 5
B Regex and Replace Support 6
Stefano Piccardi detecting BOM, FFIND multibyte regex Support 18
dcantor FFIND syntax -- is /E"regex" /X supported? Support 2
B "Fun" with DO and Regex Support 12
P Need to use a regex in a "for" loop. Support 54
J Extended directory search - cure by renaming tcmd.ini Support 10
nickles Renaming bug? Support 9
S GPF after successfully renaming directories on an ftp server Support 2
M More file-renaming problems involving commas... Support 0
M Another problem with renaming files... Support 3
gunkelkarl renaming files Support 14

Similar threads