Windows command prompt find specific folder name in sub folders

2

1

Say I have a folder tree such as:

C:\Users\Test\My Documents
C:\Users\Test2
C:\Users\Test3\My Documents

What command can I run to search through C:\Users to check which sub folders have a My Documents folder?

So for the above it would come back with:

C:\Users\Test\My Documents
C:\Users\Test3\My Documents

But not

C:\Users\Test2

Also, if I wanted to script renaming any sub folders that are found to be called My Documents and rename these to just Documents, how could I achieve this?

Guest23408290

Posted 2013-06-04T11:26:01.640

Reputation: 23

Wouldn't dir /b "My Documents" work? If not, you can parse it dir /B | findstr /R /C:"My\sDocuments"... I guess it would work, I don't have Windows installed here. To rename use ren. – Alex – 2013-06-04T11:32:59.557

So in this case, you want to rename "Test" to "Documents" or "My Documents" to "Documents"? – tumchaaditya – 2013-06-04T11:37:34.110

maybe this thread is useful for you: http://stackoverflow.com/questions/9271107/bat-file-for-renaming-multiple-folders

– bummi – 2013-06-04T11:55:18.463

@tumchaaditya I wanted to rename My Documents to Documents. – Guest23408290 – 2013-06-04T13:14:39.640

Answers

6

DIR /AD /B "My Documents" /S

for listing the folders

bummi

Posted 2013-06-04T11:26:01.640

Reputation: 1 569

@Karan It filters the results to directories only, leaving out files. – jpmc26 – 2015-03-16T23:03:51.833

I'll defer to bummi, I forgot the /D switch ;) – Jason H – 2013-06-04T11:57:36.823

1@bummi Thank you! That lists exactly what I wanted. – Guest23408290 – 2013-06-04T13:15:19.300

-1

Here's a vbscript that will also rename the folders for you:

Root = INPUTBOX("Please enter the root folder (all subfolders will be renamed)" & vbcrlf & "e.g. C:\TEST")
IF Root="" THEN Canceled

FindStr = INPUTBOX("Please enter the string that you want to find")
IF FindStr = "" THEN Canceled

ReplaceStr = INPUTBOX("Please enter the string that you want to replace it with")
IF ReplaceStr = "" THEN Canceled

SET objFSO= CREATEOBJECT("Scripting.FileSystemObject")

EnumFolders Root

SUB EnumFolders(BYVAL Folder)
    SET objFolder = objFSO.GetFolder(Folder)
    SET colSubfolders = objFolder.Subfolders

    FOR EACH objSubfolder in colSubfolders
        NewFolderName = (REPLACE(objSubfolder.name, findstr, replacestr))
            IF NewFolderName <> objSubFolder.Name THEN
                objSubFolder.Name = NewFolderName
            END IF
        enumfolders objSubfolder.path
    NEXT

END SUB

SUB Canceled
    wscript.echo "Script Canceled"
    wscript.quit
END SUB

Source: http://www.wisesoft.co.uk/scripts/vbscript_recursive_folder_rename.aspx

PS: I have not tested this. Please test with an empty directory structure first.

tumchaaditya

Posted 2013-06-04T11:26:01.640

Reputation: 3 624

its not exactly what he is asking in the question – Saksham Goyal – 2016-12-17T11:05:13.570