JScript: How to get each tag for a file?

Below is small test script to print out all files on D: with the tags. Each filename is output OK but no tags are output. What am I doing wrong ?

var items = DOpus.FSUtil.ReadDir("D:\\", false); while (!items.complete) { var item = items.Next; if (!item.is_dir) { DOpus.Output(item); var tag; for (tag in item.metadata.tags) { DOpus.Output(tag); } } } DOpus.Output("Done");

1 Like

Just read the manual about Opus returning a DOpus.Vector object for collections, and obviously JScript cannot iterate over the elements. After changing it to following, it works:

DOpus.ClearOutput; var items = DOpus.FSUtil.ReadDir("D:\\", false); while (!items.complete) { var item = items.Next; if (!item.is_dir) { DOpus.Output(item); // There may not be any tags, so check if tag collection exists before enumerating var tags = item.metadata.tags; if (tags) { var numTags = tags.count; var i = 0; while (numTags--) { DOpus.Output(tags(i++)); } } } } DOpus.Output("Done");

I tried using if (!tags.empty) and if (tags.empty == false) instead of tags.count to check if there were any tags, but I couldn't get it to work. How do you use tags.empty ?

Also, I was expecting that if a valid metadata object was returned for a file, there would be a valid tags object, but this was not the case for one of my test files. So, the following code fragment produced an error when retrieving the number of tags for the file in question:

var metadata = item.metadata; if (metadata) { var numTags = metadata.tags.count;

Is this to be expected ?

My JScript knowledge is suspect, at best, so I adapted your code into VBScript. The following works for me.

[code]@language VBScript

Dim items, item, i, meta

Set items = DOpus.FSUtil.ReadDir("C:\Test", False)

On Error Resume Next

Do While Not (items.complete)
Set item = items.Next
If Not (item.is_dir) Then
Set meta = item.metadata
If meta.tags.count > 0 Then
DOpus.Output "Item = " & item & " tags.count = " & meta.tags.count
For i=1 To meta.tags.count
DOpus.Output "tag(" & i & ") " & meta.tags(i-1)
Next
End If
End If
Loop

DOpus.Output "Done"
[/code]
Regards, AB

Thanks aussieboykie. My JScript and VBScripting are both suspect, but I know C++ hence my initial preference for JScript. :slight_smile:

Your VBScript sample works fine, including on the file that caused a problem with the JScript version.

I don't know enough about JScript to know whether there is a bug in DOpus, a bug in WSH, or if it's supposed to behave the way I described ?

Maybe it's time to use VBScript, although I'd like to know either way.

Note the inclusion of an "On Error Resume Next" statement to deal with files that throw an error when attempting to interrogate metadata. See this post for a report regarding .osp files. Other file types may also be affected. My testing was not extensive. :slight_smile:

Regards, AB

I missed tha On Error Resume Next bit, and when I remove it I get the same error on a .7z file.

This seems like a bug in Opus as it should support tags for every file type.

Collections* and DOpus.Vectors are different things.

(* In the scripting context. File Collections in Opus itself are yet another thing, of course.)

The docs say metadata.tags gives you a collection:string ("collection of strings"), so DOpus.Vector is not relevant here.
Collections don't have empty or count methods, at least not in JScript. (How collections work is up to the scripting language, so those methods might exist in other languages.)

I've posted Snippet: Enumerating files and metadata tags with examples of how to enumerate the tags in both JScript and VBScript.

Thanks for the sample code Leo - cleared up my misconceptions about how it works.

I'm curious as to why Opus could not access the tags for the .osp file and .7z file ?

Metadata not being returned for archives was a bug which we've just fixed for the next update.