Author Topic: Get_File_Properties  (Read 22178 times)

0 Members and 1 Guest are viewing this topic.

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
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 - Auto/Visual Lisp: [Select]
  1. (defun _Get_File_Properties_Ex ( file / result shell name_space items item )
  2.  
  3.     ;;  Get a file's extended properties (Title, Subject, Category ...)
  4.     ;;  returning the result as a list of cons pairs:
  5.     ;;
  6.     ;;      (   ("Name" . "Drawing.dwg")
  7.     ;;          ("Size" . "42 KB")
  8.     ;;          ("Type" . "DWG File")
  9.     ;;          ("Date Modified" . "2011/03/21 10:57 AM")
  10.     ;;          ...
  11.     ;;      )
  12.  
  13.     (vl-catch-all-apply
  14.         (function
  15.             (lambda ( / folder_name file_name i count )    
  16.                 (setq
  17.                     folder_name (vl-filename-directory file)
  18.                     file_name   (strcat (vl-filename-base file) (vl-filename-extension file))
  19.                     shell       (vlax-create-object "Shell.Application")
  20.                     name_space  (vlax-invoke shell 'NameSpace folder_name)
  21.                     items       (vlax-invoke name_space 'Items)
  22.                     item        (vlax-invoke name_space 'ParseName file_name)
  23.                     i           (setq count 300)
  24.                 )
  25.                 (if item
  26.                     (repeat count
  27.                         (setq result
  28.                             (cons
  29.                                 (cons
  30.                                     (vlax-invoke
  31.                                         name_space
  32.                                        'GetDetailsOf
  33.                                         items
  34.                                         (setq i (1- i))
  35.                                     )
  36.                                     (vlax-invoke
  37.                                         name_space
  38.                                        'GetDetailsOf
  39.                                         item
  40.                                         i
  41.                                     )
  42.                                 )    
  43.                                 result
  44.                             )
  45.                         )
  46.                     )
  47.                 )    
  48.             )                
  49.         )        
  50.     )
  51.    
  52.     (vl-catch-all-apply 'vlax-release-object (list item))
  53.     (vl-catch-all-apply 'vlax-release-object (list items))
  54.     (vl-catch-all-apply 'vlax-release-object (list name_space))
  55.     (vl-catch-all-apply 'vlax-release-object (list shell))
  56.    
  57.     (vl-remove-if
  58.         (function (lambda (item) (eq "" (car item))))
  59.         result
  60.     )    
  61.    
  62. )

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.


EDIT (John): Removed color tags from CODE block and replaced with AutoLisp code block (for colors).
« Last Edit: April 13, 2020, 06:45:42 PM by John Kaul (Se7en) »
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

alanjt

  • Needs a day job
  • Posts: 5352
  • 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 2019 ~ Windohz 7 64bit
Dropbox

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: Get_File_Properties
« Reply #2 on: April 26, 2011, 04:44:58 PM »
Thanks for checking Alan. :)
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

ronjonp

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

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: Get_File_Properties
« Reply #4 on: April 26, 2011, 04:46:55 PM »
Thanks Ron. :)
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

Lee Mac

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

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: Get_File_Properties
« Reply #6 on: April 27, 2011, 12:02:22 AM »
Thank you Lee. :)
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

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: 12905
  • 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: 17750
  • Have thousands of dwgs to process? Contact me.
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. :)
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

Lee Mac

  • Seagull
  • Posts: 12905
  • 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: 12905
  • 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: 17750
  • Have thousands of dwgs to process? Contact me.
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.
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

Lee Mac

  • Seagull
  • Posts: 12905
  • 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: 17750
  • Have thousands of dwgs to process? Contact me.
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. :)
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst