Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!

Regex problem: \xnn not recognized as a hex character

May
62
1
Within a directory which has files containing the year in their names, I want to change 2017 to 2018. I tried this:
Code:
ren /n "::^(.*201)7(.*)$" "::\1\x38\2"
but it wants to change, for example, aaa2017bbb.txt to aaa201x38bbb.txt.

The \xnn construct also does not seem to be recognized in a DIR command. For example, this works:
Code:
dir "::^.*2017.*$"
but this doesn't:
Code:
dir "::^.*201\x37.*$"

I'm using Perl regexes. What am I doing wrong?
 
I can't explain it, but this works.
Code:
v:\> dir ::.*201\x{37}.*

 Volume in drive V is DATA         Serial number is c007:d3e4
 Directory of  V:\::.*201\x{37}.*

2018-10-22  01:24               0  abc2017.txt
                   0 bytes in 1 file and 0 dirs
       6,741,622,784 bytes free
 
Unfortunately: While it works fine with DIR, as in your example, it does not with REN:
Code:
ren /n "::^(.*201)7(.*)$" "::\1\x{38}\2"
produces a file named aaa201x{38}bbb.txt.

Phoo.
 
[Corrected version]

You can enclose the back-reference numbers in braces. Also,, you don't need to include the context of the match.

Code:
C:\tmp> dir /mk ::2017
2018-10-22  09:08               0  aaa2017bbb.txt

C:\tmp> ren ::^(.*)2017(.*) ::\{1}2018\2
C:\tmp\aaa2017bbb.txt -> C:\tmp\aaa2018bbb.txt

C:\tmp> ren ::^(.*)2017 ::\{1}2018
C:\tmp\aaa2017bbb.txt -> C:\tmp\aaa2018bbb.txt

C:\tmp> ren ::2017(.*) ::2018\1
C:\tmp\aaa2017bbb.txt -> C:\tmp\aaa2018bbb.txt

C:\tmp> ren ::2017 ::2018
C:\tmp\aaa2017bbb.txt -> C:\tmp\aaa2018bbb.txt
 
Last edited:
Wow. Cool. That opens up more regex powers than I thought were available in TCC.

But how did you do the renames without using the REN command? That doesn't work for me.
Code:
[D:\foo]echo foo > aaa2017bbb.txt

[D:\foo]dir /a:-d /mk
10/22/2018   2:54               5  aaa2017bbb.txt

[D:\foo]::2017 ::2018
[D:\foo]dir /a:-d /mk
10/22/2018   2:54               5  aaa2017bbb.txt
 
Sorry! I had edited out the name of the command. Now corrected.
 
Last edited:
No, I do not want to do that. It makes no sense. I just wondered how you accomplished it in the example you gave above. Or did you simply edit out the lines where you used the REN command?
 
Unfortunately: While it works fine with DIR, as in your example, it does not with REN:
Code:
ren /n "::^(.*201)7(.*)$" "::\1\x{38}\2"
produces a file named aaa201x{38}bbb.txt.

Phoo.
Except for the back-references, I think the new name is a literal. It's not treated as a regex.
 
Sorry to resurrect this thread, but I'm still having troubles in the same (or a very similar) vein. Suppose I want to add an extension to all files whose names are wholly alphabetic (A-Z). My directory looks like this:
Code:
12/16/2018   3:05               0                  FILE.exe
12/16/2018   3:05               0                  FIRST
12/16/2018   3:05               0  NOTTHI~1        NOT THIS ONE
12/16/2018   3:05               0                  SECOND
12/16/2018   3:05               0                  TEXT.txt
12/16/2018   3:05               0                  THIRD
So my rename should work on the files named FIRST, SECOND, and THIRD. But the obvious commands (below) do not work:
Code:
ren ::^([a-z]+)$ ::\1.txt
ren "::^([a-z]+)$" "::\1.txt"
ren "::^([a-z]+)$" "::\{1}.txt"
The first one wants to work on all the files (and it succeeds in adding an extension to those files that already have an extension). For the files that should be renamed, all three commands want to rename them to the same name!

As mentioned above, I'm using Perl REs.

So get out the mallets and beat some understanding into my thick skull. Please.
 
I apologize (once again) for resurrecting this thread, but thought I'd post the answer, for anyone who happens to stumble across the thread.

In my response #13, I forgot to specify case-insensitivity (or, alternatively, specify upper-case characters). In addition, it wanted the double-quotes around the regexes. So either of the following would work:
Code:
ren "::(?i)^([a-z]+)$" "::\1.txt"
ren "::^([A-Za-z]+)$" "::\1.txt"
 

Similar threads

Back
Top