How do I look for a file and execute it via DOS?

29 views Asked by At

The title is pretty much self explanatory and my example makes it pretty obvious but I'm trying to find for a particular exe file and execute it.

cd c:\

for /f %%f in ('dir /b /s eclipse.exe') do (
    start %%f -clean
)

echo Done!  
pause >nul

For safety reason please let me know if it's possible to run only the first file found or look for a particular hash string or file size.

Thanks !

1

There are 1 answers

0
JosefZ On

To run only the first file found ("tokens=*" to eliminate spaces in path(s); note also required empty string "" in start "" "%%f" -clean)

for /f "tokens=*" %%f in ('dir /b /s eclipse.exe') do (
    start "" "%%f" -clean
    goto :enough
)
:enough
echo Done

To look for file size

for /f "tokens=*" %%f in ('dir /b /s eclipse.exe') do (
   for /F "tokens=3" %%H in ('dir "%%f"^|find /I "%%~nxf"') do (
      @echo %%f size=%%H
   )
)

To look for a particular hash string: in cmd line need an external tool like File Checksum Integrity Verifier (FCIV) or better HashMyFiles etc.

In Windows PowerShell: Get-FileHash computes the hash value for a file by using a specified hash algorithm.