7

I'm trying to write a tagging system in Powershell. I would like to be able to set a 'tag' of my choice in a file for example by using the 'title' field as I don't use it. The files I'd like to tag are a mixture of photos/videos and text.

I've found it quite easy to read/get extended file attributes as there are multiple examples available on the net, however I'm finding it extremely difficult to find examples of modifying or writing extended file properties; especially in Powershell.

I have managed to write 'titles' to files using taglib-sharp but this seems to have problems with various photos and there is not much info available for troubleshooting. Plus it only deals with audio/video/photo files.

This is the code I have to read the title (from a test photo):

$path = 'C:\temp\photo.jpg'
$shell = New-Object -COMObject Shell.Application
$folder = Split-Path $path
$file = Split-Path $path -Leaf
$shellfolder = $shell.Namespace($folder)
$shellfile = $shellfolder.ParseName($file)
$shellfolder.GetDetailsOf($shellfile, 21)

Can anyone tell me how I can I update this attribute and then write/save it to the file?

Charles
  • 50,943
  • 13
  • 104
  • 142
Tristan P
  • 91
  • 1
  • 1
  • 3
  • Setting the extended file properties in .NET seems to covered in [this question](http://stackoverflow.com/questions/5337683/how-to-set-extended-file-properties). – zdan May 20 '13 at 01:00
  • Hi - yes, I've already seen that question. The dso dll only seems to refer to MS Office documents, whereas I want to set the title on many different file types. – Tristan P May 20 '13 at 06:22

1 Answers1

2

These depend on the file formats. Windows Explorer is only able to show these extended file properties because plugins let it know how to get the information. Not all file formats support this. For example simple txt files don't have a title. I know of no single API to set the extended file properties, so you need to find a library for each file format you want to support. For example TagLib-Sharp for photos, videos and audio and iTextSharp for PDF. But even then you wan't be able to set the title for all formats, since some don't support it.

You better look for another way to tag your files. Maybe NTFS Alternate Data Streams might help you.

Community
  • 1
  • 1
Lars Truijens
  • 42,837
  • 6
  • 126
  • 143
  • Thanks for the info Lars. The files are mostly photos and videos with a few pdf's of text. What libraries do I need for these? Also I am at a very basic level of coding in Powershell so don't know how I can translate the .Net example of dsofill.dll into Powershell to check it works with these types of files. Can you help translating it? Also the files sit on a NAS so no NTFS. – Tristan P May 20 '13 at 13:45
  • I've updated my answer with suggestions for photo, video and pdf files. Text files do not have any meta data, so nothing for that. May I suggest you first try these libraries out first and post new and seperate questions if you have any? – Lars Truijens May 20 '13 at 14:48
  • Hi Lars - thanks, as I said in my initial post I tried with taglib-sharp but it doesn't seem to work with certain photos. It gives me this error `Exception calling "Save" with "0" argument(s): "File not writeable. Corrupt metadata?" At C:\Write_tag_taglib-sharp.ps1:8 char:1 + $media.save() + ~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : InvalidOperationException`---The metadata appears fine and I can edit it manually in Windows. – Tristan P May 20 '13 at 16:22
  • I'm using TagLib-Sharp 2.1.0.0, and this is the code ` [Reflection.Assembly]::LoadFrom( ("c:\taglib-sharp.dll") ) $media = [TagLib.File]::Create("C:\temp\photo.jpg") $media.ImageTag.Title = "Test Title" $media.save() ` – Tristan P May 20 '13 at 16:38
  • Link to new question about TagLib issue [link]http://stackoverflow.com/questions/16654469/powershell-set-title-in-photo-file-using-taglib-sharp-not-working – Tristan P May 20 '13 at 16:59
  • Great. Please consider accepting my answer as the correct answer to this question. – Lars Truijens May 20 '13 at 18:35
  • Hi Lars - as I said above, I am very new to Powershell. Please can you provide an example of how to use these libraries in Powershell to change the title for a Word document? Thanks – Tristan P May 21 '13 at 11:57