[Function] FGP - FileGetProperties

Post your working scripts, libraries and tools
kon
Posts: 1756
Joined: 29 Sep 2013, 17:11

[Function] FGP - FileGetProperties

25 Jun 2014, 19:01

FGP - FileGetProperties
Functions for retrieving extended file properties.

Example Usage

Code: Select all

#NoEnv
SetBatchLines, -1
FileSelectFile, FilePath					; Select a file to use for this example.

PropName := FGP_Name(0)						; Gets a property name based on the property number.
PropNum  := FGP_Num("Size")					; Gets a property number based on the property name.
PropVal1 := FGP_Value(FilePath, PropName)	; Gets a file property value by name.
PropVal2 := FGP_Value(FilePath, PropNum)	; Gets a file property value by number.
PropList := FGP_List(FilePath)				; Gets all of a file's non-blank properties.

MsgBox, % PropName ":`t" PropVal1			; Display the results.
. "`n" PropNum ":`t" PropVal2
. "`n`nList:`n" PropList.CSV
Functions

Code: Select all

/*  FGP_Init()
 *		Gets an object containing all of the property numbers that have corresponding names. 
 *		Used to initialize the other functions.
 *	Returns
 *		An object with the following format:
 *			PropTable.Name["PropName"]	:= PropNum
 *			PropTable.Num[PropNum]		:= "PropName"
 */
FGP_Init() {
	static PropTable
	if (!PropTable) {
		PropTable := {Name: {}, Num: {}}, Gap := 0
		oShell := ComObjCreate("Shell.Application")
		oFolder := oShell.NameSpace(0)
		while (Gap < 11)
			if (PropName := oFolder.GetDetailsOf(0, A_Index - 1)) {
				PropTable.Name[PropName] := A_Index - 1
				PropTable.Num[A_Index - 1] := PropName
				Gap := 0
			}
			else
				Gap++
	}
	return PropTable
}


/*  FGP_List(FilePath)
 *		Gets all of a file's non-blank properties.
 *	Parameters
 *		FilePath	- The full path of a file.
 *	Returns
 *		An object with the following format:
 *			PropList.CSV				:= "PropNum,PropName,PropVal`r`n..."
 *			PropList.Name["PropName"]	:= PropVal
 *			PropList.Num[PropNum]		:= PropVal
 */
FGP_List(FilePath) {
	static PropTable := FGP_Init()
	SplitPath, FilePath, FileName, DirPath
	oShell := ComObjCreate("Shell.Application")
	oFolder := oShell.NameSpace(DirPath)
	oFolderItem := oFolder.ParseName(FileName)
	PropList := {CSV: "", Name: {}, Num: {}}
	for PropNum, PropName in PropTable.Num
		if (PropVal := oFolder.GetDetailsOf(oFolderItem, PropNum)) {
			PropList.Num[PropNum] := PropVal
			PropList.Name[PropName] := PropVal
			PropList.CSV .= PropNum "," PropName "," PropVal "`r`n"
		}
	PropList.CSV := RTrim(PropList.CSV, "`r`n")
	return PropList
}


/*  FGP_Name(PropNum)
 *		Gets a property name based on the property number.
 *	Parameters
 *		PropNum		- The property number.
 *	Returns
 *		If succesful the file property name is returned. Otherwise:
 *		-1			- The property number does not have an associated name.
 */
FGP_Name(PropNum) {
	static PropTable := FGP_Init()
	if (PropTable.Num[PropNum] != "")
		return PropTable.Num[PropNum]
	return -1
}


/*  FGP_Num(PropName)
 *		Gets a property number based on the property name.
 *	Parameters
 *		PropName	- The property name.
 *	Returns
 *		If succesful the file property number is returned. Otherwise:
 *		-1			- The property name does not have an associated number.
 */
FGP_Num(PropName) {
	static PropTable := FGP_Init()
	if (PropTable.Name[PropName] != "")
		return PropTable.Name[PropName]
	return -1
}


