Shoutbox

Get File Properties - Printable Version

-Shoutbox (https://shoutbox.menthix.net)
+-- Forum: MsgHelp Archive (/forumdisplay.php?fid=58)
+--- Forum: Messenger Plus! for Live Messenger (/forumdisplay.php?fid=4)
+---- Forum: Scripting (/forumdisplay.php?fid=39)
+----- Thread: Get File Properties (/showthread.php?tid=89253)

Get File Properties by wincy on 02-18-2009 at 02:38 PM

Hello!
I'm trying to get properties of a file on computer.
(In particular Author and Description), so I wrote this:

var oWMI = GetObject("winmgmts:\\\\.\\root\\CIMV2");
var colFiles = oWMI.ExecQuery("Select * from Win32_FileSpecification WHERE Name='C:\\Windows\\notepad.exe'");

for(var colFile = new Enumerator(colFiles); !colFile.atEnd(); colFile.moveNext()) {

Debug.Trace(colFile.FileSize+" "+colFile.Description);

}


But it doesn't works. Maybe i'm using the wrong function?
I tried also:

     var objFil = CreateObject("DSOFile.OleDocumentProperties");
    objFil.Open("C:\antivir.exe");
    Debug.Trace(objFil.SummaryProperties.Author);


with no results.
Can you help me doing this?
Thanks in advance!


RE: Get File Properties by matty on 02-18-2009 at 03:27 PM

I think you need to use SHGetFileInfoW.


RE: Get File Properties by Spunky on 02-18-2009 at 03:44 PM

Also, CreateObject and GetObject are not valid JScript functions IIRC. I think they're WScript

They usually have to be replaced with "new ActiveXObject"...


RE: Get File Properties by wincy on 02-18-2009 at 03:54 PM

quote:
Originally posted by matty
I think you need to use SHGetFileInfoW.

Thanks! Unfortunatly i'm not sure about i should use that funcion... maybe with Interop.Call("kernel32"... ecc?? Help

I made a function which lists current active processes using
GetObject and works correctly even without calling "new ActiveXObject".
RE: Get File Properties by matty on 02-18-2009 at 04:16 PM

quote:
Originally posted by Spunky
Also, CreateObject and GetObject are not valid JScript functions IIRC. I think they're WScript

They usually have to be replaced with "new ActiveXObject"...
Using WMI you need to use it with GetObject.
quote:
Originally posted by wincy
Thanks! Unfortunatly i'm not sure about i should use that funcion... maybe with Interop.Call("kernel32"... ecc?? Help
I don't have the time right now to do further research on it. I am at work.
RE: Get File Properties by wincy on 02-18-2009 at 04:30 PM

Made this:

var SHGFI_DISPLAYNAME = "&H200";
var FILE_ATTRIBUTE_NORMAL = "&H80";

var Result = Interop.Call('shell32', 'SHGetFileInfo', 'C:\antivir.exe', FILE_ATTRIBUTE_NORMAL, 0, 0, SHGFI_DISPLAYNAME);

Debug.Trace(Result);


This returns "0" in Debugger. How can i fix it?


RE: Get File Properties by andrewdodd13 on 02-18-2009 at 05:19 PM

You're passing in a null pointer (the 0) to the API call. What you need to do is create a databloc which you can put SHFILEINFO into. Unfortunately, it's been a while since I scripted, so you'll need to read the documentation on this.


RE: Get File Properties by matty on 02-18-2009 at 05:35 PM

Also every \ in JScript has to be escaped so instead of C:\antivir.exe you need to put C:\\antivir.exe


RE: Get File Properties by wincy on 02-18-2009 at 05:57 PM

var FILE_ATTRIBUTE_NORMAL = "&H80";
var FFile = "C:\antivir.exe";
var Result = Interop.Call('shell32', 'SHGetFileInfo', FFile, FILE_ATTRIBUTE_NORMAL, '', FFile, "&H800");
Debug.Trace(Result);


Using only one "\" it outputs "1", but i don't think it is correct anyway. How can i use SHFILEINFO then? Here is its structure:

typedef struct _SHFILEINFO {
    HICON hIcon; // i don't have it
    int iIcon; // i don't have it
    DWORD dwAttributes; // needs GetAttributesOf (below)
    TCHAR szDisplayName[MAX_PATH]; // should i put file path here?
    TCHAR szTypeName[80]; // what to put here?
} SHFILEINFO;


GetAttributesOf here:

HRESULT GetAttributesOf(UINT cidl,
    PCUITEMID_CHILD_ARRAY *apidl,
    SFGAOF *rgfInOut
);


:( what a mess


RE: Get File Properties by matty on 02-18-2009 at 06:13 PM

Ok you need to review a bit about memory allocations.

js code:
var szDisplayName = Interop.Allocate(512);
var szTypeName= Interop.Allocate(162);
var SHFILEINFO = Interop.Allocate(18);

with ( SHFILEINFO ) {
    WriteDWORD(10, szDisplayName.DataPtr);
    WriteDWORD(14, szTypeName.DataPtr);
    Debug.Trace( 'ShGetFileInfoW : ' + ( Interop.Call('shell32', 'ShGetFileInfoW', 'C:\\antivir.exe', 0x80, SHFILEINFO.DataPtr, Size, 0x800 ) === 0 ? 'Passed' : 'Failed' ) );
}

And obviously I have no way of testing the above it is just a guideline... but do research a bit more.
RE: RE: Get File Properties by wincy on 02-18-2009 at 06:21 PM

quote:
Originally posted by matty
Ok you need to review a bit about memory allocations.

js code:
var szDisplayName = Interop.Allocate(512);
var szTypeName= Interop.Allocate(162);
var SHFILEINFO = Interop.Allocate(18);

with ( SHFILEINFO ) {
    WriteDWORD(10, szDisplayName.DataPtr);
    WriteDWORD(14, szTypeName.DataPtr);
}

Debut.Trace( 'ShGetFileInfoW : ' + Interop.Call('shell32', 'ShGetFileInfoW', 'C:\\antivir.exe', 0x80, SHFILEINFO.DataPtr, 0x800 ) === 0 ? 'Passed' : 'Failed' );

And obviously I have no way of testing the above it is just a guideline... but do research a bit more.


(You wrote "Debut.Trace") It is failing for some reason, it is not specified why... (error code: 2147352567 on "Debug.Trace" line) :S
It gives error also using this only:

Interop.Call('shell32', 'ShGetFileInfoW', 'C:\\antivir.exe', 0x80, SHFILEINFO.DataPtr, 0x800);
RE: Get File Properties by matty on 02-18-2009 at 06:47 PM

I fixed it, I was missing the Size parameter.


RE: RE: Get File Properties by wincy on 02-18-2009 at 06:58 PM

quote:
Originally posted by matty
I fixed it, I was missing the Size parameter.
Do I have to put file size in there (either manually or with a function)?
I really thank you for you patience, Matty.
It still gives the same error (in Interrop.Call), even using this:

var szDisplayName = Interop.Allocate(512);
var szTypeName= Interop.Allocate(162);
var SHFILEINFO = Interop.Allocate(18);

with(SHFILEINFO){
    WriteDWORD(10, szDisplayName.DataPtr);
    WriteDWORD(14, szTypeName.DataPtr);
    Interop.Call('shell32', 'ShGetFileInfoW', 'C:\antivir.exe', 0x80, SHFILEINFO.DataPtr, Size, 0x800);
}


Try the attachment (just change file path)
RE: Get File Properties by matty on 02-18-2009 at 07:03 PM

You cannot use a single \ in JScript. I told you that earlier...


RE: Get File Properties by wincy on 02-18-2009 at 07:19 PM

Sorry, it was just a try, but the error wasn't that. Did you tested it?

Edit: Ok, it was ShGetFileInfoW instead of SHGetFileInfoW.
Now code seems to be correct, but for some reason Debug says:
"ShGetFileInfoW : Failed"


RE: Get File Properties by matty on 02-18-2009 at 07:32 PM

No I didn't I will have to try when I get home.


RE: Get File Properties by wincy on 02-18-2009 at 07:42 PM

I'm sure that file path is correct.
If i don't have to change anything else on code, i really don't know why it doesn't works.
Please answer here if you find errors or a solution.

If i will succeed in making all script works I'll put a thanks to you on it!
Thanks in advance for all! :)