Tech Support > Microsoft Windows > Batch File "do if exist"
Batch File "do if exist"
Posted by John Schneider on March 3rd, 2006


I have loved playing with batch files since way, way back in the early DOS
days, and thought I knew most of the tricks & workarounds, but this one has
me completely stumped. I've been tweaking and playing with it for so long
now, I've got a splitting headache!

Under XP SP2, if I run this command in a batch file:
for /R c:\install %%I in (*.*) do if exist "d:%%~pnI" (echo yes) else (echo
no)
the output looks like this:
if exist "d:\install\abc.def" (echo yes ) else (echo no )
if exist "d:\install\ghi.jkl" (echo yes ) else (echo no )

In fact, no matter what I put after the "do if exist" part of the command,
it just displays it and does not execute it. Obviously, I'm trying to do
something more elaborate than just ehcoing yes or no, but I've simplified it
for now just to get it to work.

What the heck am I missing here?

Posted by Pegasus \(MVP\) on March 3rd, 2006



"John Schneider" <johnmsch@newsgroups.nospam> wrote in message
news:C98E6D78-E989-4ECC-AF5D-A84EC259F22C@microsoft.com...
Your batch file appears to be correct, hence there is something
else wrong. I suggest you start with something simple, then build
up on your results. Perhaps this will do:

@echo off
if exist d:\Friday03 rd /s /q Friday03
if exist d:\Friday03 (echo Friday03 exists) else (Friday03 does not exist)
md d:\Friday03
dir /ad d:\Fri*
if exist d:\Friday03 (echo Friday03 exists) else (Friday03 does not exist)

You may want to repeat this exercise for a file rather than
a folder.



Posted by Brian A. on March 3rd, 2006


This returned the proper echo for me. Perhaps it will help out some.
@echo off
for /R c:\install %%I in (*.*) do if exist "d:%%~pnI" (echo yes) else goto
nextif
:nextif
if exist "d:\install\abc.def" (echo yes) else goto next1
:next1
if exist "d:\program files\cleaners" (echo yes) else goto next2
:next2
if exist "d:\install\ghi.jkl" (echo yes) else goto eof
:eof

--

Brian A. Sesko { MS MVP_Shell/User }
Conflicts start where information lacks.
http://basconotw.mvps.org/

Suggested posting do's/don'ts: http://www.dts-l.org/goodpost.htm
How to ask a question: http://support.microsoft.com/kb/555375




"John Schneider" <johnmsch@newsgroups.nospam> wrote in message
news:C98E6D78-E989-4ECC-AF5D-A84EC259F22C@microsoft.com...

Posted by Brian A. on March 3rd, 2006


This one returns all echos.

@echo off
for /R c:\install %%I in (*.*) do if exist "d:%%~pnI" (echo yes) else (echo
no)
if exist "d:\install\abc.def" (echo yes) else (echo no)
if exist "d:\program files\cleaners" (echo yes) else (echo no)
if exist "d:\install\ghi.jkl" (echo yes) else (echo no)
if exist "d:\program files\graphics" (echo yes) else (echo no)

--

Brian A. Sesko { MS MVP_Shell/User }
Conflicts start where information lacks.
http://basconotw.mvps.org/

Suggested posting do's/don'ts: http://www.dts-l.org/goodpost.htm
How to ask a question: http://support.microsoft.com/kb/555375




"John Schneider" <johnmsch@newsgroups.nospam> wrote in message
news:C98E6D78-E989-4ECC-AF5D-A84EC259F22C@microsoft.com...

Posted by Phil Robyn on March 3rd, 2006


Brian A. wrote:
So why all the superfluous 'else goto [label]' portions? Execution
will fall through to the next label anyway.

--
Phil Robyn
University of California, Berkeley

Posted by DP on March 3rd, 2006


I am by no means a batchfile wizard. But is the command "do if exist" as you
have it? Or is it just "if exist"?




"John Schneider" <johnmsch@newsgroups.nospam> wrote in message
news:C98E6D78-E989-4ECC-AF5D-A84EC259F22C@microsoft.com...


Posted by Pegasus \(MVP\) on March 3rd, 2006


The "do" word is part of the "for" construct, e.g.

for %a in (*.*) do . . .


"DP" <dennispersica@bellsouth.net> wrote in message
news:aVSNf.17350$Pv1.8790@bignews6.bellsouth.net.. .


Posted by John Schneider on March 3rd, 2006


"Pegasus (MVP)" wrote:
Thanks Pegasus and everyone else for the replies. I shut the machine down
last night, and when I fired it up this morning, and tested the batch file
again, it worked like a charm.

Ever seen a grown man cry?


Posted by Brian A. on March 3rd, 2006


Simply testing and execution never failed.

--

Brian A. Sesko { MS MVP_Shell/User }
Conflicts start where information lacks.
http://basconotw.mvps.org/

Suggested posting do's/don'ts: http://www.dts-l.org/goodpost.htm
How to ask a question: http://support.microsoft.com/kb/555375




"Phil Robyn" <phil_robyn@comcast.net> wrote in message
news:29idnesklon2fZrZRVn-jA@comcast.com...