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 many files named like this:
YYY.XXXXXX

and i need to write a DOS batch command to rename like this:
YYYXXXXXX.ZZZ

YYY and ZZZ are fixed string, only XXXXXX is variable.

tried this with no success:
rename YYY.?????? YYY??????.ZZZ

share|improve this question

migrated from stackoverflow.com Mar 4 '13 at 22:15

This question came from our site for professional and enthusiast programmers.

    
Welcome to Stack Overflow! What have you tried? –  IronMan84 Mar 4 '13 at 18:55
    
Tried to rename like above with no success. It does work with a source filename without "." in the middle. I could find a workaround by deleting the "." in the filename –  ciquta Mar 4 '13 at 19:04
1  
James L has a good solution for your problem. The RENAME command alone cannot do what you want. See How does the Windows RENAME command interpret wildcards? to get an idea of what you can do with RENAME. –  dbenham Mar 4 '13 at 23:06
    
Thanks dbenham, this is like a goldmine, it's on my bookmarks already :) –  ciquta Mar 5 '13 at 10:21

1 Answer 1

You can use the for statement to do this because it gives you access to the filename and the extension separately:

for /f "tokens=1* delims=." %i in ('dir /b yyy.*') do ren "%i.%j" "%i%j.zzz"

Using tokens=1,2 delims=. causes it to split the value returned by dir /b yyy.* on the . into the %i and %j variables, where %i is the filename (or 'yyy'), and %j is the variable extension (without the leading dot .).

Use the command above if you are typing it directly from the command prompt. From a batch file, you need to double all of the % symbols like this:

for /f "tokens=1* delims=." %%i in ('dir /b yyy.*') do ren "%%i.%%j" "%%i%%j.zzz"

Make sure you run this command from the folder where all of the yyy.xxxxxx files reside.

share|improve this answer
    
+1 - I could not figure out how to get the extension without the period! I guess that's why I like powershell better... –  chue x Mar 4 '13 at 19:27
2  
+1, but better to use "tokens=1*" in case XXXXXX contains . –  dbenham Mar 4 '13 at 19:46
    
@dbenham - good catch, tokens=1* is much better. –  James L. Mar 4 '13 at 22:41
    
Thank you so much James, it worked like a charm!!! :) –  ciquta Mar 5 '13 at 10:20
    
This answer should have been accepted, maybe by the moderators, because it is actually excellent. –  mins Aug 1 at 13:59

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.