PC Review


Reply
Thread Tools Rate Thread

Command question (IF EXIST)

 
 
Howard Brazee
Guest
Posts: n/a
 
      17th Nov 2006

I tried the following command:
If exist "%USERPROFILE%\My Documents\My Pictures\home\*.*" dir
"%USERPROFILE%\My Documents\My Pictures\home\"
If exist "%USERPROFILE%\My Documents\My Pictures\work\*.*" dir
"%USERPROFILE%\My Documents\My Pictures\work\"

& it doesn't matter whether the files exist or not (my test results
follow).

How do I fix this test to do what I know whether a directory is empty?

C:\BELFRY>If exist "C:\Documents and Settings\brazee\My Documents\My
Pictures\ho
me\*.*" dir "C:\Documents and Settings\brazee\My Documents\My
Pictures\home\"
Volume in drive C has no label.
Volume Serial Number is xxxxxx

Directory of C:\Documents and Settings\brazee\My Documents\My
Pictures\home

11/16/2006 08:17 AM <DIR> .
11/16/2006 08:17 AM <DIR> ..
0 File(s) 0 bytes
2 Dir(s) 61,803,278,336 bytes free

C:\BELFRY>If exist "C:\Documents and Settings\brazee\My Documents\My
Pictures\wo
rk\*.*" dir "C:\Documents and Settings\brazee\My Documents\My
Pictures\work\"
Volume in drive C has no label.
Volume Serial Number is xxxxxx

Directory of C:\Documents and Settings\brazee\My Documents\My
Pictures\work

11/17/2006 08:04 AM <DIR> .
11/17/2006 08:04 AM <DIR> ..
11/17/2006 07:21 AM 141,377 Hawai'i Volcanoes National
Park, Hawai'i,
1983.jpg
1 File(s) 141,377 bytes
 
Reply With Quote
 
 
 
 
Howard Brazee
Guest
Posts: n/a
 
      17th Nov 2006
On Fri, 17 Nov 2006 08:11:54 -0700, Howard Brazee <(E-Mail Removed)>
wrote:

>If exist "%USERPROFILE%\My Documents\My Pictures\home\*.*" dir
>"%USERPROFILE%\My Documents\My Pictures\home\"
>If exist "%USERPROFILE%\My Documents\My Pictures\work\*.*" dir
>"%USERPROFILE%\My Documents\My Pictures\work\"


If I replace the *.* with *.JPG, then those commands work fine.
Unfortunately, that's not what I need to do.
 
Reply With Quote
 
Pegasus \(MVP\)
Guest
Posts: n/a
 
      17th Nov 2006
Try this:

dir "%USERPROFILE%\My Documents\My Pictures\home" | find /i " 0 File(s)" >
nul || dir
"%USERPROFILE%\My Documents\My Pictures\home\"


"Howard Brazee" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>
> I tried the following command:
> If exist "%USERPROFILE%\My Documents\My Pictures\home\*.*" dir
> "%USERPROFILE%\My Documents\My Pictures\home\"
> If exist "%USERPROFILE%\My Documents\My Pictures\work\*.*" dir
> "%USERPROFILE%\My Documents\My Pictures\work\"
>
> & it doesn't matter whether the files exist or not (my test results
> follow).
>
> How do I fix this test to do what I know whether a directory is empty?
>
> C:\BELFRY>If exist "C:\Documents and Settings\brazee\My Documents\My
> Pictures\ho
> me\*.*" dir "C:\Documents and Settings\brazee\My Documents\My
> Pictures\home\"
> Volume in drive C has no label.
> Volume Serial Number is xxxxxx
>
> Directory of C:\Documents and Settings\brazee\My Documents\My
> Pictures\home
>
> 11/16/2006 08:17 AM <DIR> .
> 11/16/2006 08:17 AM <DIR> ..
> 0 File(s) 0 bytes
> 2 Dir(s) 61,803,278,336 bytes free
>
> C:\BELFRY>If exist "C:\Documents and Settings\brazee\My Documents\My
> Pictures\wo
> rk\*.*" dir "C:\Documents and Settings\brazee\My Documents\My
> Pictures\work\"
> Volume in drive C has no label.
> Volume Serial Number is xxxxxx
>
> Directory of C:\Documents and Settings\brazee\My Documents\My
> Pictures\work
>
> 11/17/2006 08:04 AM <DIR> .
> 11/17/2006 08:04 AM <DIR> ..
> 11/17/2006 07:21 AM 141,377 Hawai'i Volcanoes National
> Park, Hawai'i,
> 1983.jpg
> 1 File(s) 141,377 bytes



 
Reply With Quote
 
