Solved

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

Posted on 2014-04-10
6
366 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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 51

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
I have published numerous articles here at Experts Exchange that present programs/scripts written in a language called AutoHotkey. Each of those articles has a brief paragraph describing where to download the product and how to install it. I have al…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

743 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now