If exist batch command not working in WinXp

G

Guest

I am trying to use if exist/if not exist in a batch file in Win XP to test
for the presence of files in a directory. However, it is not working, showing
that files exist even if the directory is empty. Am I dong anything wrong?
Any solutions ?
 
K

Ken Blake, MVP

I am trying to use if exist/if not exist in a batch file in Win XP to test
for the presence of files in a directory. However, it is not working, showing
that files exist even if the directory is empty. Am I dong anything wrong?


Probably. But it's hard to know what without seeing what you're doing.
Post the batch file here.
 
S

Stan Brown

Mon, 30 Jul 2007 22:28:00 -0700 from Kitroop
I am trying to use if exist/if not exist in a batch file in Win XP to test
for the presence of files in a directory. However, it is not working, showing
that files exist even if the directory is empty. Am I dong anything wrong?
Any solutions ?

Sorry, my crystal ball is in the shop.

Please show us the exact command you are using. Copy/paste it, don't
retype it.
 
G

Guest

Sorry for the delay.
The batch file is as follows:
~~~~
if exist c:\temp\testing\*.* goto yes
goto no
:yes
echo file exists
goto end
:no
echo file not exists
goto end
:end
~~~~
The batch file is in the dir C\temp. There are no files under dir "testing".
However the output of the batch file is "file exists".

I have run the same batch file successfully on a dos pc.
Thanks for any help.

Kitroop.
 
S

Stan Brown

Thu, 2 Aug 2007 04:10:00 -0700 from Kitroop
"Ken Blake, MVP" wrote:
The batch file is as follows:
~~~~
if exist c:\temp\testing\*.* goto yes

Note -- in XP use "*" for wildcards. "*.*" is okay but sooooo 1980s.
:)
goto no
:yes
echo file exists
goto end
:no
echo file not exists
goto end
:end
~~~~
The batch file is in the dir C\temp. There are no files under dir
"testing". However the output of the batch file is "file exists".

There are files in c:\temp\testing -- they are called "." and "..".

The only way I can think of to test whether a directory is empty is
to first make your "if exist *" test and then try an RMDIR -- if the
latter fails then the directory was not empty.

There ought to be a better way, and in fact there is in the 4DOS/4NT
shells (which I use). But in native XP I think you have to do it the
hard way.
 
A

Ayush

[Kitroop] wrote-:
Sorry for the delay.
The batch file is as follows:
~~~~
if exist c:\temp\testing\*.* goto yes
goto no
:yes
echo file exists
goto end
:no
echo file not exists
goto end
:end
~~~~
The batch file is in the dir C\temp. There are no files under dir "testing".
However the output of the batch file is "file exists".

I have run the same batch file successfully on a dos pc.
Thanks for any help.


~~~~
dir /b /a-d c:\temp >nul 2>nul && GOTO yes
goto no
:yes
echo file exists
goto end
:no
echo file not exists
goto end
:end
~~~~



Good Luck, Ayush.
 
S

Stan Brown

Fri, 03 Aug 2007 08:23:14 -0700 from Ken Blake, MVP
The * always worked instead of *.*, even in the 1980s.

There's no way to prove it now, because I no longer have my DOS
books, so I'll just say that your memory is significantly different
from mine.

"*" _did_ work for all files in certain DOS shells, but not in plain-
vanilla DOS through at least 5.0, unless I am very much mistaken.
 
O

Og

"*" _did_ work for all files in certain DOS shells, but not in plain-
vanilla DOS through at least 5.0, unless I am very much mistaken.

--
"The Internet is famously powered by the twin engines of
bitterness and contempt." -- Nathan Rabin, /The Onion/
Stan Brown, Oak Road Systems, Tompkins County, New York, USA
http://OakRoadSystems.com/

Disk Operating System Version 3.3 Reference by IBM
First Edition (April 1987)
Index page X-1
"Special Characters: * - global file name character" -- 2-8
From page 2-8:
Quote
"The * Character"
"An * in a file name or in a file name extensions indicates that any
character can occupy that position and all the remaining positions in the
file or extension. For example,
"dir ab*.xyz"
"lists all directory entries on the default drive with file names that begin
with AB and have an extension of XYZ.
"In this case, the file names may be from 2 to 8 characters in length."
End quote.

Steve
 
S

Stan Brown

Disk Operating System Version 3.3 Reference by IBM
First Edition (April 1987)
Index page X-1
"Special Characters: * - global file name character" -- 2-8
From page 2-8:
Quote
"The * Character"
"An * in a file name or in a file name extensions indicates that any
character can occupy that position and all the remaining positions in the
file or extension. For example,
"dir ab*.xyz"
"lists all directory entries on the default drive with file names that begin
with AB and have an extension of XYZ.
"In this case, the file names may be from 2 to 8 characters in length."

Well, yes. But that is not the same as the PP's statement that a
plain * would match all files. It does in XP, and may have done in
some earlier Windows, but not in DOS at least through 5.0 -- again,
unless I am remembering wrong.

* would match ABCD, XYZ, etc but not anything with a "." in it.

*.* would match anything with or without a "." in it.

Nowadays, in XP and quite possibly earlier,

*. matches anything without a dot

* or *.* matches anything with or without a dot

I've spent some time googling but can't find a DOS manual on line.
You think someone at Microsoft would have put it up, just for the
nostalgia factor. (Copyright problems, at least in the US, keep
anyone else from posting it.)
 
G

Guest

Thanks for the tip. It works fine.

Kitroop

Ayush" <"ayushmaan.j[aatt]gmail.com said:
[Kitroop] wrote-:
Sorry for the delay.
The batch file is as follows:
~~~~
if exist c:\temp\testing\*.* goto yes
goto no
:yes
echo file exists
goto end
:no
echo file not exists
goto end
:end
~~~~
The batch file is in the dir C\temp. There are no files under dir "testing".
However the output of the batch file is "file exists".

I have run the same batch file successfully on a dos pc.
Thanks for any help.


~~~~
dir /b /a-d c:\temp >nul 2>nul && GOTO yes
goto no
:yes
echo file exists
goto end
:no
echo file not exists
goto end
:end
~~~~



Good Luck, Ayush.
 

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

Top