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 fixedfor /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.