Cham
Cham

Reputation: 11

.bat program to search and find a particular .exe file and then run that file once

I want to create a .bat file that will help me to find/search a particular .exe file in folders in a particular location and then run that .exe file.
e.g

  1. search file in particular location e.g."\G:\data" and in this prime location there is many folders and in that folders many sub folders are there. and the .exe file is available in one of these sub-folder and the location I do not know. only I know the name of the .exe file.
  2. bat file need to search that file and then run that one.

below two are not able to find and run the file.
1st program

pushd G:\data-a\test
FOR /F "delims=" %F IN ('dir /S /b autorun.exe') DO SET ExePath="%F"
%ExePath%e

2nd program

for /r G:\data-a\test %a in (autorun.exe) do "%a"

from the above two different program I put it in to notepad for creating two different .bat file and executed one after anther file. but that was unable to locate and run autorun.exe file.

just for example my autorun.exe file exact locations are

G:\data-a\test\test1\t1 and G:\data-a\test\test1\t1

but in G:\data-a\test location there may be several subfolders.

so to run autorun.exe one by one that to find the file in subfolder is difficult. I want to give the path G:\data-a\test in program and all the files will be found in subfolder and then run automatically.

Upvotes: 0

Views: 6971

Answers (2)

Doctor DOS
Doctor DOS

Reputation: 263

or the one liner from the command prompt:

for /r g:\data %a in (filename.exe) do "%a"

used the quotes for %a in case the calling directory path has spaces in it

Upvotes: 1

MonkeyPushButton
MonkeyPushButton

Reputation: 1085

Three liner (use %%F if wrapping inside a batch file).

pushd G:\data
FOR /F "delims=" %F IN ('dir /S /b something.exe') DO SET ExePath="%F"
%ExePath%

Upvotes: 2

Related Questions