Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / August 2007

Tip: Looking for answers? Try searching our database.

Short-cut to a file

Thread view: 
MVB - 06 Aug 2007 06:47 GMT
Hi All,
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?

Thanks,
MVB
Jeff Higgins - 06 Aug 2007 17:23 GMT
> Hi All,
> 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?

<http://msdn2.microsoft.com/EN-US/library/aa969393.aspx>
Oliver Wong - 06 Aug 2007 23:02 GMT
> Hi All,
> 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?

   Different OSes implement shortcuts in different ways. In the case of
Windows XP, a shortcut file is a plain old regular file which you can open
up and read, but they are binary (as opposed to plain text), so you'll
have to figure out the file format to decypher.

   - Oliver
Steve Sobol - 07 Aug 2007 02:07 GMT
>     Different OSes implement shortcuts in different ways. In the case of
> Windows XP, a shortcut file is a plain old regular file which you can open
> up and read, but they are binary (as opposed to plain text), so you'll
> have to figure out the file format to decypher.

With Windows you're probably best off using JNI to call the Win32 API
function that creates shortcuts. With Linux/*BSD and Gnome (and probably also
KDE) the shortcut file is simply a text file.

I'd like to create a Java class that does what we're talking about, but I don't
have a lot of time in which to do it...
Real Gagnon - 07 Aug 2007 03:28 GMT
> 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.
Signature

Real Gagnon  from  Quebec, Canada
* Java, Javascript, VBScript and PowerBuilder code snippets
* http://www.rgagnon.com/howto.html
* http://www.rgagnon.com/bigindex.html



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.