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?
-
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.
-
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.
-
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. -
, 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?
-
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?
-
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.
-
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-jsonThis 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.
-
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?
サインインしてコメントを残してください。
コメント
11件のコメント