Sign in to follow this  
Followers 0
benleb

File properties not persistent when using 'SHObjectProperties'

4 posts in this topic

Hi everyone. I'm new to AutoIt and this forum, so please forgive any newbie behavior...

Here's a script snippet where I try to set a file's property using CallDLL() and 'SHObjectProperties'. The 'Comments' field on the 'Summary' tab is the targetted property, where I want to write four strings:

; Identify Properties window
Dim $strPropWinTitle = "[REGEXPTITLE:Propriétés | Properties]"

; Create some file
$file = FileOpen(@DesktopDir & "\SomeUnusefulFile.txt", 1)

If $file = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf

FileClose($file)

; Call file's properties dialog
$intReturn = DllCall("shell32.dll", "int", "SHObjectProperties", "hwnd", 0, "dword", _
            0x00000002, "wstr", @DesktopDir & "\SomeUnusefulFile.txt", "wstr", 0)

; Wait for dialog to come and activate 'Summary' tab
WinWaitActive($strPropWinTitle)
$strTab = ControlCommand($strPropWinTitle, "", "[ID:12320]", "CurrentTab", "")
If Not StringRegExp($strTab, "Général|General") Then
    ControlCommand($strPropWinTitle, "", "[ID:12320]", "TabRight", "")
    ControlCommand($strPropWinTitle, "", "[ID:12320]", "TabRight", "")
ElseIf Not StringRegExp($strTab, "Sécurité|Security") Then
        ControlCommand($strPropWinTitle, "", "[ID:12320]", "TabRight", "")
EndIf

; Create the comment string to write
$strComment = "20090413154626" & "|" & _
        "Really hope someone could help me with this piece of code,..." & "|" & _
        "since I searched and googled it a lot with no solution." & "|" & _
        "20090721163452"

; Write the string and click OK button to save and dismiss dialog
ControlSetText($strPropWinTitle, "", "[ID:321]", $strComment)
ControlClick($strPropWinTitle, "", "[ID:1]")

(BTW, windows and control titles must match even french or english OS versions)

The issue is that the comment string doesn't get saved within the file at the end of the script. I can see it written on runtime, but when I manually call the file's property dialog afterward, the 'Comment' field is still blank... Something weird I noted, the file timestamp gets updated with current system time.

Could someone help me on this?

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Hi everyone. I'm new to AutoIt and this forum, so please forgive any newbie behavior...

Here's a script snippet where I try to set a file's property using CallDLL() and 'SHObjectProperties'. The 'Comments' field on the 'Summary' tab is the targetted property, where I want to write four strings:

; Identify Properties window
  Dim $strPropWinTitle = "[REGEXPTITLE:Propriétés | Properties]"
  
  ; Create some file
  $file = FileOpen(@DesktopDir & "\SomeUnusefulFile.txt", 1)
  
  If $file = -1 Then
      MsgBox(0, "Error", "Unable to open file.")
      Exit
  EndIf
 
  
  FileClose($file)
  
  ; Call file's properties dialog
  $intReturn = DllCall("shell32.dll", "int", "SHObjectProperties", "hwnd", 0, "dword", _
            0x00000002, "wstr", @DesktopDir & "\SomeUnusefulFile.txt", "wstr", 0)
  
  ; Wait for dialog to come and activate 'Summary' tab
  WinWaitActive($strPropWinTitle)
  $strTab = ControlCommand($strPropWinTitle, "", "[ID:12320]", "CurrentTab", "")
  If Not StringRegExp($strTab, "Général|General") Then
    ControlCommand($strPropWinTitle, "", "[ID:12320]", "TabRight", "")
    ControlCommand($strPropWinTitle, "", "[ID:12320]", "TabRight", "")
  ElseIf Not StringRegExp($strTab, "Sécurité|Security") Then
        ControlCommand($strPropWinTitle, "", "[ID:12320]", "TabRight", "")
  EndIf
  
  ; Create the comment string to write
  $strComment = "20090413154626" & "|" & _
        "Really hope someone could help me with this piece of code,..." & "|" & _
        "since I searched and googled it a lot with no solution." & "|" & _
        "20090721163452"
  
  ; Write the string and click OK button to save and dismiss dialog
  ControlSetText($strPropWinTitle, "", "[ID:321]", $strComment)
  ControlClick($strPropWinTitle, "", "[ID:1]")

