Solved

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

Posted on 2014-04-10
6
373 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

 
LVL 54

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 84

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

Suggested Solutions

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…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

739 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