0

I have a lot of variables to place in this certain command, is there a way to add many variables in it without rewriting the command?

dir c:\ /s /b /a | find "my file"

for example i want to search for "my file" and 50 other things.

thanks for the answers

infused
  • 24,000
  • 13
  • 68
  • 78
Tim
  • 51
  • 1
  • 1
  • 3

3 Answers3

2

In short: no. You have to rewrite the command, unless the specific command actually does accept multiple parameters (which may be variables). In your case it doesn't so you need to rewrite it.

One option would be to use findstr instead of find. You can pass multiple search patterns:

dir c:\ /s /b /a | findstr /c:"my file" /c:"other" /c:"other 2" ...

I don't know how well that scales to about "50 other things" though, but then no such solution may. Maybe you can condense the filenames to a view using regular expressions (check findstr /?).

You could also simply do:

for /R c:\ %i in ("my file" "other" "other2") do @echo %i

Both solutions bear the option for duplicates, however. They essentially search based on a "contains" semantic. So, both "C:\foo\my file" and "C:\foo\bar\my file\something.txt" would match. But then your original solution had that issue as well.

If you can make your search patterns unique or can live with false positives, than that shouldn't be an issue. But be aware of it nevertheless.

Christian.K
  • 47,778
  • 10
  • 99
  • 143
  • 1
    +1, But see the warning about potential problems with FINDSTR in [my answer](http://stackoverflow.com/a/11526089/1012053). – dbenham Jul 17 '12 at 15:47
2

Extending Christian's suggestion to use FINDSTR instead of FIND - You can simply put all 50 search terms in a text file and reference them using the /G:"filename" option.

But there is one important caveat - There is a nasty FINDSTR bug when searching for multiple literal strings. See Why doesn't this FINDSTR example with multiple literal search strings find a match?.

As explained in the link, the work-around is to either do a case insensitive search using the /I option, or else use regular expression search terms with the /R option.

For a "complete" listing of undocumented FINDSTR features and bugs, see What are the undocumented features and limitations of the Windows FINDSTR command?.

Community
  • 1
  • 1
dbenham
  • 127,446
  • 28
  • 251
  • 390
0

You could wrap it in a for loop:

for %i in ("my file" "second file") do dir c:\ /s /b /a | find %i
Kevin Brotcke
  • 3,765
  • 26
  • 34