0

I have read as much of How does the Windows RENAME command interpret wildcards as I can understand, and still cannot decide if there is some wildcard combination you can use to make RENAME do this:

Old file names: abc001.txt, abc002.txt, abc003.txt, ... etc
New file names: demo_001.txt, demo_002.txt, demo_003.txt, ... etc
or: x001.txt, x002.txt, x003.txt, ... etc
or: file01.txt, file02.txt, file03.txt, ... etc

What I do know is that rename abc*.txt xyz*.txt will change all abc prefixes to xyz, but only because abc and xyz are of identical length. Is there a simple rename command which will cope with either a shorter or longer prefix... or do you have to use a batch process to carry out such renames?

omatai
  • 313
  • 5
  • 18

3 Answers3

1

In PowerShell for your first example you could use:

Dir C:\folder | ren –NewName {$_.name -replace "abc","demo_"}

Add -whatif as last parameter if you want to test the command before executing it

nixda
  • 26,823
  • 17
  • 108
  • 156
1

The answer is No, you cannot change the length of a file name prefix like you want using just the RENAME command with wildcards. It will require some amount of more complex scripting, or else a non-standard 3rd party tool.

If you read the rules carefully, you will see that the wildcards in the source mask have no bearing in the rename result - they only filter which files are renamed. The non-wildcard prefix characters in the target mask substitute one for one with the characters in the original name.

dbenham
  • 11,194
  • 6
  • 30
  • 47
0

Here's the way to do it with JP Software's TCC/LE, a command interpreter whose REN command supports regular expressions:

ren ::abc(\d+.txt) ::demo_\1

Without regular expressions, it's still a one-liner. One simply uses the @INSTR[] variable function and the FOR command:

for i in (abc*.txt) do ren %i demo_%@instr[3,,%i]

Further reading

  • JP Software. REN. Take Command / TCC Help.
  • JP Software. FOR. Take Command / TCC Help.
  • JP Software. @INSTR. Take Command / TCC Help.
JdeBP
  • 26,613
  • 1
  • 72
  • 103