CLI Search for exact match

Highlighted
Occasional Contributor

CLI Search for exact match

Hello, I am using a powershell script to use the CLI to search for user folders, but I need it to search for an exact match.  Essentially what we are doing is building users a default set of folders, and this script is meant to go through our existing users, check to see if they have the folders, and then create them if they are not there.

 

The folders would look like this:

-My Files

-- Proprietary Information

-- Personal Information

-- Third Party Proprietary Information

 

Below is the line of code for the search.  $FOLDER_ID is the ID of the "My Files" folder, so the code is meant to look inside the My Files folder to see if one of those sub-folders exist:

 

$FOLDERRESP_TPPI = (box search "Third Party Proprietary Information" --ancestor-folder-ids $FOLDER_ID --as-user $USER_ID --type folder)

 

What happens is that if someone has the "Proprietary Information" folder, the search comes back with that as a response because it contained the word Information.  I need the code to only return results for a complete match of "Third Party Proprietary Information".  Is there a way to force a search to only return an exact match?

7 REPLIES 7
Highlighted
Box Employee

Re: CLI Search for exact match

@LiangJR You can search for an exact match by using quotes around your search query (e.g. "Third Party Proprietary Information"), but I'm not familiar enough with Powershell and the specifics of Windows argv processing to give an answer for how to get the quotes passed through to the CLI.  As it stands now, it looks like Powershell is interpreting the quotes as wrapping a single argument, so the CLI gets all four words as the search query argument, but does not get the quotes around them to signal that you want an exact match.

Contributor

Re: CLI Search for exact match

I know that this is an old post, but I'm facing the exact same issue (powershell & CLI & exact string match)

I don't think that the issue lies solely with Powershell's treatment of the " character, because I can reproduce the same behavior from the normal windows command line.  

 

Powershell

> $search = "`"Active Projects`""
> $search
"Active Projects"

> $searchResult = box search $search --type=folder --ancestor-folder-ids=<aParentFolderID> --json |ConvertFrom-Json

 

In Powershell I get back 66 results in our environment

 

If I run this command in the command prompt

box search "Active Projects" --type=folder --ancestor-folder-ids=<aParentFolderID> --json

I get similar "partial" matches.

 

 

Highlighted
Contributor

Re: CLI Search for exact match

After trying with the --verbose command I was able to get a result that was "more inline" with what I was expecting. For reference in Powershell, I needed to wrap my phrase as follows:

$search = "`"`"Active Projects`"`""

 

In Verbose output:

box-cli:execute Starting execution command: search argv: [
box-cli:execute '"Active Projects"',
box-cli:execute '--type=folder',
box-cli:execute '--ancestor-folder-ids=<folderID>',
box-cli:execute '--json',
box-cli:execute '--verbose'


I should be getting one result, I'm down to two.  I "think" the second result is matching off of the description of the folder, but that's not an exact match either.  

Highlighted
Contributor

Re: CLI Search for exact match

@weigh2tall , Did you ever figure this out. I cannot, for the life of me, force an "exact match" search.

 

Does anyone have any way to comb through the results, when there is more than one and find an exact match out of the results? 

 

Example:

Trying to find "aardvark_documents"

results pulls back 3 results:

aardvark_documents   (the one I want)

aardvark folder 2

documents

 

How can I pull the one I want from the results by combing through them?

Highlighted
Contributor

Re: CLI Search for exact match

I got close enough for our needs.  This is powershell, not sure how you're calling it.  I corrected the data around the other result that was messing with me. 

 

$searchResult = box search "`"`"$DirectoryName`"`"" --type=folder --ancestor-folder-ids=$ParentFolderID  --content-types=name --json |ConvertFrom-Json

 

 I think the underscore element is going to "break" it regardless, just the way that the search function is written within Box.

I was trying to rerun the same example to verify and ran across this bonus "feature" as well.

 

$searchphrase = 'String_With_Underscores'
box search "`"`"$searchphrase`"`"" --json --verbose
...
box init version: @oclif/command@1.5.5 argv: [ 'search', 'String_With_Underscores', '--json', '--verbose' ]

 

however if I run this

 

$searchphrase = 'Active Projects'
box search "`"`"$searchphrase`"`"" --json --verbose
....
box init version: @oclif/command@1.5.5 argv: [ 'search', '"Active Projects"', '--json', '--verbose' ]

 

Note that the search parameter shows up differently for search term "_" vs a " ".  That might be what's causing your roadblock.

 

I think I wound up implementing logic that aborted or warned of "more than one match", but that was sufficient for our use case. Maybe you could report this as a bug?

Highlighted
Contributor

Re: CLI Search for exact match

Interestingly, it's not the underscore. It's any term with no spaces. Switch your example from "Active Projects" to "ActiveProjects" and you'll see the same behavior, i.e. 

 

 

[ 'search', 'ActiveProjects',   

 

 

vs

 

 

[ 'search', '"Active Projects"', 

 

@weigh2tall 

Highlighted
Contributor

Re: CLI Search for exact match

@mbo Sorry I didn't see you response, but came back to this after the "no spaces" feature bit me again in our current environ.  I don't have an answer, but I can confirm it does seem like the CLI API is what is "mishandling" the search string.