Please Help renaming files

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
fatman01
Posts: 1
Joined: 16 Jul 2009 18:49

Please Help renaming files

#1 Post by fatman01 » 16 Jul 2009 19:32

Hi Everyone

I’m tiring to make batch file to rename oeminfo.ini to oeminfoBAK.ini and if it exist to auto rename oeminfoBAK1.ini and BAK2 and BAK3 and so on..
I would also like to rename oemlogo.bmp to oemlogoBAK.bmp at the same time and the same way.

if exist c:\windows\system32\oeminfoBAK.ini ren c:\windows\system32\oeminfo.ini oeminfoBAK2.ini

if exist c:\windows\system32\oeminfo.ini ren c:\windows\system32\oeminfo.ini oeminfoBAK.ini

if exist c:\windows\system32\oemlogoBAK.bmp ren c:\windows\system32\oemlogo.bmp oemlogoBAK2.bmp

if exist c:\windows\system32\oemlogo.bmp ren c:\windows\system32\oemlogo.bmp oemlogoBAK.bmp

It works only once before errors. I know there is a better way.

Any help would be deeply appreciated.

Thanks in advance.

avery_larry
Expert
Posts: 391
Joined: 19 Mar 2009 08:47
Location: Iowa

#2 Post by avery_larry » 16 Jul 2009 20:02

UNTESTED:

Code: Select all

setlocal enabledelayedexpansion
for %%a in ("oeminfo ini" "oemlogo bmp") do call :process %%~a
goto :eof

:process
if not exist %1.%2 echo %1.%2 doesn't exist && goto :eof
if not exist %1BAK.%2 ren %1.%2 %1BAK.%2 && goto :eof
set idx=1
:loop
if exist %1BAK%idx%.%2 set /a idx+=1 && goto :loop
if not %idx%==1 (
   for /l %%a in (%idx%,-1,2) do (
      set /a old=%%a - 1
      ren %1BAK!old!.%2 %1BAK%%a.%2
   )
)
ren %1BAK.%2 %1BAK1.%2
ren %1.%2 %1BAK.%2
goto :eof

   

Post Reply