The example below demonstrates how to update existing metadata properties using a combination of criteria. Please note that the updateProperties method checks the type of all properties before applying any changes. If a property satisfies the predicate but has a type different from the passed value it won’t be updated. The explicit type check in the example is performed since we use the existing value to filter metadata properties.
publicclassUpdatingMetadata{publicstaticvoidrun(){DatethreeDaysAgo=newDate(System.currentTimeMillis()-TimeUnit.DAYS.toMillis(3));Filefolder=newFile(Constants.InputPath);for(Filefile:folder.listFiles()){try(Metadatametadata=newMetadata(file.getAbsolutePath())){if(metadata.getFileFormat()!=FileFormat.Unknown&&!metadata.getDocumentInfo().isEncrypted()){System.out.println();System.out.println(file.getName());// Update the file creation date/time if the existing value is older than 3 daysintaffected=metadata.updateProperties(newContainsTagSpecification(Tags.getTime().getCreated()).and(newOfTypeSpecification(MetadataPropertyType.DateTime)).and(newUpdatingMetadata().newDateBeforeSpecification(threeDaysAgo)),newPropertyValue(newDate()));System.out.println(String.format("Affected properties: %s",affected));metadata.save(Constants.OutputPath+"output."+FilenameUtils.getExtension(file.getName()));}}}}// Define your own specifications to filter metadata propertiespublicclassDateBeforeSpecificationextendsSpecification{publicDateBeforeSpecification(Datedate){setValue(date);}publicfinalDategetValue(){returnauto_Value;}privatevoidsetValue(Datevalue){auto_Value=value;}privateDateauto_Value;publicbooleanisSatisfiedBy(MetadataPropertycandidate){Datedate=candidate.getValue().toClass(Date.class);if(date!=null){returndate.before(getValue());}returnfalse;}}}
More resources
GitHub examples
You may easily run the code above and see the feature in action in our GitHub examples: