Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Checking if a file with pattern "abc" exists in dir?

687 views
Skip to first unread message

Peter Blatt

unread,
Nov 17, 2008, 2:29:58 AM11/17/08
to
Assume I have a variable %mypattern% which contains a pattern like "abc".

Now I want to check if a certain directory (e.g. D:\mydir) contains at least
one file which matches the pattern. How can I check this?

I should look like similar to:

dir "D:\mydir\*%mypattern%*" >nul
if errorlevel=0 (echo found) else (echo not found)

Can I somehow omit the errorlevel question and join the two statements?

Peter

ten.n...@virgin.net

unread,
Nov 17, 2008, 3:17:56 AM11/17/08
to

This should do it!
::----- START -----
@Dir/b/a-d "D:\MyDir\*%mypattern%*">Nul 2>&1&&Echo/Found||Echo/Not Found
::------ END ------

Zaphod Beeblebrox

unread,
Nov 17, 2008, 8:17:25 AM11/17/08
to

"Peter Blatt" <pet...@redseven.com> wrote in message
news:49211d75$0$32678$9b4e...@newsspool2.arcor-online.net...

How about:

if exist *%mypattern%*.* (echo found) else (echo not found)

--
Zaphod

No matter where you go, there you are!


Zaphod Beeblebrox

unread,
Nov 17, 2008, 8:24:02 AM11/17/08
to

"Zaphod Beeblebrox" <Zaphod.Ariszt...@gmail.com> wrote in
message news:gfrqt7$5oj$1...@news.motzarella.org...

It should be obvious, but to add a specific directory as your original
example contained:

if exist d:\mydir\*%mypattern%*.* (echo found) else (echo not found)

Also, this works under XP SP3, no idea if it works on other Windows
versions.

ten.n...@virgin.net

unread,
Nov 17, 2008, 9:34:04 AM11/17/08
to

However it doesn't differentiate between files and directories!

foxidrive

unread,
Nov 17, 2008, 10:46:04 AM11/17/08
to
On 17 Nov 2008 07:29:58 GMT, pet...@redseven.com (Peter Blatt) wrote:

>Assume I have a variable %mypattern% which contains a pattern like "abc".
>
>Now I want to check if a certain directory (e.g. D:\mydir) contains at least
>one file which matches the pattern. How can I check this?

>Can I somehow omit the errorlevel question and join the two statements?

dir /b "D:\mydir\*%mypattern%*" >nul 2>&1 &&(echo found)||(echo not found)

ten.n...@virgin.net

unread,
Nov 17, 2008, 11:01:39 AM11/17/08
to

Although almost identical to my solution, this also doesn't differentiate
between files and directories.

Mike Walsh

unread,
Nov 17, 2008, 1:48:51 PM11/17/08
to

If exist D:\mydir\*%mypattern%* (echo found) else (echo not found)

--
Mike Walsh

ten.n...@virgin.net

unread,
Nov 17, 2008, 4:55:08 PM11/17/08
to

Am I having a case of déjà vu?

This does not differentiate between files and directories!

I'm sure that there's a good chance the OP will not have such an
occurrence, but owing to the lack of our knowledge of both the pattern and
directory contents our solutions must not make that assumption!

To be honest, after over 10½ hours since my initial and first response to
this question, I'm disappointed that the question is still being answered
with such disregard, not only to the question but the responses which
preceed them.

Timo Salmi

unread,
Nov 18, 2008, 12:05:37 AM11/18/08
to
ten.n...@virgin.net <ten.n...@virgin.net> wrote:
> On Mon, 17 Nov 2008 13:48:51 -0500, Mike Walsh wrote:
>> Peter Blatt wrote:
>>> dir "D:\mydir\*%mypattern%*" >nul
>>> if errorlevel=0 (echo found) else (echo not found)
>>> Can I somehow omit the errorlevel question and join the two statements?

>> If exist D:\mydir\*%mypattern%* (echo found) else (echo not found)

> Am I having a case of déjà vu?

In fact, probably in more senses than one
75} How do I detect if an object is a file or a folder?
http://www.netikka.net/tsneti/info/tscmd075.htm

Not the top FAQ, but perhaps the one where most mistakes or at least
logic sloppiness occurs.

> This does not differentiate between files and directories!

Extending on foxidrive's one-liner
dir /b /a:-d "D:\mydir\*%mypattern%*" >nul 2>&1 &&(echo files
found)||(echo files not found)

