Q:

batch file for finding a particular file in a sorted directory

I need to sort all the sub-folders in a directory and print the first folder that contains a file, i tried using
    FOR /F "delims=" %%i IN ('dir "directorypath" /b /ad-h /t:c /o-d') DO SET a=%%i
IF EXIST a\*.exe
echo folder %a%

in line 1 i tried sorting the folders in the directory based on date modified
and then searched for .exe in each folder and print the latest folder which contains an exe.
Thanks for help in advance.

A:

Based purely on your question without response to my comment, I'm going to assume you wish to do something like this:
@Echo Off
Set "dP=directorypath"
Set "dN="
For /F "Delims=" %%A In ('Dir/B/AD-S-L-H/OD/TC "%dP%" 2^>Nul'
) Do If Exist "%dP%\%%A\*.exe" Set "dN=%%A"
If defined dN Echo %dN%
Pause

Just modify directorypath on line two to suit your full or relative source directory.

