Loading...
 
JoiWiki » Developer » Using Windows » Windows Scripting » Windows Script Snippets » Making Folders with Batch Script Making Folders with Batch Script

Making Folders with Batch Script

Making Folders with Batch Script

Batch files can be great for running small sections of code against the operating system and this is a lovely little tip on how to make a reasonably complex folder structure without all of the usual clicking around all over the place and should save considerable time if you’re constantly creating the same thing in lots of places.

All we’ll be using is the command “MD” which is an alias for the reliable “MKDir”.

As this is simply using the standard MKDir all we need to give it is a path to create and it will run off and do what we’d like, also creating any folders that are requisite on the way! So for example if we’d like to create the path below on a fresh USB drive (which we’ll call F:\):

F:\Projects\MyFirstProject\Documentation

All well need to put into the batch file is:

MD "F:\Projects\MyFirstProject\Documentation"

Once the file has been saved on running the command the following will be created:

F:\Projects\
F:\Projects\MyFirstProject\
F:\Projects\MyFirstProject\Documentation

Brilliant right?! One potential improvement to this would be to have the path relative to the batch file as this will mean you can create one batch file which contains your desired directory structure, copy/paste it into the directory you’d like to create in and run it!

 

Relative Paths:

There are two ways to use relative paths in batch files, the first is this: “%~dp0” and the second it this: “.” (with “..” pointing to the parent directory). From my reading I’ve found that there are differences in the use of these two but that they are generally relevant more when you’re calling batch files from a console where you can have the directory prompt be different to the directory that the batch file you’re calling is in. this is because the latter actually calls ‘Current Directory’ rather than giving an absolute path to the batch – you can see how this can get confused very quickly, here’s the kind of thing that I mean:

c:> f:\flash-files\some.bat

and if you’re interested to read more then here are a few resources:
http://stackoverflow.com/questions/5034076/what-does-dp0-mean-and-how-does-it-work

The Final Batch File

The above consideration, whilst worth knowing, is not going to bite here as I’m not planning on calling my batch file from a command prompt and as such can use “.” Which is far easier to remember. This brings the full code that I’ve put into my batch file as:

MD ".\ProjectName\Documentation"
MD ".\ProjectName\Components"
MD ".\ProjectName\Releases\Live"
MD ".\ProjectName\Releases\Testing"

Now if I want to replicate this structure anywhere I can copy/paste my batch file to the target destination, run it and then delete it and over time this will save precious keystrokes!

Created by JBaker. Last Modification: Thursday February 21, 2019 19:30:16 GMT by JBaker.

Developer