Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find a executable file path programmatically and fast

Tags:

java

file

path

I want to find a .exe files path programatically in Java, for example:

  • Skype.exe is entered into a program to find the direct path to it
  • Program does an algorithm that finds the file path
  • Program returns the file path C:\Users\Public\Desktop\Skype.exe

A method I have tried is sorting through the systems files until "skype.exe" is found, but that takes up a lot of time and resources.

Is there any hack that could make it almost instant, like maybe a Win_Api function/cmd command or is sorting through the file system until the program is found the only way?

like image 699
Mitch Zinck Avatar asked Dec 16 '13 23:12

Mitch Zinck


2 Answers

I actually found something that works, albeit not the fastest but only takes around 100ms - 500ms to do depending on the exe.

You need to use a runtime process to do this.

Basically you go into the root of your drive, and then search the filesystem using these commands in the cmd.

cd \
dir /s /b mytool.exe

This will return the filepath.

My code for it:

(Hackish I know)

try {
        Process p = Runtime.getRuntime().exec("./res/getPrograms.bat " + exeName);
        BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
        String line;
        while(true) {
            line = input.readLine();
            if(line == null) {
                break;
            }
            System.out.println(line);
        }

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

And the .bat file (I am passing the parameter "eclipse.exe" to the bat file which is represented as %1):

cd \
dir /s /b %1
exit

The output becomes:

C:\Users\Mitchell\workspace\Remote Admin>cd \ 

C:\>dir /s /b eclipse.exe 
C:\eclipse\eclipse.exe
like image 147
Mitch Zinck Avatar answered Oct 05 '22 07:10

Mitch Zinck


As an extension to @Minor's answer, if you wanted to increase performance by limiting search to only programs that are currently "installed" on Windows, the following registry keys contain information on "installed" programs.

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall
HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall

Using powershell, you can access properties of installed software stored in these keys. Of particular interest is the InstallLocation property.

Then, you just modify your Java code to utilise another batch script that retrieves these install locations, and specifically target these install locations for exe files.

getInstalledPrograms.bat

@echo off
powershell -Command "Get-ChildItem -Path HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall, HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall | Get-ItemProperty | Where-Object {$_.DisplayName -match \"%1\"} | Select-Object -Property InstallLocation"
exit

getPrograms.bat

@echo off
cd %1
dir /b /s "*%2*.exe"
exit

Java Example:

String search = "skype";
try {
    Process getInstalled = Runtime.getRuntime().exec("./src/getInstalledPrograms.bat " + search);
    BufferedReader installed = new BufferedReader(new InputStreamReader(getInstalled.getInputStream()));
    String install;
    String exe;
    int count = 0;
    while(true) {
        install = installed.readLine();
        if(install == null) {
            break;
        }
        install = install.trim();
        // Ignore powershell table header and newlines.
        if(count < 3 || install.equals("")) {
            count++;
            continue;
        }
        Process getExes = Runtime.getRuntime().exec("./src/getPrograms.bat " + "\"" + install + "\"");
        BufferedReader exes = new BufferedReader(new InputStreamReader(getExes.getInputStream()));
        while(true) {
            exe = exes.readLine();
            if(exe == null) {
                break;
            }
            exe = exe.trim();
            System.out.println(exe);
        }
    }

} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

Currently my Java example duplicates InstallLocation returned by getInstalledPrograms.bat, although the script works fine in cmd. Conceptually though, this solution is sound.

like image 38
Taylor Hx Avatar answered Oct 05 '22 07:10

Taylor Hx