Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Windows file details?

Tags:

java

file

windows

I want to get the "File description" and the "Copyright" of exe/dll/sys files, as showed in the "Details" tab when you right click the file and choose the properties.

like image 616
excellent5 Avatar asked May 24 '14 13:05

excellent5


People also ask

How do you see details about files in a folder?

You can also Alt-click on a file or folder to access its properties. The General tab of the Properties dialog box will provide you with information such as the full path to the file or folder, its size, what application is configured to open it, and the date it was created, last modified, and accessed.

How we can view the properties of the file?

Click the File tab. Click Info. Click the View and edit database properties link at the top of the page. In the Properties dialog box, click the tabs to select the properties that you want to view or update.

How do I get a list of files in CMD?

Type dir /A:D. /B > FolderList. txt and press Enter to generate a top-level folder list. When the list is complete, a new, blank prompt with a flashing cursor will appear.


1 Answers

With Windows API you can call VerQueryValue to get that information. JNA has a class for accessing this API called Version.

This other question has some code samples that can get you started:

Get Version Info for .exe

And this one has a C code sample of reading product name that you can translate into JNA:

How do I read from a version resource in Visual C++

This obviously only works on Windows. If you want something portable, you might be able to use pecoff4j to parse the executable on your own. It claims to be able to parse the version information in the resource section of the PE (Portable Executable).


It seems pecoff4j doesn't support parsing version strings, so I forked it on GitHub to add support for it. This code should now work:

import java.io.IOException;

import org.boris.pecoff4j.PE;
import org.boris.pecoff4j.ResourceDirectory;
import org.boris.pecoff4j.ResourceEntry;
import org.boris.pecoff4j.constant.ResourceType;
import org.boris.pecoff4j.io.PEParser;
import org.boris.pecoff4j.io.ResourceParser;
import org.boris.pecoff4j.resources.StringFileInfo;
import org.boris.pecoff4j.resources.StringTable;
import org.boris.pecoff4j.resources.VersionInfo;
import org.boris.pecoff4j.util.ResourceHelper;

public class Main {

    public static void main(String[] args) throws IOException {
        PE pe = PEParser.parse("C:/windows/system32/notepad.exe");
        ResourceDirectory rd = pe.getImageData().getResourceTable();

        ResourceEntry[] entries = ResourceHelper.findResources(rd, ResourceType.VERSION_INFO);
        for (int i = 0; i < entries.length; i++) {
            byte[] data = entries[i].getData();
            VersionInfo version = ResourceParser.readVersionInfo(data);

            StringFileInfo strings = version.getStringFileInfo();
            StringTable table = strings.getTable(0);
            for (int j = 0; j < table.getCount(); j++) {
                String key = table.getString(j).getKey();
                String value = table.getString(j).getValue();
                System.out.println(key + " = " + value);
            }
        }
    }

}

It will print all the information you need:

CompanyName = Microsoft Corporation
FileDescription = Notepad
FileVersion = 6.1.7600.16385 (win7_rtm.090713-1255)
InternalName = Notepad
LegalCopyright = © Microsoft Corporation. All rights reserved.
OriginalFilename = NOTEPAD.EXE
ProductName = Microsoft® Windows® Operating System
ProductVersion = 6.1.7600.16385
like image 199
kichik Avatar answered Oct 08 '22 06:10

kichik