Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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?

share|improve this question
    
I know that it’s not exactly the answer to your specific question, but for these tasks I usually use RenameIt! utility. –  stansult Jan 8 at 21:37
    
What Windows version do you have? –  nixda Jan 8 at 21:55
    
I'm mostly on Win 7, but also need a solution for XP. –  omatai Jan 8 at 22:23

3 Answers 3

up vote 1 down vote accepted

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.

share|improve this answer

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

share|improve this answer

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.
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.