1

I'm trying to figure out how to make a batch file to create a folder+subfolder in an entire directory of individual folders.

Initially I was just asked to make a batch file to create a folder + subfolder if it did not exist, I assumed my boss would have this run on startup and came up with the following after reading around on SO for a while.

@echo off
@break off
@title Create Folder with Batch

setlocal EnableDelayedExpansion

if not exist "H:\minecraftedu\saves\" (
  mkdir "H:\minecraftedu\saves\"
  if "!errorlevel!" EQU "0" (
    echo Folder created successfully
  ) else (
    echo Error while creating folder
  )
) else (
  echo Folder already exists
)

pause
exit

However what was actually needed was to make a command that we could run from the server to create

\\server\home\Students\2018(every folder in here)\minecraftedu\saves

as well as

\\server\home\Students\2019(every folder in here)\minecraftedu\saves

Which I have no idea how to do, or if that is even possible with a batch file. The end goal is to have the students Minecraftedu worlds save to their server space instead of locally, which my plan was to use symlink to have it target the folder on the server for whichever user was logged in.

I'm on staff for my hardware abilities and programming kind of escapes me past incredibly basic stuff, so any help is appreciated!

1
  • 1
    Use pushd to access a network path, it creates a temporary drive letter which points to that, and it changes to the root of that drive; finally, use popd to clean up the drive letter, and to return to the previous working directory... Commented Jan 14, 2016 at 18:28

2 Answers 2

1

I ended up going with this which worked out, just dug around SO and online to come up with the solution.

pushd \\targetdirectory\
for /D %%G in (*) DO mkdir %%G\minecraftedu\saves
pause

Pause is just in there so I could read over while I screwed up to see how badly I did in fact screw up.

But it all worked out.

Sign up to request clarification or add additional context in comments.

1 Comment

Are congratulations in order? polygon.com/2016/1/19/10789532/…
0

Try this

@echo off
SET "basepath=h:\"
SET "structure=\minecraftedu\saves"
for /f "delims=" %%D in ('dir /a:d /b %basepath%') do (
  echo creating "%basepath%%%D%structure%"
  mkdir "%basepath%%%D%structure%"
)

This will create the folder structure defined in %structure% for each subfolder directly beneath %basepath%, it is not recursive.

Keep an eye on the \'s as forgetting one can create a mess!

4 Comments

Sorry about all the edits but the issues should be resolved now. This is tested and working on multiple folders regardless of where it is run from.
for /D %%G in (*) DO mkdir %%G\minecraftedu\saves I ended up doing this. Not sure if it was overly sloppy, but it ended up working out.
That works. The only functional difference from that and my answer would be that you have to run it from the folder that contains the directories you want to modify.
Yeah I actually had pushd \\targetdirectory\ after my first attempt accidentally created minecraftedu\saves in every folder in system32 :P

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.