1

i have some folders/files in Recent items of windows.what i want to do is i want to know real path of all the files/folders of .lnk shortcuts present in Recent Items.I want result like this. Local path : C:\Program Files (x86)\Windows Live\Mail\wlmail.exe

My code is given

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package sir.aimal;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.LinkOption;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.BasicFileAttributeView;
import java.nio.file.attribute.BasicFileAttributes;
import java.nio.file.attribute.FileTime;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 *
 * @author zeeshan
 */
public class SirAimal 
{


    public static void main(String[] args) throws IOException
    {
        String user=System.getProperty("user.name");
        String path="C:\\Users\\"+user+"\\AppData\\Roaming\\Microsoft\\Windows\\Recent\\";

        File directory = new File(path);
        File[] fList = directory.listFiles();

        for (int i=0;i<fList.length;i++)
        {
            String filename=fList[i].getName();
            String actualfilename=filename.replace(".lnk", "");

            Path p = Paths.get(path+filename);


            BasicFileAttributes view= Files.getFileAttributeView(p, BasicFileAttributeView.class).readAttributes();
            FileTime fileTime=view.creationTime();
            System.out.println(actualfilename+"\t\t"+new SimpleDateFormat("dd/MM/yyyy HH:mm:ss").format((fileTime.toMillis())));


        }

    }


}
Zeeshan Nisar
  • 77
  • 1
  • 8

1 Answers1

0

Try this inside the for loop:

ShellFolder sf = ShellFolder.getShellFolder(flist[i]);
ShellFolder target = sf.getLinkLocation();
if (target != null) {
    System.out.println(target.getAbsolutePath());
}

Here is some documentation on the class:

http://srcrr.com/java/oracle/openjdk/6/reference/sun/awt/shell/ShellFolder.html

Be aware: this method will access the shortcuts through Windows API, which will have the same result as you double clicking on it. So for every single broken link Windows will show you a popup message asking you to either browse to the current location of the file or remove the broken link. This is the 'quick & nasty' solution, which should work for you. If you'd like a more sophisticated code, take a look at this answer:

https://stackoverflow.com/a/9403438/3830952

Community
  • 1
  • 1
Gábor Major
  • 444
  • 2
  • 8
  • 23