# How to find folders of a certain name



## Alex Ethridge

I'm posting this in the DOS forum because I prefer a DOS method.

How do I find folders by a certain name? Example: I want to find folders (folders only) named DE, IT, TW, etc without finding folders that contain other characters before and/or after those characters.

Is there a command-line way of doing this?


----------



## devil_himself

where do you want to find the folders ? on the whole hard drive or on a particular drive in a particular folder

ex -- lets assume there are two folders named "ADE" , DE and "DEA" in a folder.

to find folder with name DE and not DEA or ADE use the command

*dir | findstr "\<DE\>"*

to find folders with name DE and DE*

*dir | findstr "\<DE.*"*


----------



## Squashman

Would this be an better option?

dir /ad /b /s | findstr "\<DE\>"


----------



## devil_himself

Squashman said:


> Would this be an better option?
> 
> dir /ad /b /s | findstr "\<DE\>"


yea .. if you are looking for folders only.


----------



## Alex Ethridge

Thanks for the replies; but, I ran this:

dir /ad /b /s | findstr "\<1\>"

an it returned everything that contained the digit "1" regardless of whether it was the entire folder name or just any part of the name or any part of the path.

I ran this:

dir /s | findstr "\<1\>"

and it returned everything containing a "1" regardless of whether it was the entire folder name, part of a folder name, part of the file size or part of the file's date and time stamp.


----------



## devil_himself

try this

will only return if exactly matches the folder name , no path
*dir /b /ad | findstr "\<1\>"*


----------



## Alex Ethridge

I give up; I don't think we're getting anywhere. I ran this command:

dir /b /s /ad | findstr "\<1\>"

it returned the following:

C:\S\1
C:\S\abcd1xyz
C:\S\abcd1xyz2
C:\S\1\11
C:\S\1\21
C:\S\1\11\1
C:\S\1\11\123
C:\S\abcd1xyz\21

As you can see, it returned all folders containing a "1" in its name, regardless.

Thanks for your tries.


----------



## devil_himself

Code:


C:\S>dir /ad /b /s
C:\S\1
C:\S\abcd1xyz
C:\S\abcd1xyz2
C:\S\1\11
C:\S\1\21
C:\S\1\11\1
C:\S\1\11\123
C:\S\abcd1xyz\21

C:\S>dir /b /s /ad | findstr "\<1\>"
C:\S\1
C:\S\1\11
C:\S\1\21
C:\S\1\11\1
C:\S\1\11\123

C:\S>dir /b /ad | findstr "\<1\>"
1

C:\S>

using the /s switch will pipe the whole path name to the findstr command that is why you get so many results


----------



## Alex Ethridge

It does me no good to search only the current folder. I need to find ALL folders, regardless of level.

Also, if you will note this line:
C:\S\abcd1xyz\21
has no folder named "1", yet is is listed as one of the finds.

Just let it go. It obviously cannot be done.

Thanks for trying.


----------



## Squashman

Alex Ethridge said:


> It does me no good to search only the current folder. I need to find ALL folders, regardless of level.
> 
> Also, if you will note this line:
> C:\S\abcd1xyz\21
> has no folder named "1", yet is is listed as one of the finds.
> 
> Just let it go. It obviously cannot be done.
> 
> Thanks for trying.


I think you have gotten enough help here to know that nothing is impossible.

This Vbscript kind of works. I found this on the web but can't figure out how to make it output to a text file. I don't know VB at all. I also don't understand why it finds directories named "1.0".


Code:


Set objRegEx = CreateObject("VBScript.RegExp")

objRegEx.Global = True   
objRegEx.IgnoreCase = True
objRegEx.Pattern = "^1$"

strComputer = "."

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

Set colFolders = objWMIService.ExecQuery("Select * From Win32_Directory")

For Each objFolder in colFolders
    strFolder = objFolder.FileName
    Set colMatches = objRegEx.Execute(strFolder)

    If colMatches.Count > 0 Then
        Wscript.Echo objFolder.Name
    End If
Next


----------



## Alex Ethridge

VB Script is a bit over my head and it isn't profitable for me to learn it as I would have so little use for it.

This isn't really that important a job that we need to spend any more time on it. I just thought there might be some simpler solution in a DOS (character-based) environment; but, there doesn't seem to be.

Again, thanks, but I feel this thing has become an imposition, seeing it isn't that important.


----------



## Squashman

Alex Ethridge said:


> VB Script is a bit over my head and it isn't profitable for me to learn it as I would have so little use for it


Yeah, I totally forgot about that. You had said that to me many months ago that learning something new really wasn't worth your time. But how hard would it be for you to post that code in the Development forum and just ask someone to fix the code so that it will output to a file.


----------



## devil_himself

edited vbscript to write to a text file


Code:


Set objRegEx = CreateObject("VBScript.RegExp")
Set objFSO=CreateObject("Scripting.FileSystemObject")
Set objFile=objFSO.CreateTextFile("C:\Log.txt")

objRegEx.Global = True   
objRegEx.IgnoreCase = True
objRegEx.Pattern = "^1$"

strComputer = "."

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

Set colFolders = objWMIService.ExecQuery("Select * From Win32_Directory")

For Each objFolder in colFolders
    strFolder = objFolder.FileName
    Set colMatches = objRegEx.Execute(strFolder)

    If colMatches.Count > 0 Then
        objFile.WriteLine objFolder.Name
    End If
Next


----------



## devil_himself

and here is a batch code


Code:


@echo off
setlocal
set str=1
for /f "delims=" %%a in ('dir /b /s /ad') do if "%%~na" == "%str%" echo "%%a"


----------



## Squashman

Why didn't I think of that. Good use of the Modifiers. Good Job devil !!!!


----------



## devil_himself

Thanks


----------

