Jump to content

Recommended Posts

Posted (edited)

Hi Everyone,

This is my first UDF, but I thought I would share it. It allows you to read, write, and clear the different file properties, including title, subject, author, etc.

To use this UDF, you must have the dsofile.dll in the script directory.

;===============================================================================
;
; Function Name:   File Property Management
; Description:      This utility is for reading/writing file properties
; Parameter(s): _FileProperty($fFile, $fProperty = "Title", $fValue = "", $fMode=0)  
;                  $fFile - File path of file to be adjusted
;                 $fProperty - Property to be adjusted (title, subject, category,
;                                keywords, comments, company, author)
;                  $fValue - Value to insert into field
;                 $fMode - 0 for read, 1 for write, 2 for remove
; Requirement(s):  DLL File in script directory - dsofile.dll
; Return Value(s): Property read if the read mode is specified.
; Author(s):        Andrew Goulart
;
;===============================================================================
;

Func _FileProperty($fFile, $fProperty = "Title", $fValue = "", $fMode = 0)  
    _DLLstartup()
    $return = ""

    Global $g_eventerror = 0; 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
    $objFile = ObjCreate("DSOFile.OleDocumentProperties")
    If $g_eventerror Then Exit
    $g_eventerror = 0
    If Not IsObj($objFile) Then Exit
    
    $objFile.Open($fFile); bind to the summary information metadata attached to the file.
    If $g_eventerror Then Exit
    $g_eventerror = 0

; Remove properties
    Switch $fProperty
        Case "Title"
            Switch $fMode
                Case 0
                    $return = $objFile.SummaryProperties.Title
                Case 1
                    $objFile.SummaryProperties.Title = $fValue
                Case 2
                    $objFile.SummaryProperties.Title = ""
            EndSwitch
        Case "Subject"
            Switch $fMode
                Case 0
                    $return = $objFile.SummaryProperties.Subject
                Case 1
                    $objFile.SummaryProperties.Subject = $fValue
                Case 2
                    $objFile.SummaryProperties.Subject = ""
            EndSwitch
        Case "Category"
            Switch $fMode
                Case 0
                    $return = $objFile.SummaryProperties.Category
                Case 1
                    $objFile.SummaryProperties.Category = $fValue
                Case 2
                    $objFile.SummaryProperties.Category = ""
            EndSwitch
        Case "Keywords"
            Switch $fMode
                Case 0
                    $return = $objFile.SummaryProperties.Keywords
                Case 1
                    $objFile.SummaryProperties.Keywords = $fValue
                Case 2
                    $objFile.SummaryProperties.Keywords = ""
            EndSwitch
        Case "Comments"
            Switch $fMode
                Case 0
                    $return = $objFile.SummaryProperties.Comments
                Case 1
                    $objFile.SummaryProperties.Comments = $fValue
                Case 2
                    $objFile.SummaryProperties.Comments = ""
            EndSwitch
        Case "Company"
            Switch $fMode
                Case 0
                    $return = $objFile.SummaryProperties.Company
                Case 1
                    $objFile.SummaryProperties.Company = $fValue
                Case 2
                    $objFile.SummaryProperties.Company = ""
            EndSwitch
        Case "Author"
            Switch $fMode
                Case 0
                    $return = $objFile.SummaryProperties.Author
                Case 1
                    $objFile.SummaryProperties.Author = $fValue
                Case 2
                    $objFile.SummaryProperties.Author = ""
            EndSwitch
    EndSwitch

    $objFile.Save; save changes to file properties
    If $g_eventerror Then Exit
    $g_eventerror = 0

    $objFile.Close; unbind from the summary information metadata attached to the file.
    
    If $return <> "" Then Return $return
    _DLLshutdown()
EndFunc

Func ComErrorHandler(); optionally bypass message box, or use ConsoleWrite, Debugview or log errors to file
    Local $sHexNumber = Hex($oMyError.number,8)
    Local $sDesc = StringStripWS($oMyError.windescription, 2)
    Msgbox(0,"AutoItCOM Test","We intercepted a COM Error !"        & @CRLF & @CRLF & _
             "err.description is: " & @TAB & $oMyError.description     & @CRLF & _
             "err.windescription:"   & @TAB & $oMyError.windescription  & @CRLF & _
             "err.number is: "       & @TAB & $sHexNumber               & @CRLF & _
             "err.scriptline is: "   & @TAB & $oMyError.scriptline      & @CRLF)
    $g_eventerror = 1; something to check for when this function returns
    $oMyError.clear
Endfunc

Func _DLLstartup($DLLpath = '')
    If $DLLpath = Default Or $DLLpath = '' Then $DLLpath = @ScriptDir & '\dsofile.dll'
    ShellExecuteWait('regsvr32', '/s /i ' & $DLLpath, @WindowsDir, 'open', @SW_HIDE)
EndFunc;==>_DLLstartup

Func _DLLshutdown($DLLpath = '')
    If $DLLpath = Default Or $DLLpath = '' Then $DLLpath = @ScriptDir & '\dsofile.dll'
    ShellExecuteWait('regsvr32', ' /s /u ' & $DLLpath, @WindowsDir, 'open', @SW_HIDE)
EndFunc;==>_DLLshutdown

dsofile.dll

FileProperty.au3

Edited by felanor
  • 3 weeks later...
Posted

Hi Everyone,

As requested, I am including some examples for this. Remember, to use this UDF, you must have the dsofile.dll in the script directory.

;===============================================================================
;
; Function Name:   File Property Management
; Description:      This utility is for reading/writing file properties
; Parameter(s): _FileProperty($fFile, $fProperty = "Title", $fValue = "", $fMode=0)  
;                  $fFile - File path of file to be adjusted
;                 $fProperty - Property to be adjusted (title, subject, category,
;                                keywords, comments, company, author)
;                  $fValue - Value to insert into field
;                 $fMode - 0 for read, 1 for write, 2 for remove
; Requirement(s):  DLL File in script directory - dsofile.dll
; Return Value(s): Property read if the read mode is specified.
; Author(s):        Andrew Goulart
;
;===============================================================================
;

Func _FileProperty($fFile, $fProperty = "Title", $fValue = "", $fMode = 0)  
    _DLLstartup()
    $return = ""

    Global $g_eventerror = 0; 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
    $objFile = ObjCreate("DSOFile.OleDocumentProperties")
    If $g_eventerror Then Exit
    $g_eventerror = 0
    If Not IsObj($objFile) Then Exit
    
    $objFile.Open($fFile); bind to the summary information metadata attached to the file.
    If $g_eventerror Then Exit
    $g_eventerror = 0

; Remove properties
    Switch $fProperty
        Case "Title"
            Switch $fMode
                Case 0
                    $return = $objFile.SummaryProperties.Title
                Case 1
                    $objFile.SummaryProperties.Title = $fValue
                    $return = 1
                Case 2
                    $objFile.SummaryProperties.Title = ""
                    $return = 1
                Case Else
                    $return = 0
                    @error = 1
            EndSwitch
        Case "Subject"
            Switch $fMode
                Case 0
                    $return = $objFile.SummaryProperties.Subject
                Case 1
                    $objFile.SummaryProperties.Subject = $fValue
                    $return = 1
                Case 2
                    $objFile.SummaryProperties.Subject = ""
                    $return = 1
                Case Else
                    $return = 0
                    @error = 1
            EndSwitch
        Case "Category"
            Switch $fMode
                Case 0
                    $return = $objFile.SummaryProperties.Category
                Case 1
                    $objFile.SummaryProperties.Category = $fValue
                    $return = 1
                Case 2
                    $objFile.SummaryProperties.Category = ""
                    $return = 1
                Case Else
                    $return = 0
                    @error = 1
            EndSwitch
        Case "Keywords"
            Switch $fMode
                Case 0
                    $return = $objFile.SummaryProperties.Keywords
                Case 1
                    $objFile.SummaryProperties.Keywords = $fValue
                    $return = 1
                Case 2
                $objFile.SummaryProperties.Keywords = ""
                    $return = 1
                Case Else
                    $return = 0
                    @error = 1
            EndSwitch
        Case "Comments"
            Switch $fMode
                Case 0
                    $return = $objFile.SummaryProperties.Comments
                Case 1
                    $objFile.SummaryProperties.Comments = $fValue
                    $return = 1
                Case 2
                    $objFile.SummaryProperties.Comments = ""
                    $return = 1
                Case Else
                    $return = 0
                    @error = 1
            EndSwitch
        Case "Company"
            Switch $fMode
                Case 0
                    $return = $objFile.SummaryProperties.Company
                Case 1
                    $objFile.SummaryProperties.Company = $fValue
                    $return = 1
                Case 2
                    $objFile.SummaryProperties.Company = ""
                    $return = 1
                Case Else
                    $return = 0
                    @error = 1
            EndSwitch
        Case "Author"
            Switch $fMode
                Case 0
                    $return = $objFile.SummaryProperties.Author
                Case 1
                    $objFile.SummaryProperties.Author = $fValue
                    $return = 1
                Case 2
                    $objFile.SummaryProperties.Author = ""
                    $return = 1
                Case Else
                    $return = 0
                    @error = 1
            EndSwitch
        Case Else
        $return = 0
        @error = 2
    EndSwitch

    $objFile.Save; save changes to file properties
    If $g_eventerror Then Exit
    $g_eventerror = 0

    $objFile.Close; unbind from the summary information metadata attached to the file.
    _ErrorCheck($return,$fMode,$fProperty)
    If $return <> "" Then Return $return
    _DLLshutdown()
