Link to home
Start Free TrialLog in
Avatar of learntechnology
learntechnologyFlag for United States of America

asked on

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

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

Avatar of CompProbSolv
CompProbSolv
Flag of United States of America image

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
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
ASKER CERTIFIED SOLUTION
Avatar of Steve Knight
Steve Knight
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
(Was typing that while previous comments added)
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Bill Prew
Bill Prew

Q,

Great minds think alike...

~bp
We three are meeting each other too often, I assume ;-).
No way, I can't get enough of you folks!

~bp
Avatar of learntechnology

ASKER

Thanks lot for ur valuable thoughts. Let me try and keep u all posted.
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.
Glad to see it was of help..... why the B grade?