JSculley wrote: ↑Tue Mar 04, 2025 7:08 pm
Per the API docs for IEdmSearch
9
Extends IEdmSearch8 by providing the ability to peform a search for files and folders u
sing logical operators, multi-variable conditions, and new search syntax.
Have you tried casting the object returned by
CreateUtility to IEdmSearch9?
It's a long shot, but it's possible that the Search8 interface has a partial/broken implementation of the And/Or stuff. I'm not at work so I can't check.
Thank you JSculley. I was using search8 as I thought that search9 required the new special syntax from
https://help.solidworks.com/2023/englis ... dmapi.html. Well, that doesn't seem true as I couldn't find any changes when casting to 9 instead of 8. The documentation indicates it depends on the accessor used; CreateSearch2 vs CreateSearch that triggers the change in search syntax expectations. It's in the Remarks section here and in the page of a few other preexisting members of IEdmSearch:
https://help.solidworks.com/2023/englis ... State.html
So I kept pecking away at the new syntax from most basic search to more complex. I think we've got it.
- search.FileName = "*.sldprt | *.sldasm"; does what it looks like, only file must be a Solidworks part OR assembly.
- search.set_State("!OBSOLETE"); also does exactly as expected, omits all states with the name exactly = "OBSOLETE".
- search.AddVariable2("LastActiveDate", ":!{>2024-03-05}"); this is the one that took me a while.
+ the : character is a special character that is called multi-value specifier and without it the search only returns files with old dates leaving out files with no value for that variable.
+ the ! character is obviously short for NOT but it's placement is critical for getting files that do not have a value. It must be put before the {} pattern so it negates the entire pattern. Problem I ran into, while doing one step at a time, was what do I put inside the {} as a search term? Finally it dawned on me to step back and put the inverse of "older than x date". So now it gets all the files that are NOT newer than 2024-03-05, including those without a value. That's spelled out at the bottom of the Patterns section (look for !P) here:
https://help.solidworks.com/2023/englis ... l#Patterns
My function for reference and critique.
Code: Select all
private static int GetOldFiles(ref HashSet<IEdmSearchResult5> oldFiles, DateTime minDate)
{
IEdmSearch9 search = (IEdmSearch9)vault.CreateSearch2();
try
{
string searchRootPath = Path.Combine(vault.RootFolderPath, "CAD_Data");
search.StartFolderID = vault.GetFolderFromPath(searchRootPath).ID;
search.SetToken(EdmSearchToken.Edmstok_FindFolders, false);
search.SetToken(EdmSearchToken.Edmstok_FindFiles, true);
search.SetToken(EdmSearchToken.Edmstok_FindItems, false);
search.SetToken(EdmSearchToken.Edmstok_Recursive, true);
search.SetToken(EdmSearchToken.Edmstok_AllVersions, false);
search.FileName = "*.sldprt | *.sldasm";
search.set_State("!OBSOLETE");
string dateTerm = ":!{>" + minDate.ToString(LastActiveUpdater.DATE_FORMAT) + "}";
search.AddVariable2(LastActiveUpdater.LAST_ACT_VAR_NAME, dateTerm);
//prime the loop
IEdmSearchResult5 searchResult = search.GetFirstResult();
while (searchResult != null)
{
oldFiles.Add(searchResult);
searchResult = search.GetNextResult();
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
Console.WriteLine(e.StackTrace);
throw;
}
return oldFiles.Count;
}