?
Solved

IF EXIST NOT WORKING for Batch file (command file) using Windows 7

Posted on 2012-09-17
12
Medium Priority
?
1,109 Views
Last Modified: 2012-10-11
I'm running the below mentioned batch file, it is copying the content without validating the access path i.e. if the accesspath for the "MSACCESS.EXE" is invalid, still it is executing the COPYCONTENT bookmark. I'm not sure IF EXIST line is correct. Please help me.

@ECHO OFF

Rem Set the path for the Office 2010 Access DB
SET AccessPath= "C:\Program Files (X86)\Microsoft Office 2010\Office14\MSACCESS.EXE"

IF EXIST AccessPath GOTO COPYCONTENT

:COPYCONTENT

Rem copy the contents file
XCOPY "c:\test\test.txt" /y "C:\Program Files\Test\" /i

Open in new window

0
Comment
Question by:learntechnology
  • 4
  • 3
  • 2
  • +2
12 Comments
 
LVL 22

Expert Comment

by:CompProbSolv
ID: 38407652
Try:
@ECHO OFF

Rem Set the path for the Office 2010 Access DB
SET AccessPath= "C:\Program Files (X86)\Microsoft Office 2010\Office14\MSACCESS.EXE"

IF EXIST %AccessPath% GOTO COPYCONTENT
Goto SkipCopy

:COPYCONTENT

Rem copy the contents file
XCOPY "c:\test\test.txt" /y "C:\Program Files\Test\" /i
                                 
:SkipCopy
0
 
LVL 22

Expert Comment

by:CompProbSolv
ID: 38407660
Note the two changes:
Adding % around AccessPath (otherwise it is not interpreted as a variable)
Goto SkipCopy  (to handle what happens when the If Exist is false
0
 
LVL 43

Accepted Solution

by:
Steve Knight earned 402 total points
ID: 38407670
You could simplify that a bit with code like this btw.  Anything between ( and ) gets executed if the IF command is true.


@ECHO OFF

Rem Set the path for the Office 2010 Access DB
SET AccessPath= "C:\Program Files (X86)\Microsoft Office 2010\Office14\MSACCESS.EXE"

IF EXIST %AccessPath% (
  XCOPY "c:\test\test.txt" /y "C:\Program Files\Test\" /i
)

or just
@echo off
SET AccessPath= "C:\Program Files (X86)\Microsoft Office 2010\Office14\MSACCESS.EXE"
IF EXIST %AccessPath% XCOPY "c:\test\test.txt" /y "C:\Program Files\Test\" /i

You can also use syntax such as this if you want to do if ... then .. else like:

IF EXIST %AccessPath% (
  echo It does exist
) ELSE (
  echo It doesn't
)

Steve
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 43

Expert Comment

by:Steve Knight
ID: 38407672
(Was typing that while previous comments added)
0
 
LVL 72

Assisted Solution

by:Qlemo
Qlemo earned 399 total points
ID: 38407678
CompProbSolv is correct, but using GOTO is old-style and so 80's. Nowadays we use statement blocks:
@echo off

Rem Set the path for the Office 2010 Access DB
SET AccessPath=C:\Program Files (X86)\Microsoft Office 2010\Office14\MSACCESS.EXE

REM Single command:
if EXIST "%AccessPath%"   XCOPY "c:\test\test.txt" /y "C:\Program Files\Test\" /i

REM more than one statement to execute:
IF EXIST "%AccessPath%" (
  Rem copy the contents file
  XCOPY "c:\test\test.txt" /y "C:\Program Files\Test\" /i
)

Open in new window

Also note that there is no space after the equal sign, and no double quotes in that SET - you should not provide the double quotes as part of the variable value, as that makes adding filenames etc. to it difficult.
I've shown you two different methods - you will use only one of them, of course.
0
 
LVL 60

Assisted Solution

by:Bill Prew
Bill Prew earned 399 total points
ID: 38407685
Just to add a little more, you have a space after the equal sign in the SET, you want to remove that.  In this case it didn't cause a problem, but the variable will actually have a leading space as part of it, and that can cause problems under different circumstances.

I also prefer not placing the quotes as part of the variable contents, but adding them in the code when the variable is referenced.  It's a little bit personal preference, but I think it's more common that way.  It's also a good habit so that if you need to concatenate two variables together to build a longer full pathname or the like, it will work okay.

Lastly, I's suggest you also use SETs for the other hard coded paths, it will make your code easier to read and maintain long term.  So I might end up here:

@ECHO OFF

Rem Set the path for the Office 2010 Access DB
SET AccessPath=C:\Program Files (X86)\Microsoft Office 2010\Office14\MSACCESS.EXE
SET ContentsFile=c:\test\test.txt
SET DestDir=C:\Program Files\Test\

IF EXIST "%AccessPath%" (
  Rem copy the contents file
  XCOPY "%ContentsFile%" "%DestDir%" /y /i
)

Open in new window

~bp
0
 
LVL 60

Expert Comment

by:Bill Prew
ID: 38407687
Q,

Great minds think alike...

~bp
0
 
LVL 72

Expert Comment

by:Qlemo
ID: 38407707
We three are meeting each other too often, I assume ;-).
0
 
LVL 60

Expert Comment

by:Bill Prew
ID: 38407710
No way, I can't get enough of you folks!

~bp
0
 

Author Comment

by:learntechnology
ID: 38407973
Thanks lot for ur valuable thoughts. Let me try and keep u all posted.
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 38408309
I sometimes use quotes in variables, sometimes not depending upon requirements, though not too much of an issue as the os is happy to deal with quotes around different bits of filenames at the same time.  Doesn't make much difference in this case but needed/better in others,  agreed.
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 38487853
Glad to see it was of help..... why the B grade?
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Recently, I read that Microsoft has analysed statistics for their security intelligence report. It revealed: still, the clear majority of windows users do their daily work as administrator. An administrative account is a burden, security-wise. My ar…
A few solutions to a problem some of us have been having when trying to add Hostgator email accounts to Outlook 2016 (will probably work with Outlook 2013 as well).
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Suggested Courses

616 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question