Windows cmd/batch dir command set path as variable

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 KonAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Danny KonAuthor Commented:

After all these years still here :)

This make me happy 100% perfect

Many thanks

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows Batch

From novice to tech pro — start learning today.