if exist do not copy on bat script

IT Guy
IT Guy used Ask the Experts™
on
see bat script below - i want to make sure that when the script runs if the file exists it just go to end and if the file does not exist then execute
does this look right


@echo off  
setlocal
set userdir=%localappdata%\Microsoft\Office
set remotedir=\\domain.com\netlogon\Deployments\CustomOfficeRibbon
if not exist "%userdir%\Excel.officeUI" copy "%remotedir%\Excel.officeUI" "%userdir%\Excel.officeUI"
rem pause
endlocal
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
End-user support
Commented:
Looks good. It works here. Your code copies only if %userdir%\Excel.officeUI does not exist.

Per your question "... if the file exists it just go to end", it should be:
@echo off  
setlocal
set userdir=c:\local\test
set remotedir=\\domain.com\netlogon\Deployments\CustomOfficeRibbon
if exist "%userdir%\Excel.officeUI" goto :eof
copy "%remotedir%\Excel.officeUI" "%userdir%\Excel.officeUI"
rem pause
endlocal 

Open in new window


But either way works.
Qlemo"Batchelor", Developer and EE Topic Advisor
Top Expert 2015
Commented:
.. true, but if all you want to do is the copy, stay with your code. Goto is so 80s.
BTW, if you do not want to change the file name on copy, just provide the target (folder) path. That usually makes your script easier to change later.
Bill PrewIT / Software Engineering Consultant
Top Expert 2016

Commented:
And just so you know, keep in mind that BAT scripts have a basic IF ... THEN ... ELSE syntax as well, so you can do things like:

@echo off  
setlocal

set userdir=%localappdata%\Microsoft\Office
set remotedir=\\domain.com\netlogon\Deployments\CustomOfficeRibbon

if not exist "%userdir%\Excel.officeUI" (
    copy "%remotedir%\Excel.officeUI" "%userdir%\Excel.officeUI"
) else (
    echo INFO: File "%userdir%\Excel.officeUI" already exists, not copied.
)

rem pause
endlocal

Open in new window

~bp

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial