Re: Short-cut to a file
I want to get the actual path of target file from a short-cut to that
file, using Java. Can anyone help me to get this?
The easiest way on Windows is to call a VBS and capture the output.
In this`example, I open the Firefox shortcut on the desktop and read the
target Path.
import java.io.BufferedReader;
import java.io.File;
import java.io.FileWriter;
import java.io.InputStreamReader;
public class VBSUtils {
private VBSUtils() { }
public static String readShortcut() {
String result = "";
try {
File file = File.createTempFile("realhowto",".vbs");
file.deleteOnExit();
FileWriter fw = new java.io.FileWriter(file);
String vbs = "set wshshell = WScript.CreateObject(\"WScript.Shell\") \n"
+ "desktop = wshshell.SpecialFolders(\"AllUsersDesktop\") \n"
+ "set shortcut = wshshell.CreateShortcut"
+ "(desktop & \"\\Mozilla Firefox.lnk\") \n"
+ "WScript.Echo shortcut.TargetPath \n"
+ "Set wshshell = Nothing \n";
fw.write(vbs);
fw.close();
Process p =
Runtime.getRuntime().exec("cscript //NoLogo " + file.getPath());
BufferedReader input =
new BufferedReader
(new InputStreamReader(p.getInputStream()));
String line;
while ((line = input.readLine()) != null) {
result += line;
}
input.close();
}
catch(Exception e){
e.printStackTrace();
}
return result.trim();
}
public static void main(String[] args){
msgBox("Shortcut for Firefox is " + readShortcut());
}
public static void msgBox(String msg) {
javax.swing.JOptionPane.showConfirmDialog((java.awt.Component)
null, msg, "VBSUtils", javax.swing.JOptionPane.DEFAULT_OPTION);
}
}
Bye.
--
Real Gagnon from Quebec, Canada
* Java, Javascript, VBScript and PowerBuilder code snippets
* http://www.rgagnon.com/howto.html
* http://www.rgagnon.com/bigindex.html