Community
Vault Customization
Share your knowledge, ask questions, and explore popular Vault API, Data Standard, and VBA topics related to programming, creating add-ins, or working with the Vault API.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Find matching vaulted files using Data Standard and PowerShell

2 REPLIES 2
SOLVED
Reply
Message 1 of 3
Anonymous
1281 Views, 2 Replies

Find matching vaulted files using Data Standard and PowerShell

I am not sure I can do this with PowerShell but I figure its worth asking:

 

Currently I am reading data from my new file XAML and combining that with the Number Generator code that is in the Default.ps1 in Data Standard.  So far this works great.  Our vault is getting a import of 20k-30k files from a legacy PDM system and they want to index new drawing numbers off of the existing files being imported and not the standard numbering schemes.  So, here is my question:

 

I am reading data from a few combo boxes and combining them into a partial file name 

 

$drwName=$dsWindow.FindName("cmbsize").SelectedValue + $dsWindow.FindName("cmbbldg").SelectedValue + $dsWindow.FindName("Section").Text + $dsWindow.FindName("cmbdisc").SelectedValue + $dsWindow.FindName("cmbcategory").SelectedValue + $dsWindow.FindName("cmbsubject").SelectedValue 

 

I need to take this $drwName and search the vault for a file property that matches this partial name.  There should be multiple matches. I want to take that returned list and sort it. The last 3 digits of the existing property are numbers, so I figure I can read the last property in the list, take the last 3 digits and index it by 1.

 

I can then attach that to the $drwName and have my new complete number.

 

Can this type of search and needed function be done with PowerShell, or should I be looking into .net API for this?  Still trying to get a handle on PowerShell and the Vault functions.

 

Thanks,

 

Mike Space

2 REPLIES 2
Message 2 of 3
marco.mirandola
in reply to: Anonymous

Hi Mike, in order to find files in Vault you have to use the FindFilesBySearchCondition function. You can either do it in .Net or PowerShell. The code is amlost the same, except that in PowerShell you have to use the full qualified names of namespaces and classes, as there is no "using". On the other hand there are some simplification with PowerShell. Here is the code for searching you Vault on file name with a given string:

$propDefs = $vault.PropertyService.GetPropertyDefinitionsByEntityClassId("FILE")
$pdFileName = $propDefs | Where-Object { $_.DispName -eq "File Name" }

$srcConds = New-Object Autodesk.Connectivity.WebServices.SrchCond[] 0
$srcCond = New-Object Autodesk.Connectivity.WebServices.SrchCond
$srcCond.PropDefId = $pdFileName.Id
$srcCond.PropTyp = [Autodesk.Connectivity.WebServices.PropertySearchType]::SingleProperty
$srcCond.SrchOper = 1 #contains
$srcCond.SrchRule = [Autodesk.Connectivity.WebServices.SearchRuleType]::Must
$srcCond.SrchTxt = "test"
$srcConds += $srcCond

$srcSorts = New-Object Autodesk.Connectivity.WebServices.SrchSort[] 0
$srcSort = New-Object Autodesk.Connectivity.WebServices.SrchSort
$srcSort.PropDefId = $pdFileName.Id
$srcSort.SortAsc = $true
$srcSorts += $srcSort

$bookmark = $null
$status = $null
$files = @()
$counter = 0

$files = $vault.DocumentService.FindFilesBySearchConditions($srcConds, $srcSorts, $null, $true, $true, [ref] $bookmark, [ref] $status)
$lastFile = $files[-1]

 first thing is to collect all the property definitions and look for the property called file name. In case you are running a Vault in a different language, then of course you have to use tha according property. Then we define our search criteria. As a search operator i have used "contains" as there is no "starts with". The SrchTxt must be set to the text you like to search. In order to get the results sorted, create also the sort conditions. finally you can execute the FindfilesBySearchConditions. In case you expect lots of results, the command shall be executed several time inside a while loop. In your case, i presume you will get a reasonable small amount of hits. 

In order to access the last element of the sorted list, you can use a PowerShell trick which is [-1]

So, in $lastFile you should have what you are looking for and can then proceed. 

If you like to get the numeric part of your string, you may use a regex expression like this $num = $lastFile -replace'\D+(\d+)','$1'

 

 

If you like to know more on the search abilities of Vault, you can find descriptions in the SDK help, or take a look to this post of Doug Redmond: http://justonesandzeros.typepad.com/blog/2012/09/finding-by-search-conditions.html

 

ciao

marco

 

coolOrange
www.coolorange.com
Message 3 of 3

Hi Marco,

 

Your answer is a great example of how to use Powershell instead of resorting to the .NET API; previously I have done exactly this type of search using .NET but I have to say I am liking Powershell more and more as I delve into it's capabilities.

 

Great stuff!

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report