adbdkb
adbdkb

Reputation: 2181

Searching Windows directory for a file, only 'n' levels deep from command line

In Windows 7, is it possible to search directories for a file or pattern, but go only 'n' levels deep?

I cannot install any external tools

The Dir /s command seems to go all the levels that may exist. I want to limit it to just 'n' (3 or 4 ) levels, but couldn't find an option to do so.

Upvotes: 2

Views: 5428

Answers (2)

David Bandel
David Bandel

Reputation: 252

Yes. I wrote a batch file to do this very thing:

@echo off
setlocal
set currentLevel=0
set maxLevel=%2
if not defined maxLevel set maxLevel=1

:procFolder
pushd %1 2>nul || exit /b
if %currentLevel% lss %maxLevel% (
  for /d %%F in (*) do (
    echo %%~fF
    set /a currentLevel+=1
    call :procFolder "%%F"
    set /a currentLevel-=1
  )
)
popd

This will list directories arbitrarily deep.

Usage: list.bat "search string" [number]

This will search for the string [number] levels deep. (This includes the current level, so 2 would search current directory and one level deeper)

Upvotes: 2

PMF
PMF

Reputation: 17288

Unfortunatelly not. The default command line options do not include this option.

Upvotes: 0

Related Questions