/*  FGP_Value(FilePath, Property)
 *		Gets a file property value.
 *	Parameters
 *		FilePath	- The full path of a file.
 *		Property	- Either the name or number of a property.
 *	Returns
 *		If succesful the file property value is returned. Otherwise:
 *		0			- The property is blank.
 *		-1			- The property name or number is not valid.
 */
FGP_Value(FilePath, Property) {
	static PropTable := FGP_Init()
	if ((PropNum := PropTable.Name[Property] != "" ? PropTable.Name[Property]
	: PropTable.Num[Property] ? Property : "") != "") {
		SplitPath, FilePath, FileName, DirPath
		oShell := ComObjCreate("Shell.Application")
		oFolder := oShell.NameSpace(DirPath)
		oFolderItem := oFolder.ParseName(FileName)
		if (PropVal := oFolder.GetDetailsOf(oFolderItem, PropNum))
			return PropVal
		return 0
	}
	return -1
}
File properties numbers will vary depending on your system. This is the reason some functions generate a static look-up table.
Files will not necessarily have values for every property. i.e. The "Authors" property is used by some MSOffice files, but it is blank in the majority of other cases.

References
Change Log
Last edited by kon on 05 Jul 2014, 02:46, edited 3 times in total.
User avatar
joedf
Posts: 7403
Joined: 29 Sep 2013, 17:08
Facebook: J0EDF
Google: +joedf
GitHub: joedf
Location: Canada
Contact:

Re: [Function] FGP - FileGetProperties

25 Jun 2014, 19:19

Providing an MSDN link or a list of properties would be great.
Image Image Image Image Image
Windows 10 x64 Professional, Intel i5-8500, NVIDIA GTX 1060 6GB, 2x8GB G.Skill RipJaws V - DDR4 3280 MHz | [About Me] | [ASPDM - StdLib Distribution]
[Populate the AHK MiniCity!] | [Qonsole - Quake-like console emulator] | [LibCon - Autohotkey Console Library] | [About the AHK Foundation]
kon
Posts: 1756
Joined: 29 Sep 2013, 17:11

Re: [Function] FGP - FileGetProperties

25 Jun 2014, 20:33

I moved the reference URL's from the comments to actual links (much better, thanks joedf). Also, I added a bit of an explanation regarding property numbers varying.
User avatar
joedf
Posts: 7403
Joined: 29 Sep 2013, 17:08
Facebook: J0EDF
Google: +joedf
GitHub: joedf
Location: Canada
Contact:

Re: [Function] FGP - FileGetProperties

25 Jun 2014, 21:10

Great! :D
Image Image Image Image Image
Windows 10 x64 Professional, Intel i5-8500, NVIDIA GTX 1060 6GB, 2x8GB G.Skill RipJaws V - DDR4 3280 MHz | [About Me] | [ASPDM - StdLib Distribution]
[Populate the AHK MiniCity!] | [Qonsole - Quake-like console emulator] | [LibCon - Autohotkey Console Library] | [About the AHK Foundation]
kon
Posts: 1756
Joined: 29 Sep 2013, 17:11

Re: [Function] FGP - FileGetProperties

27 Jun 2014, 14:49

Some updates to the functions:
  • Added the FGP_List function to get all of a file's non-blank properties.
  • Changed FGP_Init's returned array to use a zero-based index.
  • Changed FGP_Num to use a look-up table instead of looping.
elmo
Posts: 92
Joined: 09 Oct 2013, 09:08

Re: [Function] FGP - FileGetProperties

27 Jun 2014, 16:39

Quite elegant. Thank you.
kon
Posts: 1756
Joined: 29 Sep 2013, 17:11

Re: [Function] FGP - FileGetProperties

05 Jul 2014, 02:50

Update:
  • Changed FGP_Init so that the other functions all contain static references to the same PropTable object instead of each function having its own array.
  • Replaced FGP_ByName and FGP_ByNum with the new function FGP_Value. The new function accepts either a property name or number as its second parameter and returns the property value.
  • Changed FGP_Name to return -1 instead of 0 in the event the property number does not have an associated name. The reason for the change is for consistency with FGP_Num. FGP_Num cannot return 0 as an error code because 0 is a valid property number.
jiggunjer
Posts: 13
Joined: 16 Jan 2016, 04:00

Re: [Function] FGP - FileGetProperties

18 Jan 2016, 22:40

can these properties be used to differentiate a compressed (e.g. zip/rar) from a 'normal' file (e.g. txt, jpeg, etc). The compressed field from the default "FileGetAttrib" is unreliable.
User avatar
joedf
Posts: 7403
Joined: 29 Sep 2013, 17:08
Facebook: J0EDF
Google: +joedf
GitHub: joedf
Location: Canada
Contact:

Re: [Function] FGP - FileGetProperties

18 Jan 2016, 23:33

Well, you could have a custom function to check for the "file magic number" and determine zip and rar, etc
Image Image Image Image Image
Windows 10 x64 Professional, Intel i5-8500, NVIDIA GTX 1060 6GB, 2x8GB G.Skill RipJaws V - DDR4 3280 MHz | [About Me] | [ASPDM - StdLib Distribution]
[Populate the AHK MiniCity!] | [Qonsole - Quake-like console emulator] | [LibCon - Autohotkey Console Library] | [About the AHK Foundation]
lexikos
Posts: 6676
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

Re: [Function] FGP - FileGetProperties

19 Jan 2016, 00:27

The compressed field from the default "FileGetAttrib" is unreliable.
The "Compressed" attribute is about file system compression, not about what type of data the file contains. It's the "Compress contents to save disk space" option in the file's properties, under "Advanced". Usually names of compressed files are displayed with blue text in Explorer.

Windows does not "know" about all compressed file types natively, and it's doubtful that all of the different archiving programs mark the file in some way that's common between file types. So if you want to identify whether a file is a compressed archive or not, you need to know about each file type (filename extension or some signature within the data). Or you could utilize an external program or library, like the 7-zip DLL.
User avatar
JoeWinograd
Posts: 1369
Joined: 10 Feb 2014, 20:00

Re: [Function] FGP - FileGetProperties

01 Feb 2018, 15:58

Hi kon,
First, I want to thank you very much for FGP. It has solved a nasty problem for me, namely, to create a count of all the pages in all the PDF files in a folder (and, optionally, its subfolders). I had been using a utility that requires opening a PDF to determine its page count and, of course, it was failing on all of the password protected PDFs. Your fantastic FGP does not suffer from that!

But I am having one problem that I hope you can help me to troubleshoot. I'm using just FGP_Value (which, of course, calls FGP_Init). I've tried both FGP_Value(FilePath,"Pages") and FGP_Value(FilePath,148) — both work perfectly. I haven't modified your functions. Here's my simple test code calling it:

Code: Select all

#NoEnv
SetBatchLines,-1
FileSelectFile,FilePath
PropValPages:=FGP_Value(FilePath,148)
MsgBox % PropValPages
ExitApp
I'm using it just on PDF files. It works perfectly on some computers, returning the number of pages in the PDF, but fails on others, returning 0. The machines are a mixture of W7 and W10, Home and Pro, all 64-bit. In some cases, I'm running from source code with AHK 1.1.27.07 U32; in other cases, I'm running the EXE (compiled with AHK 1.1.27.07 U32). But I'm pretty sure none of that matters. I think the issue has to do with the properties of the PDF files not being exposed due to the absence of something PDF-related on the machines where it fails. I don't think that this is an FGP issue, but I'm wondering if you've seen this before and can point me to the reason why FGP is not able to retrieve the number of pages in a PDF file. Thanks much, Joe

Update: There's a good discussion going on in this thread. Please have a look when you get a chance. Thanks!
User avatar
SKAN
Posts: 391
Joined: 29 Sep 2013, 16:58

Re: [Function] FGP - FileGetProperties

12 Dec 2018, 05:43

I have posted my Filexpro() \which makes use of (WIndow property system) canonical names.
I'm sharing here the list of property names extracted from WIN_XP, WIN_7, WIN_8 and WIN_10
List of Explorer column names (with index)
User avatar
joedf
Posts: 7403
Joined: 29 Sep 2013, 17:08
Facebook: J0EDF
Google: +joedf
GitHub: joedf
Location: Canada
Contact:

Re: [Function] FGP - FileGetProperties

12 Dec 2018, 10:05

Very nice! :+1: I just wonder where you get these lists?
Header files?
Image Image Image Image Image
Windows 10 x64 Professional, Intel i5-8500, NVIDIA GTX 1060 6GB, 2x8GB G.Skill RipJaws V - DDR4 3280 MHz | [About Me] | [ASPDM - StdLib Distribution]
[Populate the AHK MiniCity!] | [Qonsole - Quake-like console emulator] | [LibCon - Autohotkey Console Library] | [About the AHK Foundation]
User avatar
SKAN
Posts: 391
Joined: 29 Sep 2013, 16:58

Re: [Function] FGP - FileGetProperties

12 Dec 2018, 10:16

joedf wrote:I just wonder where you get these lists?
I have all 4 os in my desktop.
I booted into them one by one - generated individual lists - and then merged them into a single nested object
and printed this list with Format() command

:)
User avatar
joedf
Posts: 7403
Joined: 29 Sep 2013, 17:08
Facebook: J0EDF
Google: +joedf
GitHub: joedf
Location: Canada
Contact:

Re: [Function] FGP - FileGetProperties

12 Dec 2018, 13:44

Wow! :O ... nice! :+1:
Image Image Image Image Image
Windows 10 x64 Professional, Intel i5-8500, NVIDIA GTX 1060 6GB, 2x8GB G.Skill RipJaws V - DDR4 3280 MHz | [About Me] | [ASPDM - StdLib Distribution]
[Populate the AHK MiniCity!] | [Qonsole - Quake-like console emulator] | [LibCon - Autohotkey Console Library] | [About the AHK Foundation]
DRocks
Posts: 556
Joined: 08 May 2018, 10:20

Re: [Function] FGP - FileGetProperties

31 Dec 2018, 16:00

Very nice, used this today thank you!
carno
Posts: 209
Joined: 20 Jun 2014, 16:48

Re: [Function] FGP - FileGetProperties

01 Jan 2019, 21:30

I was always looking for a function to change a Shortcut file's properties. Specially, "Target" and "Start in" for a Shortcut file. How can this be achieved?
gregster
Posts: 3731
Joined: 30 Sep 2013, 06:48

Re: [Function] FGP - FileGetProperties

01 Jan 2019, 21:47

carno wrote:
01 Jan 2019, 21:30
I was always looking for a function to change a Shortcut file's properties. Specially, "Target" and "Start in" for a Shortcut file. How can this be achieved?
There is built-in FileGetShortcut to get info and FileCreateShortcut to create them (also able to overwrite=change existing shortcuts). You can determine both properties you mentioned with it.
carno
Posts: 209
Joined: 20 Jun 2014, 16:48

Re: [Function] FGP - FileGetProperties

01 Jan 2019, 22:07

How can I change the "Target" and "Start in" properties of a shortcut file?
gregster
Posts: 3731
Joined: 30 Sep 2013, 06:48

Re: [Function] FGP - FileGetProperties

01 Jan 2019, 22:17

Use the Target and WorkingDir parameters of FileCreateShortcut. "If the file already exists, it will be overwritten."

--> get info via FileGetShortcut and write info back (partially changed) with FileCreateShortcut: overwritten = changed

Return to “Scripts and Functions”

Who is online

Users browsing this forum: No registered users and 66 guests