Solved

Search for a program in multiple locations, in a Windows script (.bat file)

Posted on 2014-04-10
6
371 Views
Last Modified: 2014-04-10
I need to find the location of a program, which could be in one of several folders - either "..\bin" and if not there then look in c:\Program Files\yyy\bin" and maybe some other optional locations. Is there a way to do this in a Windows script (a .bat file)?
0
Comment
Question by:Miritm
6 Comments
 
LVL 25

Expert Comment

by:Tony Giangreco
ID: 39991506
Go to Start, accessories, right click Command prompt and select Run as Administrator

Type in Dir c:\(application file name.xxx) /s

You can run this for all drives. You need to substitute the file name so it knows what to search for.
0
 

Author Comment

by:Miritm
ID: 39991530
Thanks for the answer, however this performs an exhaustive search, and I would like to search only in 2-3 fixed locations. Also, I need to get the result into some variable so that I can use it later in the script file to invoke the program.
0
 
LVL 25

Expert Comment

by:Tony Giangreco
ID: 39991536
If you expand the path, it will limit the search.

dir c:\ProgramData\appname.xxx /s

dir c:\users\joe\.... /s
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 53

Expert Comment

by:Bill Prew
ID: 39991559
Not sure how fancy you want to get, but a simple BAT like this would display anyplace it finds the program.

@echo off

dir /s /b /a-d "C;\dir1\yourfile.exe"
dir /s /b /a-d "C;\dir2\yourfile.exe"
dir /s /b /a-d "C;\dir3\yourfile.exe"
dir /s /b /a-d "C;\dir4\yourfile.exe"

Open in new window

~bp
0
 

Author Comment

by:Miritm
ID: 39991574
thanks. This will print out the result, right? how do I put this into a variable?
0
 
LVL 83

Accepted Solution

by:
oBdA earned 500 total points
ID: 39991587
Try this; just add your search locations to the SearchList variable, and set your program name in SearchFile. After the :Found label, you'll have the full file path in the "File" variable. The file name will already be enclosed in double quotes, so you can just run it by using %File%
@echo off
setlocal
set SearchList="..\bin" "%ProgramFiles%\yyy\bin" "ProgramFiles(x86)\yyy\bin"
set SearchFile=acme.exe
for %%a in (%SearchList%) do (
	if exist "%%~a\%SearchFile%" (
		for %%b in ("%%~a\%SearchFile%") do set File="%%~fb"
		goto Found
	)
)
echo File '%SearchFile%' not found in anywhere in %SearchList%!
exit /b 1
:Found
echo Found file: %File%

Open in new window

0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When you receive another warning that your shared drive is almost full and you have asked your users to clean out old files again and again, here is a single command that may help. This command will place all the files that have not been used rec…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question