The M-Files Community will be updated on Tuesday, April 2, 2024 at 10:00 AM EST / 2:00 PM GMT and the update is expected to last for several hours. The site will be unavailable during this time.

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

Parents
  • Well from your code it looks like you have the objectFiles already stored in your ObjectFileCollection variable,

    Since you are already looping through each object files in your collection, when you find a document with the (1) in it's name and you want to delete it's corresponding file (Same name without the (1))

    You can probably get that by just doing something like this :

    Dim fileToFind : fileToFind = Replace(CurrentObjectFile.Title,"(1)","")
    Dim extension : extension = CurrentObjectFile.Extension
    Dim fVer : Set fVer = ObjectFileCollection.GetObjectFileByNameForFileSystem(fileToFind & extension ).FileVer
    Vault.ObjectFileOperations.RemoveFile ObjVer, fVer

    This is untested code so sorry if there are any typo, but let me know if it works or throws any unexpected errors. Also you should probably add some error checks in case it doesn't find a file without the (1). For example if the user drags a file with (1) already in the title for some reason.  

  • minor typos

    line 1 should have a space: " (1)" instead of "(1)"
    line 3 should be (fileToFind & "." & extension) instead of (fileToFind & extension )

    But, it is now working and thank you for your help Smiley
    Will post full code when it's finished

  • ' Validate the File Name against Document Name
    ' Define variables & 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
    Dim iCorrectLength : iCorrectLength = 22 ' Document Naming format: 12345-12-AB-123-123456
    
    ' Get all Files within the current document Object
    Dim ObjectFileCollection : Set ObjectFileCollection = Vault.ObjectFileOperations.GetFiles(ObjVer)
    Dim iFileCount : iFileCount = ObjectFileCollection.Count
    
    ' Loop through each File within the current document Object
    For i = 1 To iFileCount
       
    	' Iteratively get each individual file within the current document Object  
    	Dim CurrentObjectFile : Set CurrentObjectFile = ObjectFileCollection.Item(i)
    	
    	Dim iTitleLength : iTitleLength = Len(CurrentObjectFile.Title)
    	Dim szFullTitle : szFullTitle = CurrentObjectFile.Title
    	Dim szLeftTrim : szLeftTrim = Left(CurrentObjectFile.Title,iCorrectLength)
    	Dim szExtension : szExtension = CurrentObjectFile.Extension
    	
    '''' Check Filename is not too short 				
    	If iTitleLength < iCorrectLength Then ' Start validation IF statement
    		Err.Raise MFScriptCancel, "Your uploaded File Name: '" & szFullTitle & "." & szExtension & "' does not match this Document's Name: '" & szDocName & "'"
    
    '''' Check if the left side trimmed length of name starts by not matching document name
    	ElseIf szLeftTrim <> szDocName Then
    		Err.Raise MFScriptCancel, "Your uploaded File Name: '" & szFullTitle & "." & szExtension & "' does not match this Document's Name: '" & szDocName & "'"
    
    '''' Check if an exact match of filename.extension already exists in this document. (Duplicates will be suffixed like 'XXX (1).ext' and therefore length of 26)
    '''' OR Check if Length is longer than normal but is valid after trimming down to length
    	ElseIf (iTitleLength = (iCorrectLength + 4) AND Right(szFullTitle,4) = " (1)") OR (iTitleLength > iCorrectLength AND szLeftTrim = szDocName) Then				
    
    		' Remove any existing version of file that match the uploaded file
    		If iFileCount > 1 Then 
    			Dim fVer : Set fVer = ObjectFileCollection.GetObjectFileByNameForFileSystem(szLeftTrim & "." & szExtension).FileVer
    			Vault.ObjectFileOperations.RemoveFile ObjVer, fVer
    		End If
    
    		' Rename the newly uploaded file to remove the excess characters
    		Dim fVerNew : Set fVerNew = ObjectFileCollection.GetObjectFileByNameForFileSystem(szFullTitle & "." & szExtension).FileVer
    		Vault.ObjectFileOperations.RenameFile ObjVer, fVerNew, szLeftTrim, szExtension, false
    
    	End If ' End validation IF statement
    
    Next ' End the For loop

Reply
  • ' Validate the File Name against Document Name
    ' Define variables & 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
    Dim iCorrectLength : iCorrectLength = 22 ' Document Naming format: 12345-12-AB-123-123456
    
    ' Get all Files within the current document Object
    Dim ObjectFileCollection : Set ObjectFileCollection = Vault.ObjectFileOperations.GetFiles(ObjVer)
    Dim iFileCount : iFileCount = ObjectFileCollection.Count
    
    ' Loop through each File within the current document Object
    For i = 1 To iFileCount
       
    	' Iteratively get each individual file within the current document Object  
    	Dim CurrentObjectFile : Set CurrentObjectFile = ObjectFileCollection.Item(i)
    	
    	Dim iTitleLength : iTitleLength = Len(CurrentObjectFile.Title)
    	Dim szFullTitle : szFullTitle = CurrentObjectFile.Title
    	Dim szLeftTrim : szLeftTrim = Left(CurrentObjectFile.Title,iCorrectLength)
    	Dim szExtension : szExtension = CurrentObjectFile.Extension
    	
    '''' Check Filename is not too short 				
    	If iTitleLength < iCorrectLength Then ' Start validation IF statement
    		Err.Raise MFScriptCancel, "Your uploaded File Name: '" & szFullTitle & "." & szExtension & "' does not match this Document's Name: '" & szDocName & "'"
    
    '''' Check if the left side trimmed length of name starts by not matching document name
    	ElseIf szLeftTrim <> szDocName Then
    		Err.Raise MFScriptCancel, "Your uploaded File Name: '" & szFullTitle & "." & szExtension & "' does not match this Document's Name: '" & szDocName & "'"
    
    '''' Check if an exact match of filename.extension already exists in this document. (Duplicates will be suffixed like 'XXX (1).ext' and therefore length of 26)
    '''' OR Check if Length is longer than normal but is valid after trimming down to length
    	ElseIf (iTitleLength = (iCorrectLength + 4) AND Right(szFullTitle,4) = " (1)") OR (iTitleLength > iCorrectLength AND szLeftTrim = szDocName) Then				
    
    		' Remove any existing version of file that match the uploaded file
    		If iFileCount > 1 Then 
    			Dim fVer : Set fVer = ObjectFileCollection.GetObjectFileByNameForFileSystem(szLeftTrim & "." & szExtension).FileVer
    			Vault.ObjectFileOperations.RemoveFile ObjVer, fVer
    		End If
    
    		' Rename the newly uploaded file to remove the excess characters
    		Dim fVerNew : Set fVerNew = ObjectFileCollection.GetObjectFileByNameForFileSystem(szFullTitle & "." & szExtension).FileVer
    		Vault.ObjectFileOperations.RenameFile ObjVer, fVerNew, szLeftTrim, szExtension, false
    
    	End If ' End validation IF statement
    
    Next ' End the For loop

Children
No Data