Author Topic: Get_File_Properties  (Read 6952 times)

0 Members and 1 Guest are viewing this topic.

MP

  • Seagull
  • Posts: 17258
Get_File_Properties
« on: April 26, 2011, 04:27:34 PM »
Reader's Digest Version: I needed a function to grab the extended properties for a file (specifically Title, Subject, Category ... for LISP files). I wrote this quick and dirty and it appears to work:

Code: [Select]
(defun [color=red]_Get_File_Properties_Ex[/color] ( file / result shell name_space items item )

    [color=green];;  Get a file's extended properties (Title, Subject, Category ...)
    ;;  returning the result as a list of cons pairs:
    ;;
    ;;      (   ("Name" . "Drawing.dwg")
    ;;          ("Size" . "42 KB")
    ;;          ("Type" . "DWG File")
    ;;          ("Date Modified" . "2011/03/21 10:57 AM")
    ;;          ...
    ;;      )[/color]

    (vl-catch-all-apply
        (function
            (lambda ( / folder_name file_name i count )    
                (setq
                    folder_name (vl-filename-directory file)
                    file_name   (strcat (vl-filename-base file) (vl-filename-extension file))
                    shell       (vlax-create-object "Shell.Application")
                    name_space  (vlax-invoke shell 'NameSpace folder_name)
                    items       (vlax-invoke name_space 'Items)
                    item        (vlax-invoke name_space 'ParseName file_name)
                    i           (setq count 300)
                )
                (if item
                    (repeat count
                        (setq result
                            (cons
                                (cons
                                    (vlax-invoke
                                        name_space
                                       'GetDetailsOf
                                        items
                                        (setq i (1- i))
                                    )
                                    (vlax-invoke
                                        name_space
                                       'GetDetailsOf
                                        item
                                        i
                                    )
                                )    
                                result
                            )
                        )
                    )
                )    
            )                
        )        
    )
    
    (vl-catch-all-apply 'vlax-release-object (list item))
    (vl-catch-all-apply 'vlax-release-object (list items))
    (vl-catch-all-apply 'vlax-release-object (list name_space))
    (vl-catch-all-apply 'vlax-release-object (list shell))
    
    (vl-remove-if
        (function (lambda (item) (eq "" (car item))))
        result
    )    
    
)

Test run (under XP):

(_Get_File_Properties_Ex "c:\\tests\\SampleDrawing.dwg")

(
    ("Name" . "SampleDrawing.dwg")
    ("Size" . "42 KB")
    ("Type" . "DWG File")
    ("Date Modified" . "2011/03/21 10:57 AM")
    ("Date Created" . "2011/03/10 3:03 PM")
    ("Date Accessed" . "2011/04/26 1:32 PM")
    ("Attributes" . "A")
    ("Status" . "Online")
    ("Owner" . "COMPANY\\USERNAME")
    ("Author" . "")
    ("Title" . "")
    ("Subject" . "")
    ("Category" . "")
    ("Pages" . "")
    ("Comments" . "")
    ("Copyright" . "")
    ("Artist" . "")
    ("Album Title" . "")
    ("Year" . "")
    ("Track Number" . "")
    ("Genre" . "")
    ("Duration" . "")
    ("Bit Rate" . "")
    ("Protected" . "")
    ("Camera Model" . "")
    ("Date Picture Taken" . "")
    ("Dimensions" . "")
    ("Episode Name" . "")
    ("Program Description" . "")
    ("Audio sample size" . "")
    ("Audio sample rate" . "")
    ("Channels" . "")
    ("Company" . "")
    ("Description" . "")
    ("File Version" . "")
    ("Product Name" . "")
    ("Product Version" . "")
    ("DWG Creator Name" . "")
    ("DWG Creator Version" . "")
    ("Keywords" . "")
)


I could use the black board for the shell object >> next version will, no time right now. PS: Let me know if it works / doesn't work for you. Thanks and cheers, MP.

PS: For use with any file, not just drawings. I'm using it to categorize LISP files regardless of their folder parentage.

Edit: Revised number items the function interrogates for from 34 to 300.
« Last Edit: April 27, 2011, 10:59:47 AM by MP »
\|// Set goal. Experiment tirelessly until
|Oo| practice has become expertise.  Loop.
|- | LinkedIn | Dropbox | About

alanjt

  • Needs a day job
  • Posts: 5324
  • Standby for witty remark...
Re: Get_File_Properties
« Reply #1 on: April 26, 2011, 04:43:03 PM »
Very nice! Works well on my end. Tried it on a DWG and an MP3.

eg.
Code: [Select]
Command: (mapcar 'print (_get_file_properties (getfiled "" "" "" 2)))

("Name" . "01 - Going up.mp3")
("Size" . "6.13 MB")
("Item type" . "MP3 Format Sound")
("Date modified" . "1/24/2011 11:36 AM")
("Date created" . "11/29/2010 2:35 PM")
("Date accessed" . "11/29/2010 2:51 PM")
("Attributes" . "A")
("Offline status" . "")
("Offline availability" . "")
("Perceived type" . "Audio")
("Owner" . "ENGINEER\\athompson")
("Kind" . "Music")
("Date taken" . "")
("Contributing artists" . "Echo & the Bunnymen")
("Album" . "Crocodiles")
("Year" . "1980")
("Genre" . "New Wave")
("Conductors" . "")
("Tags" . "")
("Rating" . "Unrated")
("Authors" . "Echo & the Bunnymen")
("Title" . "Going up")
("Subject" . "")
("Categories" . "")
("Comments" . " 0000067C 000005A3 00001F33 0000144A 00021AF3 0001838F 00006562
00006A2F 0000E2FD 0000E2FD")
("Copyright" . "")
("#" . "1")
("Length" . "00:03:59")
("Bit rate" . "?214kbps")
("Protected" . "No")
("Camera model" . "")
("Dimensions" . "")
("Camera maker" . "")
("Company" . "")
("File description" . "")
Civil 3D 2017 ~ Windohz 7 64bit
Dropbox

MP

  • Seagull
  • Posts: 17258
Re: Get_File_Properties
« Reply #2 on: April 26, 2011, 04:44:58 PM »
Thanks for checking Alan. :)
\|// Set goal. Experiment tirelessly until
|Oo| practice has become expertise.  Loop.
|- | LinkedIn | Dropbox | About

ronjonp

  • Needs a day job
  • Posts: 6830
Re: Get_File_Properties
« Reply #3 on: April 26, 2011, 04:45:00 PM »
Works here too ... Nice work as usual Michael :)

Windows 10 x64 - AutoCAD /C3D 2019

Custom Build PC

MP

  • Seagull
  • Posts: 17258
Re: Get_File_Properties
« Reply #4 on: April 26, 2011, 04:46:55 PM »
Thanks Ron. :)
\|// Set goal. Experiment tirelessly until
|Oo| practice has become expertise.  Loop.
|- | LinkedIn | Dropbox | About

Lee Mac

  • Seagull
  • Posts: 12129
  • London, England
Re: Get_File_Properties
« Reply #5 on: April 26, 2011, 06:42:34 PM »
Nice Michael!  8-)

MP

  • Seagull
  • Posts: 17258
Re: Get_File_Properties
« Reply #6 on: April 27, 2011, 12:02:22 AM »
Thank you Lee. :)
\|// Set goal. Experiment tirelessly until
|Oo| practice has become expertise.  Loop.
|- | LinkedIn | Dropbox | About

gskelly

  • Newt
  • Posts: 185
Re: Get_File_Properties
« Reply #7 on: April 27, 2011, 08:08:03 AM »
PS: Let me know if it works / doesn't work for you. Thanks and cheers, MP.

Tested successfully with Bricsad v11 on WinXP Pro SP3.

Code: [Select]
(
    ("Name" . "myra.dwg")
    ("Size" . "48 KB")
    ("Type" . "AcadDocument Class")
    ("Date Modified" . "27/04/2011 9:01 AM")
    ("Date Created" . "16/03/2011 9:18 AM")
    ("Date Accessed" . "22/03/2011 5:55 PM")
    ("Attributes" . "A")
    ("Status" . "Online")
    ("Owner" . "GREG-WS\\greg")
    ("Author" . "You")
    ("Title" . "This is the title.")
    ("Subject" . "Everything about this.")
    ("Category" . "A bunch of bits.")
    ("Pages" . "")
    ("Comments" . "Some unrelated info...")
    ("Copyright" . "")
    ("Artist" . "")
    ("Album Title" . "")
    ("Year" . "")
    ("Track Number" . "")
    ("Genre" . "")
    ("Duration" . "")
    ("Bit Rate" . "")
    ("Protected" . "")
    ("Camera Model" . "")
    ("Date Picture Taken" . "")
    ("Dimensions" . "")
    ("Episode Name" . "")
    ("Program Description" . "")
    ("Audio sample size" . "")
    ("Audio sample rate" . "")
    ("Channels" . "")
)
Bricscad v12

Lee Mac

  • Seagull
  • Posts: 12129
  • London, England
Re: Get_File_Properties
« Reply #8 on: April 27, 2011, 08:12:16 AM »
I think I might be inclined to change:

Code: [Select]
(vl-remove-if
    (function (lambda (item) (eq "" (car item))))
    result
)

to:

Code: [Select]
(vl-remove-if
    (function (lambda ( item ) (or (eq "" (cdr item)) (eq "" (car item)))))
    result
)

 :-)

MP

  • Seagull
  • Posts: 17258
Re: Get_File_Properties
« Reply #9 on: April 27, 2011, 08:17:48 AM »
I think I might be inclined to change: ...

I understand the rationale but to me a field value of "" is legit, whereas a field name of "" is not. But that's merely my mileage ... thanks for your thoughts Lee. :)

Tested successfully with Bricsad v11 on WinXP Pro SP3.

Awesome, thanks for testing under Bricscad. :)
\|// Set goal. Experiment tirelessly until
|Oo| practice has become expertise.  Loop.
|- | LinkedIn | Dropbox | About

Lee Mac

  • Seagull
  • Posts: 12129
  • London, England
Re: Get_File_Properties
« Reply #10 on: April 27, 2011, 08:23:35 AM »
I think I might be inclined to change: ...

I understand the rationale but to me a field value of "" is legit, whereas a field name of "" is not. But that's merely my mileage ... thanks for your thoughts Lee. :)

Good point. It would return results of consistent length - I'm converted.  :-)

Lee Mac

  • Seagull
  • Posts: 12129
  • London, England
Re: Get_File_Properties
« Reply #11 on: April 27, 2011, 08:28:11 AM »
By the way, I was meaning to ask - how did you ascertain there were 35 properties to be had? Looking over the reference for the 'GetDetailsOf' method for the Folder Object I notice there are only 5 or 6 properties listed.

MP

  • Seagull
  • Posts: 17258
Re: Get_File_Properties
« Reply #12 on: April 27, 2011, 08:31:34 AM »
To write the function I had to use several sources, none of which on their own yielded code that when converted to LISP worked. This one listed the properties.
\|// Set goal. Experiment tirelessly until
|Oo| practice has become expertise.  Loop.
|- | LinkedIn | Dropbox | About

Lee Mac

  • Seagull
  • Posts: 12129
  • London, England
Re: Get_File_Properties
« Reply #13 on: April 27, 2011, 08:36:19 AM »
Thanks for the link MP, appreciated.

MP

  • Seagull
  • Posts: 17258
Re: Get_File_Properties
« Reply #14 on: April 27, 2011, 08:39:48 AM »
HA!!

Turns out some experimentation reveals that list is incomplete. See revision to first post. :)
\|// Set goal. Experiment tirelessly until
|Oo| practice has become expertise.  Loop.
|- | LinkedIn | Dropbox | About