How to recursively create subfolder in each folder of a directory in CMD

8,798

However, this creates the folder testFolder in every newly created folder

This is because the for /r command is updating the list of files to process every time you create a new directory, so is really only useful if you want to visit a fixed list of directories.

Here is a batch file (test.cmd) that will do what you want. Place it in the Grandparent directory.

test.cmd:

@echo off
setlocal
for /f "usebackq tokens=*" %%a in (`dir /b /a:d`) do (
  rem enter the directory
  pushd %%a
  echo In Directory: %%a
  md child
  rem leave the directory
  popd
  )
endlocal

Notes:

  • dir /b /a:d is evaluated once, so the list of directories is fixed
  • for /f will loop through this fixed list exactly once.

Example output:

> test
In Directory: Documentation
In Directory: subdir
In Directory: test
In Directory: test with space
In Directory: test1

> dir /b /a:d /s child
F:\test\Documentation\child
F:\test\subdir\child
F:\test\test\child
F:\test\test with space\child
F:\test\test1\child

Further Reading

  • An A-Z Index of the Windows CMD command line - An excellent reference for all things Windows cmd line related.
  • dir - Display a list of files and subfolders.
  • for /f - Loop command against the results of another command.
  • md - Make Directory - Creates a new folder.
  • pushd - Change the current directory/folder and store the previous folder/path for use by the POPD command.
  • popd - Change directory back to the path/folder most recently stored by the PUSHD command.
Share:
8,798

Related videos on Youtube

amallard
Author by

amallard

Software Engineer at Enveil.

Updated on September 18, 2022

Comments

  • amallard
    amallard almost 2 years

    Let's say I have a directory called Grandparent
    Inside this directory are multiple other folders, Parent 1, Parent 2, etc.. My goal is to create a new folder, Child, inside each of the Parent folders.

    For example, what I have:

    Grandparent/
        Parent1/
        Parent2/
        ...
        ...
        ParentX/
    

    What I want:

    Grandparent/
        Parent1/
            Child/
        Parent2/
            Child/
        ...
        ...
        ParentX/
            Child/
    

    Is there a way to do this in CMD? (Note: I cannot download Powershell or any other convenient tool that would make my life easier, I am stuck with the default Windows Command Prompt)

    Update

    Following the links in the comments, I have tried the following:

    for /r %%a in (.) do (
      rem enter the directory
      pushd %%a
      echo In Directory:
      mkdir testFolder
      cd
      rem leave the directory
      popd
    )
    

    However, this creates the folder testFolder in every newly created folder:

    Grandparent/
        Parent1/
            Child/
                Child/
                    Child/
                        ...
        Parent2/
            Child/
                Child/
                    Child/
                        ...
        ...
        ...
        ParentX/
            Child/
                Child/
                    Child/
                        ...
        Child/
            Child/
                Child/
                    ...