Page 1 of 1

** IF EXSIST ** Help

Posted: 26 Apr 2010 06:35
by ENU_USER*.*
Need script to rename file and use the "if exist and Go To" to jump to next name if the file already exists, without stopping and asking for any user intervention.

such as: ren "%systemroot%\system32\shell32.dll" "shell32.dll.OLD"
and if that exsist then Go To next such as OLD2 or OLD3 etc.
Any ideas on how to do this?
Thanks for any help.. 8)

Re: ** IF EXSIST ** Help

Posted: 26 Apr 2010 07:40
by aGerman
You could try something like that:

Code: Select all

@echo off &setlocal

set "Xpath=%systemroot%\system32\"
set "Xname=shell32.dll"
set "Xext=.old"

:: check if the origin file exists
if not exist "%Xpath%%Xname%" goto :EOF

:: look for extension .old
if not exist "%Xpath%%Xname%%Xext%" (
  set "Newext=%Xext%"
  goto REN
)

:: look for extension .old2 etc.
set /a n=1
:LOOP
set /a n+=1
if not exist "%Xpath%%Xname%%Xext%%n%" (
  set "Newext=%Xext%%n%"
  goto REN
)
goto LOOP

:: rename the file
:REN
ren "%Xpath%%Xname%" "%Xname%%Newext%"


Regards
aGerman