0

I am able to search files that contain the term plus in a directory using the following command:

dir "*plus*" /s

However, I need to modify the command to search for files whose names contain the terms plus OR vest. In Windows Explorer, I am able to do this by entering search terms joined by OR like this - plus OR vest. How to accomplish this using windows command line?

2 Answers 2

1

dir receives a list of patterns so you can just use multiple wildcard patterns

dir /s *plus* *vest*

You may want to use dir /b /s *plus* *vest* instead for saner output


Or you can also filter the result with findstr

dir /b /s | findstr /i "plus vest"

findstr will find words separated by spaces by default and will return lines that match plus or vest. /i is used to match case-insensitively, you can remove it if you don't want. But this method will be much slower because all the file paths must be printed out to the pipe

Note that this will also find all files in folders named *plus* or *vest*. You can remove /b but the output of dir /b /s | findstr /i "plus vest" will very confusing because you don't know which folder a file is in


To find (plus OR vest) but NOT hoodie use

dir /b /s *plus* *vest* | findstr /i /v hoodie

But that will also exclude files in folders named *hoodie*. Again removing /b helps but the output would be likely unhelpful. You should use PowerShell instead for such complex things. In fact I avoid writing anything new in cmd, PowerShell is significantly better, you don't have to learn the legacy quirks of cmd. In PowerShell that would be done as

Get-ChildItem -Recurse -Include "*plus*", "*vest*" | Where-Object { $_.Name -notlike "*hoodie*" }

or the shorter aliased version:

ls -R -I "*plus*", "*vest*" | where { $_.Name -notlike "*hoodie*" }
2
  • Thanks, this works for OR condition. How to search for filenames that contain plus OR vest but NOT hoodie?
    – dc09
    Commented Nov 6, 2022 at 11:20
  • @dc09 that's out of scope of the OP. But please see the update
    – phuclv
    Commented Nov 6, 2022 at 13:19
0

With Powershell, you can use the following command:

Get-ChildItem -Path "C:\Path\To\Your\Files\*" -Include "*plus*", "*vest*"

If you are already in the path:

Get-ChildItem -Path .\* -Include "*plus*", "*vest*"
2
  • there's a missing quote in your first command. And if you're already in path then you won't need -Path, but you'll need -Recurse to recurse into subfolders like dir /s. Additionally -File may be used to filter out just files
    – phuclv
    Commented Nov 6, 2022 at 9:57
  • @phuclv Thanks for noticing about the missing quote. Commented Nov 6, 2022 at 10:12

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .