[Function] FGP - FileGetProperties

Post your working scripts, libraries and tools for AHK v1.1 and older
User avatar
JoeWinograd
Posts: 2202
Joined: 10 Feb 2014, 20:00
Location: U.S. Central Time Zone

Re: [Function] FGP - FileGetProperties

01 Jan 2019, 22:33

Hi carno,
gregster gave you the solution, i.e., FileCreateShortcut. If you want some sample code using just the properties that you mentioned ("Target" and "Start in"), here it is:

Code: Select all

ShortcutFile:="c:\temp\calc.lnk"
Target:="c:\Windows\system32\calc.exe"
StartIn:="c:\Windows\system32\"
FileCreateShortcut,%Target%,%ShortcutFile%,%StartIn%
Regards, Joe
carno
Posts: 265
Joined: 20 Jun 2014, 16:48

Re: [Function] FGP - FileGetProperties

04 Jan 2019, 10:49

gregster wrote:
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
Thanks, absolutely beautiful! I had no idea about the FileGetShortcut and FileCreateShortcut. :)
User avatar
JoeWinograd
Posts: 2202
Joined: 10 Feb 2014, 20:00
Location: U.S. Central Time Zone

Re: [Function] FGP - FileGetProperties

25 May 2019, 10:30

I have a question related to this library but wasn't sure if posting it here or in a separate thread was better. I decided on the latter:

How to set file properties - SetDetailsOf - FileSetProperties

I'd certainly appreciate the help of kon and anyone else on this thread. Thanks, Joe
User avatar
elModo7
Posts: 217
Joined: 01 Sep 2017, 02:38
Location: Spain
Contact:

Re: [Function] FGP - FileGetProperties

28 May 2019, 02:30

Thanks for this! stumbled upon it a few times but now I really find a use out of it, thanks for the time @kon
User avatar
SirSocks
Posts: 360
Joined: 26 Oct 2018, 08:14

Re: [Function] FGP - FileGetProperties

19 Feb 2020, 09:43

I'm trying to get the GPS location "Latitude" and "longitude". This always Returns "-1" for me. Does anyone know how to get the GPS coordinates of an image?
somethingfly
Posts: 38
Joined: 23 Mar 2015, 17:23

Re: [Function] FGP - FileGetProperties

01 May 2020, 18:01

FYI, results may have unicode hidden characters. I think the reason has to do with this: https://devblogs.microsoft.com/oldnewthing/20150506-00/?p=44924 . You may need to strip the hidden characters depending on what you are doing.

I was trying to get the "Last Saved Date" of a file then convert that value to YYYYMMDDHH24MISS. My eventual intention was to use the converted value to set the created or modified date. I tried using (the American version) of this https://autohotkey.com/board/topic/18760-date-parser-convert-any-date-format-to-yyyymmddhh24miss/ . However, I kept getting YYYYMMDDHH24MISS for the current local date and time, not the last saved date. I found I needed to first convert the "Last Saved Date" to ascii, per https://autohotkey.com/board/topic/92405-is-it-possible-to-remove-all-unicode-characters/ . Then I could date parse it to YYYYMMDDHH24MISS.

I haven't bothered to test that my problem was indeed hidden unicode characters, such as a U+202A at the beginning of the Last Saved Date property, I just know converting to ascii worked for me and thought it might help someone else.

Code: Select all

FileSelectFile, FilePath
datelastsaved := FGP_Value(FilePath,154)
; RegExReplace(datelastsaved,"[^0-9/APM:\s]+") also works
datelastsavedfix := RegExReplace(datelastsaved,"[^[:ascii:]]")
datelastparsed := DateParse(datelastsavedfix)
msgbox % datelastparsed


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_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
}


DateParse(str, americanOrder=1) {
	static monthNames := "(?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\w*"
		, dayAndMonth := "(?:(\d{1,2}|" . monthNames . ")[\s\.\-\/,]+)?(\d{1,2}|" . monthNames . ")"
	If RegExMatch(str, "i)^\s*(?:(\d{4})([\s\-:\/])(\d{1,2})\2(\d{1,2}))?"
		. "(?:\s*[T\s](\d{1,2})([\s\-:\/])(\d{1,2})(?:\6(\d{1,2})\s*(?:(Z)|(\+|\-)?"
		. "(\d{1,2})\6(\d{1,2})(?:\6(\d{1,2}))?)?)?)?\s*$", i) ;ISO 8601 timestamps
		year := i1, month := i3, day := i4, t1 := i5, t2 := i7, t3 := i8
	Else If !RegExMatch(str, "^\W*(\d{1,2}+)(\d{2})\W*$", t){
		RegExMatch(str, "i)(\d{1,2})"					;hours
				. "\s*:\s*(\d{1,2})"				;minutes
				. "(?:\s*:\s*(\d{1,2}))?"			;seconds
				. "(?:\s*([ap]m))?", t)				;am/pm
		StringReplace, str, str, %t%
		If Regexmatch(str, "i)(\d{4})[\s\.\-\/,]+" . dayAndMonth, d) ;2004/22/03
			year := d1, month := d3, day := d2
		Else If Regexmatch(str, "i)" . dayAndMonth . "[\s\.\-\/,]+(\d{2,4})", d)  ;22/03/2004 or 22/03/04
			year := d3, month := d2, day := d1
		If (RegExMatch(day, monthNames) or americanOrder and !RegExMatch(month, monthNames)) ;try to infer day/month order
			tmp := month, month := day, day := tmp
	}
	f = %A_FormatFloat%
	SetFormat, Float, 02.0
	d := (StrLen(year) == 2 ? "20" . year : (year ? year : A_YYYY))
		. ((month := month + 0 ? month : InStr(monthNames, SubStr(month, 1, 3)) // 4 ) > 0 ? month + 0.0 : A_MM)
		. ((day += 0.0) ? day : A_DD) 
		. t1 + (t1 == 12 ? t4 = "am" ? -12.0 : 0.0 : t4 = "pm" ? 12.0 : 0.0)
		. t2 + 0.0 . t3 + 0.0
	SetFormat, Float, %f%
	return, d
}
burakyak
Posts: 3
Joined: 14 Nov 2020, 10:19

Re: [Function] FGP - FileGetProperties

14 Nov 2020, 12:02

Thank you so much @kon. 🙏🏼
ericmil8
Posts: 2
Joined: 14 Aug 2023, 10:32

Re: [Function] FGP - FileGetProperties

30 Oct 2023, 12:07

Here are the Functions adapted for V2.0+

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 (!IsSet(PropTable)) {
        ;PropTable := {Name:={},Num:={} }
        ;PropTable := {{},{}}
        PropTable := {}
        PropTable.Name := Map()
        PropTable.Num := Map()
        Gap := 0
		oShell := ComObject("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 
                ;PropTable.Num.InsertAt( 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 := ComObject("Shell.Application")
	oFolder := oShell.NameSpace(DirPath)
	oFolderItem := oFolder.ParseName(FileName)
    PropList := {}
    PropList.Name := Map()
    PropList.Num := Map()
    PropList.CSV := ""
	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.Has(Property) ? PropTable.Name[Property] : PropTable.Num[Property] ? Property : "") != "") {
		SplitPath(FilePath, &FileName, &DirPath)
		oShell := ComObject("Shell.Application")
		oFolder := oShell.NameSpace(DirPath)
		oFolderItem := oFolder.ParseName(FileName)
		if (PropVal := oFolder.GetDetailsOf(oFolderItem, PropNum))
			return PropVal
		return 0
	}
	return -1
}

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: No registered users and 80 guests