Windows cmd/batch dir command set path as variable

Danny Kon
Danny Kon used Ask the Experts™
on
This has to work at windows 7 or higher,
I want a cmd/batch wat will do:

set /p ffile=Give the filename
as example calc.exe
find the first directory where i can execute the calc.exe and set this directory to a variable
That i can use "c:\windows\system32\" in a other batch  

Thanks for your help

Danny
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2018
Distinguished Expert 2018
Commented:
This will first try to find the file in the Path, and if not found there, start searching C:. You can pass it the file as command line argument, without an argument it will ask for the file name.
You can add your own code before line 16.
@echo off
setlocal enabledelayedexpansion
if "%~1"=="" (
	set /p FileName=Give the filename:
	if "!FileName!"=="" goto :eof
) else (
	set FileName=%~1
)
call :Where Folder "%FileName%"
if "%Folder%"=="" (
	echo %FileName% not found.
	exit /b 1
)
echo Folder: %Folder%

REM ========== Only function after this line! ==========
goto :eof
:Where <Variable> <File>
set %~1=%~dp$PATH:2
if not "!%~1!"=="" goto :eof
echo '%~2' not found in path; searching C: ...
for /f "delims=" %%a in ('dir /a:-d /s /b "C:\%~2"') do (set %~1=%%~dpa&goto :eof)
set %~1=
goto :eof

Open in new window

Author

Commented:
oBdA

After all these years still here :)

This make me happy 100% perfect

Many thanks

Danny

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial