Level up your Box knowledge with brand new learning paths on Box University. Visit training.box.com to get started

CLI Search for exact match

New post

Comments

11 comments

  • mwiller

     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.

    0
    Comment actions Permalink
  • jnickell

    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= --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= --json

    I get similar "partial" matches.

     

     

    0
    Comment actions Permalink
  • jnickell

    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=',
    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.  

    0
    Comment actions Permalink
  • mboggs

     , 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?

    0
    Comment actions Permalink
  • jnickell

    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?

    0
    Comment actions Permalink
  • mboggs

    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"', 

     

     

    0
    Comment actions Permalink
  • jnickell

     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. 

    0
    Comment actions Permalink
  • sgarlanka

    If you want to include special characters, using an escape character should work. For example box search \"test.txt\". I tried this locally on a Mac terminal and it was working for me. For Powershell, this seemed to work box search \`"test.txt\`". I am not familiar with Powershell, but used the following resource to help me out.

    0
    Comment actions Permalink
  • jnickell

    I have a file name "Dataflow_Working.pbix" in my tenant.  I'll use this as my demonstration

    If via the webapp I submit a query for "Dataflow_Working" (note the quotes are required).  I get one result.

    Initially my searches were turning up more than one result using "Dataflow" or "Working".  But using the code below I was able to get one result:

    $searchString = "\`"Dataflow_Working\`""
    $searchResult = box search $searchString --content-types=names --json --verbose |convertfrom-json

     

    This worked like I was hoping, then I tried a "real example"

    $searchString = "\`"1234_Project_Name_A3\`""
    $searchResult = box search $searchString --content-types=names --json --verbose |convertfrom-json

    This gave me the correct result, plus others with names like "plot.log"

    Conducting the same search in the Webapp and filtering to "File and Folder names", I get 4 results, which I can make sense of their matching.

    I did a spot check on a couple of the results and some of them do not include the search term in their folder heirarchy either.

     

    0
    Comment actions Permalink
  • Pack, Eric

    I know this is an old post, but has anyone successfully been able to do an exact filename search using the CLI and Powershell?  I need to find the File ID of a file so I can use it to do a file:versions:upload to replace the file.  I am searching by filename in the folder that the file is in and it comes back with all files in the folder.  I have tried everything listed in this post with no luck.  Any ideas?

     

    0
    Comment actions Permalink
  • jnickell

    Eric,

     What's here is all that I've been able to come up with. I've added a couple of mitigations in the script, like checking if I'm getting more than one search result, but nothing that I've been able to do via the CLI/API really got me where I was happy with its behavior.

    0
    Comment actions Permalink

Please sign in to leave a comment.