# Scripts (JS & VBS) Snippet: Enumerating files and metadata tags

**URL:** <https://resource.dopus.com/t/scripts-js-vbs-snippet-enumerating-files-and-metadata-tags/18439>\
**Category:** Opus FAQs\
**Created:** [May 26, 2014, 9:00pm UTC](https://resource.dopus.com/t/scripts-js-vbs-snippet-enumerating-files-and-metadata-tags/18439 "2014-05-26T21:00:00Z")\
**Posts on this page:** 4\
**Page:** 2

<div class="post-metadata">

**Author:** ![akpuggy](https://resource.dopus.com/letter_avatar_proxy/v4/letter/a/94ad74/32.png) [@akpuggy](https://resource.dopus.com/u/akpuggy)\
**Post date:** [November 13, 2016, 6:58pm UTC](https://resource.dopus.com/t/scripts-js-vbs-snippet-enumerating-files-and-metadata-tags/18439/21 "2016-11-13T18:58:42Z")

</div>

Awesome, thank you!

---

<div class="post-metadata">

**Author:** ![kundal](https://resource.dopus.com/user_avatar/resource.dopus.com/kundal/32/24277_2.png) [@kundal](https://resource.dopus.com/u/kundal)\
**Post date:** [November 13, 2016, 9:51pm UTC](https://resource.dopus.com/t/scripts-js-vbs-snippet-enumerating-files-and-metadata-tags/18439/22 "2016-11-13T21:51:51Z")

</div>

This one also looks for "test" (possibly followed or preceded by any other characters like "dotest" or "testing") followed by exactly 10 digits:

```vbscript
Option Explicit
Function OnClick(ByRef clickData)
	Dim countFiles, countFoundTags, countTags, file, files, reg, strReg, tag
	Set files = clickData.func.sourcetab.selected_files
	Set reg = new RegExp
	reg.Pattern = "^(.*test\D*\d{10})$"
	reg.IgnoreCase = True

	For Each file in files
		countFiles = countFiles +1
		countTags = 0
		countFoundTags = 0
		DOpus.Output "File " & countFiles & ": " & file.name

		For Each tag In file.metadata.tags
			countTags = countTags + 1
			If InStr(Lcase(tag),"test") <> 0 Then
				countFoundTags = countFoundTags + 1
				DOpus.Output tag
			End If

			If (reg.Test(tag)) Then
				strReg = strReg & countTags & " and tag "
			End If
		Next

		If Not strReg = "" Then
			strReg = StrReverse(Replace(StrReverse(strReg)," gat dna ","",1,1))
			DOpus.Output "File " & countFiles & " contains ""test and 10 digits"" in tag " & strReg &"."
		End If        

        	DOpus.Output "All Tags: " & countTags & ", Tags with searchstring: " & countFoundTags & vbCrLf & "--------"
		strReg = ""
	Next
End Function

```

---

<div class="post-metadata">

**Author:** ![akpuggy](https://resource.dopus.com/letter_avatar_proxy/v4/letter/a/94ad74/32.png) [@akpuggy](https://resource.dopus.com/u/akpuggy)\
**Post date:** [November 14, 2016, 5:27am UTC](https://resource.dopus.com/t/scripts-js-vbs-snippet-enumerating-files-and-metadata-tags/18439/23 "2016-11-14T05:27:11Z")

</div>

I used a combination of all the input here and got something that seems to be working well ([goo.gl/Nwc3jq](https://goo.gl/Nwc3jq)).

Thanks!

BTW, here is the final version so far:

```vbscript
Option Explicit
Function OnClick(ByRef clickData)
	
	Dim file, files, timeStamp, countFiles, countUpdate, countSkip, countReview, regex0, liveStatus
	Set files = clickData.func.sourcetab.selected_files 
	timeStamp = DOpus.Create.Date(now()).Format("D#yyyyMMddT#HHmmss")
	countFiles = 0
	countUpdate = 0
	countSkip = 0 
	countReview = 0

	Set regex0 = new RegExp
	regex0.Pattern = "^(Name\()(.*)(\)\,\s)(Created\()(\d*)(\)\,\s)(Modified\()(\d*).*(\)\,\s)(Tagged\()(\d*)\)"
	regex0.IgnoreCase = True
		
	If (InStr(clickData.func.qualifiers, "shift") > 0) Then
		liveStatus = " - SHIFT held during operation: All Changes are LIVE"
	End If

	DOpus.OutputString "Number; File Name; Old Tag; New Tag; File Type; Status; Reason"
	
	For Each file in files
		Dim fileName, fileType, createdDate, modifiedDate, imgTaken, imgDigitized, tag, newTag, tagString, countAllTags, countSearchTags
		fileName = file.name
		fileType = file.metadata
		createdDate = file.create.Format("D#yyyyMMddT#HHmmss")
		modifiedDate = file.modify.Format("D#yyyyMMddT#HHmmss")
		imgTaken = Empty
		imgDigitized = Empty
		tagString = Empty
		newtag = Empty
		countFiles = countFiles + 1
		countAllTags = 0
		countSearchTags = 0

	 	If file.metadata <> "none" Then
			For Each tag In file.metadata.tags
				countAllTags = countAllTags + 1
				If tagString = Empty Then
					tagString = tag
				Else
					tagString = tagString & " | " & tag
				End If
				If (regex0.Test(tag)) Then
					countSearchTags = countSearchTags + 1
				End If
				'If InStr(LCase(tag),"name(") <> 0 Then
				'	countSearchTags = countSearchTags + 1
				'End If
			Next
		End If

		If (tagString = Empty) Then
			If file.metadata = "image" Then
				imgTaken = file.metadata.image.datetaken
				If (Not IsEmpty(imgTaken)) Then
					imgTaken = DOpus.Create.Date(imgTaken).Format("D#yyyyMMddT#HHmmss")
				End If
				imgDigitized = file.metadata.image.datedigitized
				If (Not IsEmpty(imgDigitized)) Then
					imgDigitized = DOpus.Create.Date(imgDigitized).Format("D#yyyyMMddT#HHmmss")
				End If
			 	newTag = "Name(" & fileName & "), Created(" & createdDate & "), Modified(" & modifiedDate & "), Taken(" & imgTaken & "), Digitized(" & imgDigitized & "), Tagged(" & timeStamp & ")"	
				'Image, NO Tags, Update with newTag
				DOpus.OutputString countFiles & "; " & fileName & "; " & tagString & "; " & newTag & "; " & fileType & "; Update; 1"
				countUpdate = countUpdate + 1
				If (InStr(clickData.func.qualifiers, "shift") > 0) Then
					clickData.func.command.ClearFiles
					clickData.func.command.AddFile file
					clickData.func.command.RunCommand "SetAttr META ""tags: " & newTag & """"
				End If
			Else
				newTag = "Name(" & fileName & "), Created(" & createdDate & "), Modified(" & modifiedDate & "), Tagged(" & timeStamp & ")"
				'Other, NO Tags, Update with newTag 
				DOpus.OutputString countFiles & "; " & fileName & "; " & tagString & "; " & newTag & "; " & fileType & "; Update; 2"
				countUpdate = countUpdate + 1
				If (InStr(clickData.func.qualifiers, "shift") > 0) Then
					clickData.func.command.ClearFiles
					clickData.func.command.AddFile file
					clickData.func.command.RunCommand "SetAttr META ""tags: " & newTag & """"
				End If
			End If	
		Elseif (countAllTags = 1 AND countSearchTags = 1) Then
			'Skip, Tag has already been updated with expected Tag string
			DOpus.OutputString countFiles & "; " & fileName & "; " & tagString & "; " & newTag & "; " & fileType & "; Skip; 3"
			countSkip = countSkip + 1
		Else
			'Review, Contains multiple Tags
			DOpus.OutputString countFiles & "; " & fileName & "; " & tagString & "; " & newTag & "; " & fileType & "; Review; 4"
			countReview = countReview + 1
		End If
   Next

   	DOpus.OutputString "-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------"
	DOpus.OutputString ""
	DOpus.OutputString "Total Files: (" & countFiles & ") - Update: (" & countUpdate & "), Skip: (" & countSkip & "), Review: (" & countReview & ")" & liveStatus
End Function

```

---

<div class="post-metadata">

**Author:** ![Leo](https://resource.dopus.com/user_avatar/resource.dopus.com/leo/32/69732_2.png) [@Leo](https://resource.dopus.com/u/Leo)\
**Post date:** [December 21, 2016, 3:05pm UTC](https://resource.dopus.com/t/scripts-js-vbs-snippet-enumerating-files-and-metadata-tags/18439/24 "2016-12-21T15:05:34Z")

</div>

I've updated the root post of this thread to include an example of reading and writing image metadata, which is probably more common than needing to work with the free-form "tags" field. Similar techniques work with MP3 metadata like Album Artist etc.

[Previous page](https://resource.dopus.com/t/scripts-js-vbs-snippet-enumerating-files-and-metadata-tags/18439.md?page=1)