EndFunc

Func ComErrorHandler(); optionally bypass message box, or use ConsoleWrite, Debugview or log errors to file
    Local $sHexNumber = Hex($oMyError.number,8)
    Local $sDesc = StringStripWS($oMyError.windescription, 2)
    Msgbox(0,"AutoItCOM Test","We intercepted a COM Error !"        & @CRLF & @CRLF & _
             "err.description is: " & @TAB & $oMyError.description     & @CRLF & _
             "err.windescription:"   & @TAB & $oMyError.windescription  & @CRLF & _
             "err.number is: "       & @TAB & $sHexNumber               & @CRLF & _
             "err.scriptline is: "   & @TAB & $oMyError.scriptline      & @CRLF)
    $g_eventerror = 1; something to check for when this function returns
    $oMyError.clear
Endfunc

Func _DLLstartup($DLLpath = '')
    If $DLLpath = Default Or $DLLpath = '' Then $DLLpath = @ScriptDir & '\dsofile.dll'
    ShellExecuteWait('regsvr32', '/s /i ' & $DLLpath, @WindowsDir, 'open', @SW_HIDE)
EndFunc;==>_DLLstartup

Func _DLLshutdown($DLLpath = '')
    If $DLLpath = Default Or $DLLpath = '' Then $DLLpath = @ScriptDir & '\dsofile.dll'
    ShellExecuteWait('regsvr32', ' /s /u ' & $DLLpath, @WindowsDir, 'open', @SW_HIDE)
EndFunc;==>_DLLshutdown

Func _ErrorCheck($var, $mode, $property)
    If @error = 1 Then
        MsgBox(16, "Error", "The specified mode: " & $mode & ", was not 0, 1, or 2.")
        Exit
    EndIf
    If @error = 2 Then
        MsgBox(16, "Error", "The specified file property: " & $property & ", is not supported.")
        Exit
    EndIf
EndFunc

EXAMPLES

; READING A FILE'S TITLE PROPERTY
$filename = FileOpenDialog("Please Select A File", @DesktopDir, "All Files (*.*)",3)
$filetitle = _FileProperty($filename, "Title", "", 0)
MsgBox(64, "Title Property for File: " & $filename & ".", "The title of the file selected is " & $filetitle & ".")

; WRITING A FILE'S AUTHOR VALUE
$filename = FileOpenDialog("Please Select A File", @DesktopDir, "All Files (*.*)",3)
_FileProperty($filename, "Author", "Felanor", 1)
$fileauthor = _FileProperty($filename, "Author", "", 0)
MsgBox(64, "Author Property for File: " & $filename & ".", "The author of the file selected is " & $fileauthor & ".")

If you need any more examples, please let me know. The examples provided will work with either the original code, or the updated code that can be found in the codebox above. I included a bit more error checking.

@ptrex, I did not realize someone else had already done this. I was having a devil of a finding some existing code to extrapolate from. I did find one article prior, but sadly I dont remember who wrote it, however, it wasn't in any convenient UDF format. I am hoping that this will simplify the process for anyone who needs to modify file properties.

~Felanor

  • 1 year later...
Posted

14 downloads and no replies?

I know it's been over a year since you wrote this, but I just found it. It is very useful! Thank you!

Two comments:

1) As an UDF, it should not do registering and unregistering of DLL... it's a slow process. Why not just make it a required item to register before hand?

2) There is a note about UDF should be careful about handling the COM Errors, since there can be only one handler per instance of the running code.

Thanks again!

  • 5 weeks later...
Posted

............@ptrex, I did not realize someone else had already done this. I was having a devil of a finding some existing code to extrapolate from. I did find one article prior, but sadly I dont remember who wrote it, however, it wasn't in any convenient UDF format. I am hoping that this will simplify the process for anyone who needs to modify file properties.

~Felanor

Hello, just searching "autoit dsofile" on google will bring you this topic has first result.

  • 4 months later...
  • 2 years later...
Posted

Try running it as an x86 script, and not as a 64 bit program. Try adding this line"#AutoIt3Wrapper_UseX64=n" to the top of the script and see if that helps.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...