windowscmddir

Running dir against multiple directories


If I run, for example:

dir /s /b /o:gn "c:\Program Files\TrueCrypt" | findstr .sys

I comfortably get

c:\Program Files\TrueCrypt\truecrypt.sys
c:\Program Files\TrueCrypt\truecrypt-x64.sys

..in return. But if I add a folder that doesn't exist (I won't go into the reasons why this would be the case, but it will be), I just get an error:

dir /s /b /o:gn "c:\Program Files\TrueCrypt" "c:\non folder\non subfolder" | findstr .sys
The system cannot find the file specified.

Unlike in the UNIX world where I can use "find" and it returns files found in any directories without only barfing on the directories that don't exist.


Solution

  • You can use a loop and call dir for each of the folders:

    for %F in ("c:\Program Files\TrueCrypt" "c:\non folder\non subfolder") do (
      dir /s /b /o:gn "%F"
    ) | findstr .sys
    

    However, depending on what you need exactly you can probably do better by ditching dir and findstr here and opt for a loop over the files. But that depends a bit on what your overall goal is.