2

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?

  • 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 Jun 4 '13 at 11:32
  • So in this case, you want to rename "Test" to "Documents" or "My Documents" to "Documents"? – tumchaaditya Jun 4 '13 at 11:37
  • maybe this thread is useful for you: stackoverflow.com/questions/9271107/… – bummi Jun 4 '13 at 11:55
  • @tumchaaditya I wanted to rename My Documents to Documents. – Guest23408290 Jun 4 '13 at 13:14
6
DIR /AD /B "My Documents" /S

for listing the folders

  • I'll defer to bummi, I forgot the /D switch ;) – Jason H Jun 4 '13 at 11:57
  • 1
    @bummi Thank you! That lists exactly what I wanted. – Guest23408290 Jun 4 '13 at 13:15
  • @Karan It filters the results to directories only, leaving out files. – jpmc26 Mar 16 '15 at 23:03
-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.

  • its not exactly what he is asking in the question – Saksham Goyal Dec 17 '16 at 11:05

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.