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/
...
Best Answer
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:
Notes:
dir /b /a:d
is evaluated once, so the list of directories is fixedfor /f
will loop through this fixed list exactly once.Example output:
Further Reading