Rename several files to remove characters

I'v got several directories that need the file names to all be the same, so I need to remove the part that makes them distinct.

For example I need to rename in directory 'abc' the file 'access.abc.20051227.gz' to 'access.20051227.gz'

and in directory 'xyz' rename the file 'access.xyz.20051227.gz' to 'access.20051227.gz'

Thanks!
LVL 1
charvettAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Lee W, MVPTechnology and Business Process AdvisorCommented:
PROVIDED all the files are in that format, then use this single command line:

FOR /f "tokens=1,2,3,4 delims=." %a in ('dir /a-d /b') do echo ren "%a.%b.%c.%d" "%a.%c.%d"

NOTE: this will DISPLAY the command line executed for each file in the directory.  If you are satisfied this is going to do what you want it to, remove the echo from the line above.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
charvettAuthor Commented:
Thank you for the quick answer, it worked great. Only one thing, this is something I need to run nightly on the same directories. So I noticed if you run it once it works great, but when I use it a 2nd time it eliminates needed characters.

For example it renames 'access.20051227.gz' to 'access.gz' and so on.

If it's easier to hardcode the abc or xyz from my examples, I can do that because the format is consistent.

Thanks!
Lee W, MVPTechnology and Business Process AdvisorCommented:
If "access.abc" and "access.xyz" NEVER change, then I'd suggest this:

FOR /f "tokens=1,2,3,4 delims=." %a in ('dir /a-d /b access.abc.*') do echo ren "%a.%b.%c.%d" "%a.%c.%d"
charvettAuthor Commented:
OK, that worked great. One last (probably dumb) question.

When I put it in a batch file it errors

'C:\test>"FOR /f "tokens=1,2,3,4 delims=." a.c.a.d""
The system cannot find the path specified.'

But it works fine straight from the command line. What syntax do I use in a bat file?

Thanks!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft DOS

From novice to tech pro — start learning today.