Search code examples
pythonfiledllpropertiessolidworks

Obtaining All Properties of a File in Python


I'm trying to grab a file property (like "Name, Date modified", "Type", "Size", etc...) from a file in python. The property is called "SW Last saved with" (click for example). This property tells you what version of Solidworks a model was saved with.

After some research, it appears that this "SW last saved with" property was added to Windows Explorer via registering a .DLL file (sldwinshellextu.dll).

Is there a way to grab this specific file property using some python function like (file.getProperty("SW last saved with"))?


Solution

  • So I figured out a way to do this with the help of another post I found:

    import subprocess
    newCOMObjectTxt = ("$path = 'PATH_TO_SLDPRT_FILE';"
             "$shell = New-Object -COMObject Shell.Application;"
             "$folder = Split-Path $path;"
             "$file = Split-Path $path -Leaf;"
             "$shellfolder= $shell.Namespace($folder);"
             "$shellfile = $shellfolder.ParseName($file);")
     swLastSavedWithIdx = None
     swFindLastSavedWithProp = subprocess.Popen (["powershell.exe", newCOMObjectTxt + \
            "0..500 | Foreach-Object { '{0} = {1}' -f $_, $shellfolder.GetDetailsOf($null, $_)}"],
            stdout = subprocess.PIPE)
      while True:
         line = swFindLastSavedWithProp.stdout.readline()
         if b"SW Last saved with" in line:
            swLastSavedWithIdx = int(line.split()[0])
            break
         if not line:
            break
      swLastSaveWithVersion = subprocess.Popen (["powershell.exe", newCOMObjectTxt + \
            "$shellfolder.GetDetailsOf($shellfile, %i)" %swLastSavedWithIdx], stdout = subprocess.PIPE)
      ver = str(swLastSaveWithVersion.stdout.readline(),'utf-8').strip()
    

    Basically I found that you can get all file properties via a few Windows Powershell commands. We write a few quick commands in powershell using subprocess.Popen(), then PIPE from STDOUT.