John S.
John S.

Reputation: 514

How to dynamically create Folder in batch file - CMD

I have my source folder that contains many, many folders that are named like this:

enter image description here

What I am trying to do is essentially create new, empty folders with those exact names.

This could be done is 2 ways I think:

  1. The dates are exactly 7 days apart, so the code could create the folders dynamically, given a start date.

or

  1. Loop through the source folder, and create a new empty folder with that name, in my target folder.

I have never written a batch file. I was able to get this far, listing the directories though:

@echo

setlocal

FOR /D %%G in ("*") DO echo We found %%~nxG

endlocal

Does anyone know how to take the name found in the FOR statement, and create a folder in a specific directory?

Thanks! The syntax here sure is different!

Upvotes: 1

Views: 767

Answers (2)

lit
lit

Reputation: 16266

Another way, if you wanted to use PowerShell. Works exactly the same on Mac and Linux.

Get-ChildItem -Directory -Path './srcdir' | ForEach-Object { Copy-Item -Path $_.FullName -Destination './destdir' }

Upvotes: 1

Compo
Compo

Reputation: 38719

I would suggest an easier method involving :

RoboCopy "SourceDir" "TargetDir" /E /Lev:2 /Create /XF *>NUL

Obviously you'd replace SourceDir with the directory location holding your date named directories, and TargetDir would be the directory location where you're wanting to replicate those names.

You can use just . for the current directory if it is the source or target.


For information purposes, to do it using the method in your question, you'd simply need to use the Make Directory command, MkDir|MD:

For /D %%A In ("SourceDir\*")Do MD "TargetDir\%%~nxA"

If the current directory is either SourceDir or TargetDir, you can remove it and it's trailing backslash.

Upvotes: 1

Related Questions