Solved

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

Posted on 2014-04-10
6
372 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
The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

 
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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

820 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