Finding an exe from any hard drive and setting it as a variable

Solution 1:

To improve your code:

Use dir /s /b to get just the bare file-name.

To get the result of the dir command into the variable, use an inner loop like that:

FOR /F "USEBACKQ" %%F IN (`dir /s /b ffprobe.exe`) DO (
SET var=%%F
)
ECHO %var%

Solution 2:

Any of the resulting files could be set as the variable (like the last one), but the latest (by date or version) would be optimal.

  • To scan all drivers:
@echo off

setlocal enabledelayedexpansion && set "_f=" && set "_ff=" && set /a "_cnt=0" 
for /f usebackq^tokens^=* %%f in (`%__AppDir__%mountvol.exe^|find.exe /i ":\"
    `)do 2>nul pushd %%~f && (set/a "_cnt+=1" && call %:^) _ff_!_cnt! & popd)
    
for /f useback^tokens^=1*delims^=^=^  %%i in (`^<nul ^<con: set _ff_^|%__AppDir__%sort.exe /r
    `)do <con: <nul call set "_ffprobe=%%~dpnxj" & call set "_ffprob_Path=%%~dpj" & goto %:^|
 
%:^)
for /f usebackq^tokens^=2^,4* %%i in =;(`2^>nul %__AppDir__%where.exe /r \ ffprobe.exe /t ^|^
    %__AppDir__%sort.exe /r^|^|exit/b`)do set "_f=%%~k" && for /f useback^delims^=. %%G in (`
        ^<con %__AppDir__%wbem\wmic.exe DataFile where name^="!_f:\=\\!" get LastModified ^|^
            %__AppDir__%findstr.exe /rb [0-9]`)do <con: <nul call set "%~1=%%~k" && exit /b 0

%:^|
<con: rem.// your code continues at this point using: & echo+!_ffprobe! & echo+!_ffprob_Path!


  • To limit to only in Local Disk (DriveType=3 == HD's):
@echo off

=====setlocal enabledelayedexpansion && set/a "_cnt=0" & call set _ff=<nul & call set _f=<nul
for /f usebackq^delims^= %%i In (`%__AppDir__%wbem\wmic.exe LogicalDisk where DriveType^='3'^
    get caption^|find ":"`)do pushd %%~i && (set/a "_cnt+=1" && call %:^) _ff_!_cnt! & popd )

for /f useback^tokens^=1*delims^=^=^  %%i in (`^<nul ^<con: set _ff_^|%__AppDir__%sort.exe /r
    `)do <con: <nul call set "_ffprobe=%%~dpnxj" & call set "_ffprob_Path=%%~dpj" & goto %:^|
 
%:^)
for /f usebackq^tokens^=2^,4* %%i in =;(`2^>nul %__AppDir__%where.exe /r \ ffprobe.exe /t ^|^
    %__AppDir__%sort.exe /r^|^|exit/b`)do set "_f=%%~k" && for /f useback^delims^=. %%G in (`
        ^<con %__AppDir__%wbem\wmic.exe DataFile where name^="!_f:\=\\!" get LastModified ^|^
            %__AppDir__%findstr.exe /rb [0-9]`)do <con: <nul call set "%~1=%%~k" && exit /b 0

%:^|
<con: rem.// your code continues at this point using: & echo+!_ffprobe! & echo+!_ffprob_Path!

1. Get all available drivers

2. Scan each validated drive

3. Use size-ordered output

4. Get the LastModified date

5. Save Last Modified date and Path one by one

6. Use date inversion and get latest/highest number


Obs.: Scan each hd, as well as each pen drive, memory card, cd (if any media is present on the drive), keep in mind that it may take some time...


Some further reading:

[√] Set /?

[√] Wmic /?

[√] For /?

[√] For /F

[√] MountVol /?

[√] Redirection |, <, > 2>, ...

[√] Goto :Label /? | Call :Label /?

[√] DelayedExpansion (Refer: !Expand_RunTime_Variable!)

[√] Show all drives from Windows command prompt

[√] Set variable=variable:substrings | DOS - String Manipulation

How does the Windows Command Interpreter cmd.exe Parse Scripts