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
This should do it!
::----- START -----
@Dir/b/a-d "D:\MyDir\*%mypattern%*">Nul 2>&1&&Echo/Found||Echo/Not Found
::------ END ------
How about:
if exist *%mypattern%*.* (echo found) else (echo not found)
--
Zaphod
No matter where you go, there you are!
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.
However it doesn't differentiate between files and directories!
>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)
Although almost identical to my solution, this also doesn't differentiate
between files and directories.
If exist D:\mydir\*%mypattern%* (echo found) else (echo not found)
--
Mike Walsh
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.
>> 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
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.
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.
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.
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
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)
> 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!
>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. :)
"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.