Is it possible to retain file Type when removing all metadata?

Hi,

I created a script to delete all existing metadata from a video file and replace it with just the title and year from the file name. The problem I have is that it seems to randomly not work due to "error setting mettadata" and im sometimes just left with two .TMP files of the video. Also, it sometimes removes the file.metadata of the type of file it is in the first place (i.e. video), instead, making it a doc file.

Here is one of the errors when I ran the script on this file (only happened a few times and not consistently):

2017-09-02_01-08-43

  1. Is the "Error setting metadata" sometimes occurring because the two SetAttr META commands (First to remove all meta and second to set them from the file name) are right next to each other? And since it takes time for the first one to resave a big file (creating a .TMP file temporarily) the second command cannot find the original file. If that is the case, what's the best way, if any, to make the second SetAttr wait for the file to resave before working on the second one?

  2. Since the "SetAttr META *" removed the identifier that a file is a video file, is there some metadata parameter I can set at the end that will say it's a video file again? Still confused why this also only happens sometimes to the same file.

Option Explicit
Function OnClick(ByRef clickData)
	
	Dim file, files, countFiles, regex0, regex1
	Set files = clickData.func.sourcetab.selected_files 
	countFiles = 0
		
	Set regex0 = new RegExp
	regex0.Pattern = "^(.*)\_\((\d{4})\).*"			'Search for Name and 4 digit year
	regex0.IgnoreCase = True
	
	Set regex1 = new RegExp
	regex1.Pattern = "_"        					'Search for underscore
	regex1.Global=True
	regex1.IgnoreCase = True	

	
	For Each file in files
		Dim fileName, fileType, metaTitle, metaYear
		fileName = file.name
		fileType = file.metadata
		metaTitle = Empty
		metaYear = Empty
		countFiles = countFiles + 1

		If (file.metadata = "video") Then
		
			If (regex0.Test(fileName)) Then
				metaTitle = regex0.Replace (fileName,"$1")        					'Set Title to the text before year in filename
					DOpus.Output countFiles & ") Full title: " & metaTitle 
				metaYear = regex0.Replace (fileName,"$2")							'Set Year to 4 digits inside parentheses
					DOpus.Output countFiles & ") Full year: " & metaYear 
				metaTitle = regex1.Replace (metaTitle," ")							'Replace all underscores in title to spaces.
					DOpus.Output countFiles & ") Title without underscores: " & metaTitle 
			
				If (clickData.func.qualifiers = "shift") Then
					clickData.func.command.ClearFiles
					clickData.func.command.AddFile file
					clickData.func.command.RunCommand "SetAttr META *"
					clickData.func.command.RunCommand "SetAttr META " & """title:" & metaTitle & """" & " " & """year:" & metaYear & """"
				End If
			
			Else
				DOpus.Output countFiles & ") " & fileName & " does not meet regex0 criteria: " & fileType
			End If
		
		Else
			DOpus.Output countFiles & ") " & fileName & " is NOT a Video File: " & fileType
		End if

   Next
End Function

Thanks!

I simplified and merged both SetAttr commands into one:

clickData.func.command.RunCommand "SetAttr META " & """title:" & metaTitle & """" & " " & """year:" & metaYear & """" & " " & """comment:"""

Nothing really changed and it still takes about a minute to set the changes on a 2Gb file since it creates a temp files and slowely writes. I found out the reason for this is because I'm updating files on a mounted network drive. When I do it on a local file that size, it's almost instance. I just didn't realize the fact that to update metadata, it has to recreate the file from scratch rather than just update something. Well the standard Windows properties viewer does not even let you set those properties in properties viewer, So I guess this is still better than nothing.

It won't always re-write the whole file, but will in some cases.

(It can depend on the file type, the type and layout of metadata already in the file, what's being changed, and also how different libraries or components (both OS and third party) decide to proceed with the change. Maybe the type of drive can be a factor as well, since it can be up to Windows or installed components which we don't have the details of, for some types.)

Haven't had a chance to look properly at the first post yet, but it's on the list.