** IF EXSIST ** Help

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
ENU_USER*.*
Posts: 5
Joined: 10 Jan 2010 14:57
Location: U.S FL.

** IF EXSIST ** Help

#1 Post by ENU_USER*.* » 26 Apr 2010 06:35

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)

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: ** IF EXSIST ** Help

#2 Post by aGerman » 26 Apr 2010 07:40

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

Post Reply