felanor Posted May 25, 2008 Posted May 25, 2008 (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. expandcollapse popup;=============================================================================== ; ; 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;==>_DLLshutdowndsofile.dllFileProperty.au3 Edited May 25, 2008 by felanor Check Out My ScriptsFile Property Management - Adjust a file's title, author, subject, etc.
felanor Posted May 28, 2008 Author Posted May 28, 2008 14 downloads and no replies? Check Out My ScriptsFile Property Management - Adjust a file's title, author, subject, etc.
Michel Claveau Posted May 29, 2008 Posted May 29, 2008 Hi! Cool. I am surprised that Ptrex did not already show its enthusiasm...
ptrex Posted May 30, 2008 Posted May 30, 2008 @Michel ClaveauI am surprised that Ptrex did not already show its enthusiasmThis did not come to me as something new.Because it was already discussed long time ago ?!Did you miss that thread.regards,ptrex Contributions :Firewall Log Analyzer for XP - Creating COM objects without a need of DLL's - UPnP support in AU3Crystal Reports Viewer - PDFCreator in AutoIT - Duplicate File FinderSQLite3 Database functionality - USB Monitoring - Reading Excel using SQLRun Au3 as a Windows Service - File Monitor - Embedded Flash PlayerDynamic Functions - Control Panel Applets - Digital Signing Code - Excel Grid In AutoIT - Constants for Special Folders in WindowsRead data from Any Windows Edit Control - SOAP and Web Services in AutoIT - Barcode Printing Using PS - AU3 on LightTD WebserverMS LogParser SQL Engine in AutoIT - ImageMagick Image Processing - Converter @ Dec - Hex - Bin -Email Address Encoder - MSI Editor - SNMP - MIB ProtocolFinancial Functions UDF - Set ACL Permissions - Syntax HighLighter for AU3ADOR.RecordSet approach - Real OCR - HTTP Disk - PDF Reader Personal Worldclock - MS Indexing Engine - Printing ControlsGuiListView - Navigation (break the 4000 Limit barrier) - Registration Free COM DLL Distribution - Update - WinRM SMART Analysis - COM Object Browser - Excel PivotTable Object - VLC Media Player - Windows LogOnOff Gui -Extract Data from Outlook to Word & Excel - Analyze Event ID 4226 - DotNet Compiler Wrapper - Powershell_COM - New
felanor Posted June 18, 2008 Author Posted June 18, 2008 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. expandcollapse popup;=============================================================================== ; ; 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 Check Out My ScriptsFile Property Management - Adjust a file's title, author, subject, etc.
hhzz Posted September 9, 2009 Posted September 9, 2009 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!
sksbir Posted October 8, 2009 Posted October 8, 2009 ............@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.~FelanorHello, just searching "autoit dsofile" on google will bring you this topic has first result.
RagsRevenge Posted February 11, 2010 Posted February 11, 2010 Felanor. Thanks for this. It'll prove invaluable in a forthcoming project. D
photonbuddy Posted June 24, 2012 Posted June 24, 2012 Hi All, I know this is pulling a very old thread from the grave, but ... This routine doesn't seem to work under Win7/x64. Anyone have a solution to write file comments?
BrewManNH Posted June 24, 2012 Posted June 24, 2012 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 GudeHow 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
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now