3

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/
                ...
amallard
  • 157
  • 1
  • 1
  • 7
  • Yes, but please note that https://superuser.com is not a free script/code writing service. If you tell us what you have tried so far (include the scripts/code you are already using) and where you are stuck then we can try to help with specific problems. You should also read [How do I ask a good question?](https://superuser.com/help/how-to-ask). – DavidPostill Mar 25 '17 at 14:25
  • [Recursively Visit Directories in a Directory Tree](http://stackoverflow.com/documentation/batch-file/3695/for-loops-in-batch-files/12871/recursively-visit-directories-in-a-directory-tree#t=201703251426285361546) – DavidPostill Mar 25 '17 at 14:27
  • What did you try? show your code and explain what happened with it – yass Mar 25 '17 at 14:28
  • @DavidPostill thanks for the link, that's a great place to start! – amallard Mar 25 '17 at 14:41
  • @yass I have updated my attempt and result – amallard Mar 25 '17 at 14:41

1 Answers1

3

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.
DavidPostill
  • 153,128
  • 77
  • 353
  • 394