All the best, Timo

--
Prof. Timo Salmi mailto:t...@uwasa.fi ftp & http://garbo.uwasa.fi/
Hpage: http://www.uwasa.fi/laskentatoimi/english/personnel/salmitimo/
Department of Accounting and Finance, University of Vaasa, Finland
Useful CMD script tricks http://www.netikka.net/tsneti/info/tscmd.htm

Zaphod Beeblebrox

unread,
Nov 18, 2008, 8:00:56 AM11/18/08
to

<ten.n...@virgin.net> wrote in message
news:gzs4a41z15nu$.1qqepxw6xu247$.dlg@40tude.net...

I never claimed to be perfect :-) Only the OP can say whether or not
that is important in his circumstance. If it is, your response (that
finally showed up on my server) will hopefully serve his needs.

Zaphod Beeblebrox

unread,
Nov 18, 2008, 8:03:40 AM11/18/08
to

<ten.n...@virgin.net> wrote in message
news:g3zm29joxt1i$.69nwpccobrgg$.dlg@40tude.net...

One must remember that newsgroup post propogation is at times slow and
unreliable. For example, your first response to the OP wasn't present
on my server yesterday when I last checked this group, but is there this
morning. Others could easily be experiencing similar delays.

ten.n...@virgin.net

unread,
Nov 18, 2008, 8:49:25 AM11/18/08
to

That appears likely, since yourself, foxidrive and Timo all appeared to
miss my opening post. (However it is odd that Timo's response came well
over twenty hours after mine). All I know is that the posts propagated to
my news server and to Google Groups almost immediately.

Batchman

unread,
Nov 23, 2008, 12:46:00 AM11/23/08
to
Hi Peter,

On Mon, 17 Nov 2008 07:29:58 +0000, Peter Blatt wrote:

[snip]


>
> I should look like similar to:
>
> dir "D:\mydir\*%mypattern%*" >nul
> if errorlevel=0 (echo found) else (echo not found)
>
> Can I somehow omit the errorlevel question and join the two statements?
>

If you mean that you're looking for a fileNAME containing the pattern, you
should just be able to use...

set ok=N
if exist d:\mydir\*%mypattern%* set ok=Y
echo %ok%

If that doesn't do the job, try redirecting the DIR output to a temp. file
and using FIND to test the file for %mypattern%. FIND returns errorlevel
which can be used to indicate success (or not).

Batchman

Todd Vargo

unread,
Nov 23, 2008, 8:55:45 AM11/23/08
to
ten.n...@virgin.net wrote:
> Zaphod Beeblebrox wrote:

ISTM, several helpers are forgetting (or did not know) that IF EXIST which
only returns files on DOS/9x/Me systems, also includes directories on NTish
systems. Had this explanation been provided, instead of posting déjà vu
remarks or propagation debates, we possibly would not be seeing yet another
IF EXIST solution offered 5 days later from Batchman which also does not
differentiate between files and directories.

And to your response to foxidrive, a explanation about the /A-D switch usage
with DIR would be more helpful to the OP (and others) than just saying it
"also doesn't differentiate between files and directories".

Just my observation.

--
Todd Vargo
(Post questions to group only. Remove "z" to email personal messages)

ten.n...@virgin.net

unread,
Nov 23, 2008, 11:36:54 AM11/23/08
to
On Sun, 23 Nov 2008 08:55:45 -0500, Todd Vargo wrote:

> And to your response to foxidrive, a explanation about the /A-D switch usage
> with DIR would be more helpful to the OP (and others) than just saying it
> "also doesn't differentiate between files and directories".
>
> Just my observation.

It more than likely would; however bearing in mind that my post appeared
missing on many servers due to propagation issues, the same might be said
for Timo's 'Extending on' post!

foxidrive

unread,
Nov 23, 2008, 12:53:00 PM11/23/08
to
On Sun, 23 Nov 2008 16:36:54 +0000, ten.n...@virgin.net wrote:

>bearing in mind that my post appeared
>missing on many servers due to propagation issues

I saw your post - after I read the OP and replied. Mea culpa. :)

Todd Vargo

unread,
Nov 23, 2008, 3:52:31 PM11/23/08
to

"Appeared missing" seems to be the key notion. Perhaps others just
read/answer OPs messages before reading responses. Batchman posted his IF
EXIST solution 6 days after your first "doesn't differentiate" post and 5
days after this propagation discussion.

0 new messages