Howard Brazee
Guest
Posts: n/a
 
      17th Nov 2006
On Sat, 18 Nov 2006 07:55:12 +1100, "Pegasus \(MVP\)" <(E-Mail Removed)>
wrote:

>dir "%USERPROFILE%\My Documents\My Pictures\home" | find /i " 0 File(s)" >
>nul || dir
>"%USERPROFILE%\My Documents\My Pictures\home\"


That works.
 
Reply With Quote
 
Howard Brazee
Guest
Posts: n/a
 
      17th Nov 2006
On Sat, 18 Nov 2006 07:55:12 +1100, "Pegasus \(MVP\)" <(E-Mail Removed)>
wrote:

>dir "%USERPROFILE%\My Documents\My Pictures\home" | find /i " 0 File(s)" >
>nul || dir
>"%USERPROFILE%\My Documents\My Pictures\home\"


I understand most of this - Could you explain how this works -
including how the | and || work?

Thanks.
 
Reply With Quote
 
Pegasus \(MVP\)
Guest
Posts: n/a
 
      17th Nov 2006

"Howard Brazee" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> On Sat, 18 Nov 2006 07:55:12 +1100, "Pegasus \(MVP\)" <(E-Mail Removed)>
> wrote:
>
> >dir "%USERPROFILE%\My Documents\My Pictures\home" | find /i " 0 File(s)"

>
> >nul || dir
> >"%USERPROFILE%\My Documents\My Pictures\home\"

>
> I understand most of this - Could you explain how this works -
> including how the | and || work?
>
> Thanks.


Good questions!

| is the pipe operator. It sends the output from the first command
as input to the next command. Hence in the line

dir c:\ | find /i "bytes free"

the "dir" command will send its output to find.exe which in turn will
output only those lines that contain the string "bytes free". Lines that
do not contain this string are discarded.

The && and || operators check the ErrorLevel of the preceding
command, allowing conditional execution of the succeeding command.
Here are two examples:

echo %UserName% | find /i "Howard" > nul && echo You are Howard.
echo %UserName% | find /i "Howard" > nul || echo You are not Howard.

I could have achieved the same effect like so:

echo %UserName% | find /i "Howard" > nul
if %ErrorLevel% EQU 0 echo You are Howard.
if %ErrorLevel% GTR 0 echo You are not Howard.

or like so:

echo %UserName% | find /i "Howard" > nul
if %ErrorLevel% EQU 0 (echo You are Howard.) else (echo You are not
Howard.)


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Check to see icon exist in command bar Peter T Microsoft Excel Programming 0 9th Jan 2007 09:52 PM
Command Line. How to tell to XL : If the xls file exist : Open it, if it does not exist : Create it. Tintin92 Microsoft Excel Programming 3 11th Mar 2006 06:45 PM
Command Ligne. How to tell to XL : If the xls file exist : Open it, if it does not exist : Create it. Tintin92 Microsoft Excel Discussion 0 10th Mar 2006 09:49 AM
Newsgroup button and command don't exist Monte Grant Microsoft Outlook Discussion 2 30th Dec 2004 09:02 PM
does this vb.6 command exist in vb.net smhaig Microsoft Dot NET 3 8th Aug 2004 09:08 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 05:00 PM.