Technical: Microsoft – OS – Command Shell – Which.exe

Technical: Microsoft – OS – Command Shell – Which.exe

Introduction

Back in terminal mode, running some DOS Command Shell scripts and seeing an error I am not quite familiar with; specifically I am seeing “INFO: Could not find files for the given pattern(s)”

It is bugging me a bit and so I keep shredding the file to hopefully pinpoint the offending line.

The where.exe

Finally tamed \ traced it back to the where.exe command.  It is a Microsoft take on the Unix\Linux which command.


Description:
Displays the location of files that match the search pattern.
By default, the search is done along the current directory and
in the paths specified by the PATH environment variable.

Where.exe – How does it work

If I need to determine how it works, I will issue something like


where sqlcmd.exe

And, I will get back, something like:


c:\Program Files\Microsoft SQL Server\100\Tools\Binn\SQLCMD.EXE
C:\Program Files\Microsoft SQL Server\110\Tools\Binn\SQLCMD.EXE

If I look for a file (sqlcmdNoop.exe) that does not likely exist:


where sqlcmdNoop.exe

And, I will get back, something like:


INFO: Could not find files for the given pattern(s).

My little fix towards silencing the error

I am not really interested in seeing and reviewing the list of file matches.  All I need to know is whether sql server client app (sqlcmd.exe) exists or not.

And, so I can the change the code snippet a bit.



@ECHO OFF
@ECHO Is sqlcmd.exe installed?
@WHERE SQLCMD.EXE /Q
if %errorlevel%==0 (
	echo "file (sqlcmd.exe) found"
) else (
	echo "file (sqlcmd.exe) not found"
)

@WHERE SQLCMDNOOP.EXE /Q
if %errorlevel%==0 (
	echo "file (sqlcmdNOOP.exe) found"
) else (
	echo "file (sqlcmdNOOP.exe) not found"
)

Output:



Is sqlcmd.exe installed?

"file (sqlcmd.exe) found"

"file (sqlcmdNOOP.exe) not found"


Explanation:

  • Passing in the /Q argument to the where.exe utility says do not list out the files you find nor display “INFO: Could not find files for the given pattern(s)” if there are no matches
  • For our specific use case, we partake of the nice ERRORLEVEL exit code

References

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s