I want, with a batch file, to be able to search and display the files in a bunch of folders called "error" located in a bunch of different places.

The error folder is always at the same folder depth but the folder names befor them is different.

dir /A-d /b C:\Temp\*random_folder_name*\*random_folder_name*\error

So, can I create a line that displays all the files in all the error folders, two folders "below", in this example "C:\Temp"? or do I have to add one dir line for every error-folder?


Windows 7 and later have power shell:

You can try:

Get-ChildItem \*\*\*

This returns all items with a depth of two subfolders. Adding * adds an additional subfolder to search in.

Source: https://stackoverflow.com/questions/13249085/limit-get-childitem-recursion-depth

| improve this answer | |

Here is a relatively simple, but inefficient solution using FINDSTR with a regular expression. I use [\\] to match \ because of an inconsistency on Vista.

@echo off
dir /b /s c:\temp\*|findstr /rix "c:[\\]temp[\\][^\\]*[\\][^\\]*[\\]error[\\][^\\]*"

Here is a more complicated, but very efficient solution using multiple FOR statements.

@echo off
for /d %%A in (c:\temp\*) do for /d %%B in ("%%A\*") do for %%C in ("%%B\error\*") do echo %%C
| improve this answer | |

You could install the GNU Win32 libraries and use grep for this:

dir /b /s *.*|grep "error\\[^\\]*$"

The suggested way to install them is with getgnuwin32.

BTW, if you just wanted to see text files it'd be like this:

dir /b /s *.txt|grep "error\\[^\\]*$"

If you would like to ensure that only folders at a certain depth are used then it's a little more complicated:

dir /b /s *.*|grep "^\([^\\]*\\\)\{7\}error\\[^\\]*$"

This will only display results where the error directory is seven directories deep (from the root directory).

| improve this answer | |
  • Are you sure? That last regex looks incorrect. Why are you escaping (, ), {, and }? – dbenham Aug 27 '14 at 2:11
  • Yes I'm sure I've actually tested all of these on my system. Trust me, those characters need escaped. It won't work without it. – krowe Aug 27 '14 at 2:55
  • Sorry, I'm used to using JavaScript regex. That is a very curious design choice for grep. Most meta characters are escaped if you want to treat them as a literal. But in grep, some literal characters are "escaped" to turn them into meta characters. That is crazy! – dbenham Aug 27 '14 at 11:21
  • That gave me an idea: gist.github.com/krowe/da2a6b021ac891f8e3be – krowe Aug 27 '14 at 12:58
  • Yes, hybrid scripting is a powerful way to extend batch capabilities. I have created a different but related JScript/batch utility called REPL.BAT. It has been incredibly useful for many problems that are difficult to solve with pure batch. – dbenham Aug 27 '14 at 16:51

Windows has built-in alternatives to grep: find and findstr.

findstr will support a regular-expression (you can get more info by typing help findstr), but you will be somewhat limited by the output of dir which doesn't seem to support relative listings ...

dir /A-d /S /B | findstr /B /R /I /X "C:\\Temp\\[^\\]*\\[^\\]*\\error\\.*"
| improve this answer | |
  • That regex will not work because .* can match any number of \ – dbenham Aug 27 '14 at 2:09
  • sorry, that was a silly mistake by me, please review my update. – robert Aug 27 '14 at 8:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.