(BTW, windows and control titles must match even french or english OS versions)

The issue is that the comment string doesn't get saved within the file at the end of the script. I can see it written on runtime, but when I manually call the file's property dialog afterward, the 'Comment' field is still blank... Something weird I noted, the file timestamp gets updated with current system time.

Could someone help me on this?

Welcome to the forums benleb

you need to set the focus on the comment edit control

that's why the text was not written

you can eliminate the tab navigation code by setting

the dialogs default properties tab,

and use @OSLang to set language of tab text

added GetExtProperty by Simucal to check file properties.

look for Dsofile.dll on forum as alternative to setting file properties

Drawback: requires installing and registering dll on target system

EDIT: don't use the COM file properties function. On one XP SP2 machine of mine the Comment property value is 19, not 14

it's probably some com version difference.

the Vista dialog does not have a comments tab.

I don't know what the file properties setting limitations are for Vista, I don't use it.

Global $g_eventerror = 0, $sCOMError ; to be checked to know if com error occurs. Must be reset after handling.
Global $oMyError = ObjEvent("AutoIt.Error", "ComErrorHandler"); Install a custom error handler

Global Const $SHOP_FILEPATH = 0x00000002

; Create the comment string to write
Global $strComment = "20090413154626" & "|" & _
        "Really hope someone could help me with this piece of code,..." & "|" & _
        "since I searched and googled it a lot with no solution." & "|" & _
        "20090721163452"

; Identify Properties window
Global $strPropWinTitle = "[REGEXPTITLE:Propriétés | Properties; CLASS:#32770]"

Global $hProperties, $sTab, $hEdit, $sComment

Switch @OSLang
    Case "040c", "080c", "0c0c", "100c", "140c", "180c" ; French
        $sTab = "Résumé"
    ;Case Else
    Case "1009", "0409", "0809", "0c09", "1409", "1809", "1c09", "2009", "2409", "2809", "2c09", "3009", "3409" ; English
        $sTab = "Summary"
EndSwitch

; Create some file
Global $file = FileOpen(@TempDir & "\SomeUnusefulFile.txt", 1)
If $file = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf
FileClose($file)


; Call file's properties dialog with 'Summary' property sheet page as default tab
Global $intReturn = DllCall("shell32.dll", "int", "SHObjectProperties", "hwnd", 0, "dword", _
        $SHOP_FILEPATH, "wstr", @TempDir & "\SomeUnusefulFile.txt", "wstr", $sTab)

; Wait for dialog to come and activate 'Summary' tab
WinWaitActive($strPropWinTitle, "", 3)
If Not WinExists($strPropWinTitle) Then
    MsgBox(0, "Window does not exist", $strPropWinTitle)
    Exit
EndIf

$hProperties = WinGetHandle($strPropWinTitle, "")
If Not IsHWnd($hProperties) Then Exit

; Write the string and click OK button to save and dismiss dialog
$hEdit = ControlGetHandle($hProperties, "", "[CLASS:Edit; ID:321]")
If Not IsHWnd($hEdit) Then
    MsgBox(0, "Control handle error", "[CLASS:Edit; ID:321]")
    Exit
EndIf

ControlFocus($hProperties, "", $hEdit)
ControlSetText($hProperties, "", $hEdit, $strComment)
ControlClick($hProperties, "", "[CLASS:Button; ID:1]")

;retrieve comment text from file properties
$sComment = _GetExtProperty(@TempDir & "\SomeUnusefulFile.txt", 14)
;check COM error
If $g_eventerror = 1 Or @error <> 0 Then
    $g_eventerror = 0
    MsgBox(0, "COM Error", "COM: " & $sCOMError & @CRLF & "Error: " & @error)
