Reputation: 45
I have a batch checking for a specific file like this:
if exist "C:\Program Files\App\App version number\file.exe" (
echo EXISTS
) else (
echo NOT EXIST!
)
I want to be able to check for the file.exe
in any version so it might be in:
"C:\Program Files\App\App v.1\file.exe"
"C:\Program Files\App\App v.2\file.exe"
How can I skip the version number as a requirement inside the path?
Upvotes: 1
Views: 2674
Reputation: 11621
setlocal
set AppDir=
for /d %%d in ("C:\Program Files\App\App v*") do if exist "%%d\file.exe" set "AppDir=%%d"
if defined AppDir (
echo EXISTS - it's in "%AppDir%"
) else (
echo NOT EXIST!
)
endlocal
(Update: Edited to put quotes around variables in case there's a filename that contains a special character such as &
. Thanks to @aschipfl for pointing this out. On the other hand, there's no need to use %%~d
instead of just %%d
because %%d
won't have any quotes anyway.)
Upvotes: 0
Reputation: 5874
Might be a bit overkill but should work.
main.bat:
@echo off
for /f "tokens=*" %%i in ('sub.bat') do (
if "x%%i"=="x" (
echo "Not found!"
) ELSE (
echo "Found in %%i"
)
)
sub.bat:
@echo off
set your_path="C:\Your Path\"
cd /d %your_path%
for /r %%p in (file.exe) do echo %%p
Explanation:
The main batch is the one to execute. It reads the output of the sub batch and reacts properly.
Set the root for the search to the variable your_path
. It will change the directory and then searches with an /R
ecursive loop through the current directory and looks for the file you set in the parenthesis. If the file is found, it will echo
that to get read by the main file.
Feel free to ask further questions :)
Upvotes: 0
Reputation: 14290
Here is an example using the WHERE
command with conditional operators.
where /Q /R "C:\Program Files\App" file.exe && echo found || echo not found
Upvotes: 4