Command question (IF EXIST)

  • Thread starter Thread starter Howard Brazee
  • Start date Start date
H

Howard Brazee

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
 
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.
 
Try this:

dir "%USERPROFILE%\My Documents\My Pictures\home" | find /i " 0 File(s)" >
nul || dir
"%USERPROFILE%\My Documents\My Pictures\home\"
 
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.
 
Howard Brazee said:
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.)
 
Back
Top