Batch File "do if exist"

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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?
 
John Schneider said:
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?

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.
 
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
 
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
 
Brian said:
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

So why all the superfluous 'else goto [label]' portions? Execution
will fall through to the next label anyway.
 
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"?
 
Pegasus (MVP) said:
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.

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?
 
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 said:
Brian said:
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

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top