EndIf

ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $sComment = ' & $sComment & @crlf & '>Error code: ' & @error & @crlf)

If StringCompare($strComment, $sComment) <> 0 Then
    MsgBox(0, "Error setting file comment property", "Comment string does not match $strComment")
Else
    MsgBox(0, "File comment property set", $sComment)
EndIf

Func ComErrorHandler()
    Local $sHexNumber = Hex($oMyError.number, 8)
    $sCOMError = StringStripWS($oMyError.windescription, 2)
    $g_eventerror = 1 ; something to check for when this function returns
    $oMyError.clear
EndFunc   ;==>ComErrorHandler

;===============================================================================
; modified version: array return removed, object error handling added
; Function Name:    GetExtProperty($sPath,$iProp)
; Description:      Returns an extended property of a given file.
; Parameter(s):     $sPath - The path to the file you are attempting to retrieve an extended property from.
;                   $iProp - The numerical value for the property you want returned
;                     The properties are as follows:
;                     Name = 0
;                     Size = 1
;                     Type = 2
;                     DateModified = 3
;                     DateCreated = 4
;                     DateAccessed = 5
;                     Attributes = 6
;                     Status = 7
;                     Owner = 8
;                     Author = 9
;                     Title = 10
;                     Subject = 11
;                     Category = 12
;                     Pages = 13
;                     Comments = 14
;                     Copyright = 15
;                     Artist = 16
;                     AlbumTitle = 17
;                     Year = 18
;                     TrackNumber = 19
;                     Genre = 20
;                     Duration = 21
;                     BitRate = 22
;                     Protected = 23
;                     CameraModel = 24
;                     DatePictureTaken = 25
;                     Dimensions = 26
;                     Width = 27
;                     Height = 28
;                     Company = 30
;                     Description = 31
;                     FileVersion = 32
;                     ProductName = 33
;                     ProductVersion = 34
; Requirement(s):   File specified in $spath must exist.
; Return Value(s):  On Success - The extended file property
;                   On Failure - 0, @Error - 1 (If file does not exist)
; Author(s):        Simucal (Simucal@gmail.com)
; Note(s):
;
;===============================================================================
Func _GetExtProperty($sPath, $iProp = 14)
    Local $iExist, $sFile, $sDir, $oShellApp, $oDir, $oFile, $aProperty, $sProperty
    $iExist = FileExists($sPath)
    If $iExist = 0 Then Return SetError(1, 0, "")
    $sFile = StringTrimLeft($sPath, StringInStr($sPath, "\", 0, -1))
    $sDir = StringTrimRight($sPath, (StringLen($sPath) - StringInStr($sPath, "\", 0, -1)))
    $oShellApp = ObjCreate("shell.application")
    If Not IsObj($oShellApp) Then Return SetError(2, 0, "")
    $oDir = $oShellApp.NameSpace($sDir)
    If Not IsObj($oDir) Then Return SetError(3, 0, "")
    $oFile = $oDir.Parsename($sFile)
    If Not IsObj($oFile) Then Return SetError(4, 0, "")
    Local $sComment = $oDir.GetDetailsOf($oFile, $iProp)
    Return SetError(0, 0, $sComment)
EndFunc   ;==>_GetExtProperty
Edited by rover

I see fascists...

Share this post


Link to post
Share on other sites

Hey Rover, thanks for this quick and detailed reply. I really do appreciate it!

I'll investigate on every tips you supplied and let you know in a short time. Thx again!! :)

Share this post


Link to post
Share on other sites

Hi Rover,

Finally used 'dsofile' to manage file properties. I tested it with a sample script on XP SP2 and Vista Home Premium, and it performs cleanly and quickly! No more tab browsing, no more control management... I sure have to test furthermore, but until now it delivers what it promises.

Did I ever say thank you? >_<

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0