'How do I check if a file with a varying path doesn't exist in Batch?

I'm attempting to detect if Python is installed on a Windows system at any given time, while future proofing for updates past 3.10 -- though I assume, of course, that this is reproducible with any file or folder that also has a varying path.

The details

Python doesn't get "installed" the same way others do, i.e. get placed into Program Files (or PF x86) -- it instead gets placed into a user's AppData\Local\Programs\ folder by default. Since my current project relies on the user having Python installed, I want to make sure when the installer shows up that it completed.

The problem arises with that future proofing I mentioned -- Python 3 folders get named after their version, e.g. the folder containing Python 3.10.2 is in AppData\Local\Programs\Python\Python310\, 3.9.9 is in \Python\Python39\, et cetera. I need to check whether python.exe exists or not, but since its directory varies based on version, I also need to check for each folder. However, it occurred to me that there are actually two instances of python.exe past the initial \Python\ folder, one located at the version directory and another deeper in \Lib\venv\scripts\nt\.

The attempts

Currently, I'm attempting to use the method mentioned on this Microsoft Exchange forum post, which explains the following FOR loop could perform a command in each applicable directory;

for /r "<folder one>" %a in ("<folder three or file name>") do @if exist %a <command>

...which I attempted to apply as a simple check (in a standard cmd process, not in a Batch script) with the following;

for /r %userprofile%\AppData\Local\Programs\Python %A in (python.exe) do @if exist %A echo found %~nxA in %~dpA

Because there are multiple matches, though, it returns both paths instead of just the first instance;

found python.exe in C:\Users\<me>\AppData\Local\Programs\Python\Python310\
found python.exe in C:\Users\<me>\AppData\Local\Programs\Python\Python310\Lib\venv\scripts\nt\ 

The problem

This means I can't simply save %A as a variable and check for it, because by FOR's logic it would save the instance at \Lib\venv\scripts\nt\ and not \Python310\, which is the one I'm looking for specifically. I also can't use if not exist instead, because if I attempt to do so, FOR will act whatever command I use for each of the folders not containing python.exe, which would ignore the fact it does exist elsewhere in the tree.

I attempted to save them both as variables by using /f "tokens=2", but after trying to place it both before and after my /r, I got errors explaining that either "tokens=2" was unexpected at this time" or "%userprofile%\AppData\Local\Programs\Python" was unexpected at this time" -- I'm assuming this is due to the fact I can't use multiple options at once, but it isn't mentioned in FOR's documentation, so I'm not entirely certain.

I also can't simply run where python.exe, as it isn't guaranteed Python will be added to PATH since it isn't checked by default when installing.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source