Start a new topic

rename files from .csv using powerVault

Hi there,


Given the advice here:


http://support.coolorange.com/support/discussions/topics/22000007882


And the recent blog post here:


https://blog.coolorange.com/2017/07/14/complete-your-vault-data/


Could the two approaches be combined to rename files to the names contained within a specific .csv file?


Thanks,


Alex.

1 Comment

hi Alex, renaming files is quite tricky. However, we took some time and made a code sample. Please use it carefully and do some testing first, as we did not test all the scenarios, so it's on your risk. However, this is how renaming would work


  

function Rename-VaultFile($currentFullPath, $newFileName, $comment)
{
	$file = Get-VaultFile -File $currentFullPath
	$vaultFile = $vault.DocumentService.GetLatestFileByMasterId($file.MasterId)
	$fileIteration = new-object Autodesk.DataManagement.Client.Framework.Vault.Currency.Entities.FileIteration($vaultConnection, $vaultFile)

	$fileAssocs = $vault.DocumentService.GetFileAssociationsByIds(@($fileIteration.EntityIterationId), "None", $false, "All", $false, $false, $true)
	$fileAssocs = $fileAssocs[0]
	$fileAssocParams = @()
	if($fileAssocs.FileAssocs -ne $null)
	{
		foreach($fileAssoc in $fileAssocs.FileAssocs)
		{
			$fileAssocParam = New-Object Autodesk.connectivity.Webservices.FileAssocParam
			$fileAssocParam.CldFileId = $fileAssoc.CldFile.Id
			$fileAssocParam.ExpectedVaultPath = $fileAssoc.ExpectedVaultPath
			$fileAssocParam.RefId = $fileAssoc.RefId
			$fileAssocParam.Source = $fileAssoc.Source
			$fileAssocParam.Typ = $fileAssoc.Typ
			$fileAssocParams += $fileAssocParam
		}
	}

	$settings = New-Object Autodesk.DataManagement.Client.Framework.Vault.Settings.AcquireFilesSettings($vaultConnection)
	$settings.OptionsRelationshipGathering.FileRelationshipSettings.IncludeChildren = $false;
	$settings.OptionsRelationshipGathering.FileRelationshipSettings.RecurseChildren = $false;
	$settings.OptionsRelationshipGathering.FileRelationshipSettings.VersionGatheringOption = "Latest";
	$acquisitionOption = "Checkout"
	$settings.AddFileToAcquire($fileIteration, "Checkout")
	$acquiredFiles = $vaultConnection.FileManager.AcquireFiles($settings)

	$newFile = $vaultConnection.FileManager.CheckinFile($fileIteration, $comment, $false, [DateTime]::Now, $fileAssocParams, $null, $true, $newFileName, $fileIteration.FileClassification, $fileIteration.IsHidden, $null)
}

 

in the code above you see how the file I first "acquired", then the associations are collected, and then the file is checked in again with the new with same associations and same BOM information.


I packed the code into a function so that it could be called like this

 

Rename-VaultFile -currentFullPath "$/Designs/Suspension/test2.iam" -newFileName "test3.iam" -comment "file renamed"

 

so, by placing the function on top of your script, and then using the Rename-VaultFile within your loop inside the CSV looping, then you should be able to achieve what you are looking for.


again, please use this code carefully and especially with Inventor double check that after the rename, the references are still OK (open the file with Inventor), and the BOM work (assign item).


hope this helps

Login or Signup to post a comment