Find program file path programmatically and quickly

I want to find the path to the .exe files programmatically in Java, for example:

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

The method I tried sorts by system files until "skype.exe" is found, but it takes a lot of time and resources.

Is there any hack that could make it almost instantaneous, for example, maybe the Win_Api / cmd function or sorting by file system until the program is found in a single way?

+6
source share
3 answers

I really found something that works, although not the fastest, but it only takes about 100 ms - 500 ms to do depending on the exe.

For this you need to use the execution process.

Basically, you go to the root of your disk, and then look for the file system using these commands in cmd.

cd \ dir /s /b mytool.exe 

This will return the file path.

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 pass the parameter "eclipse.exe" to the bat file, which is represented as% 1):

 cd \ dir /s /b %1 exit 

The output will be:

 C:\Users\Mitchell\workspace\Remote Admin>cd \ C:\>dir /s /b eclipse.exe C:\eclipse\eclipse.exe 
+1
source

As an extension to @Minor’s answer , if you want to improve performance by limiting the search to only those programs that are currently “installed” on Windows, the following registry keys contain information about the “installed” programs.

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

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

Then you simply modify your Java code to use another batch script that extracts these installation locations, and specifically target these installation 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 the InstallLocation returned by getInstalledPrograms.bat, although the script works fine in cmd. Conceptually, however, this decision is reasonable.

+1
source

If you want to know that the WHICH executable is executed from the command line as you type. Use commands where / where ..

Unix / linux / Mac OSX Possix < which

which chmod which cd which which myprogram

returns nothing returns if such a program file does not exist.

for windows where < where > /? for reference on this command

If you do not have an executable file other than an alias or any environment / procedure / shell command, use the < type > command on Unix / linux / Mac OS X. This will tell you if you are dealing with an alias, environment function, or executable .

0
source

All Articles