Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2014-04-10
6
Medium Priority
?
375 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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

 
LVL 57

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 85

Accepted Solution

by:
oBdA earned 2000 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

If like me you are one who spends a lot of time working and scripting with cmd.exe, sometimes it is handy to be able to quickly view a calendar for a given month and year. This script will quickly do just that!  Save the code posted below to a .bat …
This is a fine trick which I've found useful many times, when you just don't want to accidentally run a batch script or the commands needs administrator rights.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

704 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