I would like to add subfolders with the same Name to multiple Folders which currently have the following structure:

V:/fixed name/ABC/fixed Name/

where the "ABC" is a changing combination of three letters.

I found the following code, but it is unable to accept my "*" Wildcard for the "ABC".

FOR /d %A IN (V:/fixed name/ABC/fixed Name/) DO mkdir "%A\14c"

Any help would be appreciated.

Cheers, nickma


From the command line:

FOR /D %A IN ("V:\fixed name\*") DO mkdir "%A\fixed name\14c"

In a batch file:

FOR /D %%A IN ("V:\fixed name\*") DO mkdir "%%A\fixed name\14c"
  • Thanks for your help. Unfortunately, that doesn't fix my Problem: I have a fixed Name, then a Wildcard, then a fixed Name for the directory, in which I want to add the subfolder. Do you have any solution for that? – Nickma Oct 23 '17 at 13:40
  • I tested it on the same directory structure you detailed. For me it was "Test Folder\ABC\fixed name\14c". Are you sure this is what you have and it didn't work? – Tomer Godinger Oct 23 '17 at 15:18
  • Now I tested it with the exact same directory names you listed here and copy/pasted my answer. It created the directories just fine. You can see the results here. – Tomer Godinger Oct 23 '17 at 15:26
  • Note that the mkdir commands were not typed manually; the FOR loop created them. If you put it in a batch script and precede it with an @ sign or set ECHO to OFF then it won't show the commands it generates and runs. – Tomer Godinger Oct 23 '17 at 15:33
  • I just tried again and it actually worked out, although I don't know why it didn't work in the first place... Anyway, thanks a lot :) – Nickma Oct 23 '17 at 15:42

This will work but only ONE '*' is allowed:

@echo off
set pre=
set post=
set pattern=%~1
for /f "tokens=1,2 delims=*" %%p in ("%pattern%") do (
   set pre=%%p
   set post=%%q
pushd %pre%
for /f %%d in ('dir /b /ad') do @md %%d%post% && echo added: %%d%post%

The path before the wildcard must exist. Here, we look for all folders (= wildcard) and append the path which follows the wildcard argument. This could be a folder tree as well (mkwild C:\users\me\test\*\first\second).

  • Thanks for your help. As I am very new to this Topic, how can I define the directory in the code you provided? My directory only Needs one Wildcard, so I think your solution might work if I can get it entered ;) – Nickma Oct 23 '17 at 13:41
  • Just call this batch file with one argument. Like mkwild.cmd fixedpath\*\newfolder which will create one folder newfolder below each folder in fixedpath. Enclose the argument in "..." if it contains blanks. – user1016274 Oct 23 '17 at 14:03
  • Thanks again. I created a Batch file with the code you provided, saved it and tried to call the file with mkwild.cmd fixedpath*\newfolder. However, I Keep getting an error message back that the file does not exist... – Nickma Oct 23 '17 at 14:28
  • did you name the batch file "mkwild.cmd"? Does "fixedpath" exist? Please note that the correct argument would be "fixedpath*\newfolder", i.e. with 2 backslashes. – user1016274 Oct 23 '17 at 14:36
  • Yes, I did. Do you have any idea why it doesn't work? :/ – Nickma Oct 23 '17 at 14:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.