This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Replacing files within MFD, on drag & drop action

Hi 
I have an existing event handler on BeforeCheckInChanges which attempts to control the files being dragged onto Multi File Documents. (we want all the files within MFD to be named the same, no duplicates of same filetypes)

' BeforeCheckInChanges > H.EventHandler.MultiFileControl
' When uploading Files onto Documents, the names must match or be rejected

' Test the current object is a 'Document' Object Type = 0	
If ObjVer.Type = 0 Then	

	' Test the current object is a 'Document' Class
	Dim oPropVal : Set oPropVal = CreateObject("MFilesApi.PropertyValues")
	Set oPropVal = Vault.ObjectPropertyOperations.GetProperties(ObjVer)
    Dim iClassId
    iClassId = Vault.ClassOperations.GetObjectClassIDByAlias("H.Class.Document")
    If oPropVal.SearchForProperty(MFBuiltInPropertyDefClass).TypedValue.GetValueAsLookup.Item = iClassId Then 
	
		' Test the document is in WIP workflow state (if not, it would not be editable)
		Dim wfState : wfState = oPropVal.SearchForProperty(39).TypedValue.DisplayValue ' 39 = Workflow State
		If wfState = "WIP" Then 
	
			' Get the 'Document Name' string value from current document Object
			Dim pDocName : pDocName = Vault.PropertyDefOperations.GetPropertyDefIDByAlias("H.Property.DocumentName")
			Dim szDocName : szDocName = oPropVal.SearchForProperty(pDocName).TypedValue.DisplayValue
			
			' Get all Files within the current document Object
			dim ObjectFileCollection: set ObjectFileCollection = Vault.ObjectFileOperations.GetFiles(ObjVer)
			
			' Loop through each File within the current document Object
			For i = 1 To ObjectFileCollection.Count
			   
				' Iteratively get each individual file within the current document Object  
				dim CurrentObjectFile: set CurrentObjectFile = ObjectFileCollection.Item(i)
			
				' Check if the filetype already exists in this document. (Duplicates will be suffixed like '__(1).ext')
				If Right(CurrentObjectFile.Title,3) = "(1)" Then 
					Err.Raise MFScriptCancel, "A ." & UCase(CurrentObjectFile.Extension) & " file already exists in this document: '" & szDocName & "'"
				End If
		
				' Check if the name does not match correctly
				If szDocName <> CurrentObjectFile.Title Then 
					Err.Raise MFScriptCancel, "Your uploaded file: '" & CurrentObjectFile.Title & "." & CurrentObjectFile.Extension & "' does not match this Document Name: '" & szDocName & "'"
				End If
			
			Next ' End the For loop
		End If ' End the WIP workflow state check
	End If ' End the Class check
End If ' End the Type check

It works nicely, and gives error messages in 2x situations:
1) If someone drags a name-matching "document.pdf" onto "document" MFD, but there's already an existing "document.pdf"
2) If someone drags a non-matching "wrong.pdf" onto "document" MFD

I'd like to change functionality of 1) though, and have it replace the existing pdf with the new one.


Is it possible to do this? When a duplicate file+ext is dragged onto the MFD, it will be named "document(1).pdf" before the checkin happens

So I think we'd need to identify the new file within the object which will end with (1), then find the corresponding old file, delete that, and then rename the new one to strip the '(1)' 

There's not a lot of info here about RemoveFile 
https://www.m-files.com/api/documentation/MFilesAPI~VaultObjectFileOperations~RemoveFile.html 
but i did see this thread from 11 years ago
https://community.m-files.com/forums-1552881334/f/m-files-api/604/removing-file-and-filever-parameter

Many thanks