Creating and deleting directories

Creating and deleting directories
Will show you the basics of creating several directories named 1, 2, 3... or using a prefix like dir1, dir2, dir3. Also shows you how to delete an empty dir.

The most importand commands that we use in this tutorial are ‘MkDir’ and ‘RmDir’, that is ‘make a directory’ and ‘remove a directory’.
For enhancing the program, we use a while loop to create several directories following a pattern.

First, download the code from here.

You probably already know the Visual Basic loops and other elementary things about Visual Basic, so I’m not going to insist on analyzing the small code.

The main action takes place when we click the cmdMakeDir button:

Dim i As Integer
i = 1
While i <= txtNum.Text
Dim dir As String
dir = txtPath.Text & i
MkDir dir
i = i + 1
Wend



Dim i As Integer
i = 1

We create a variable i that will be used for looping.

While i <= txtNum.Text

We test to see if i is smaller than number typed in the txtNum that shows us how many directories we want to create.

Dim dir As String
dir = txtPath.Text & i

We concatenate the string in txtPath.Text (the path where we want the directories to be created) with the number that the variable i currently holds.

Warning: Be sure the directory where you want the other directories to be created exists, else you will get an ugly error.

Here is the trick:
We suppose we have a directory named temp in the root of C:\ (C:\temp).
If in the txtPath we type C:\temp\ (notice the trailing slash), we will have directories 1, 2, 3… (till we reach the number typed in the txtNum) created in the folder C:\temp.
The path for the directories will be:

C:\temp\1
C:\temp\2
C:\temp\3

… and so on.

On the other hand, if we type C:\temp (notice there is no trailing slash), we will have the directories created in the root of C:\ with the prefix ‘temp’, like this:

C:\temp1
C:\temp2
C:\temp3

… and so on.
This is because when we concatenate using:

dir = txtPath.Text & i

we actually have dir = C:\temp1 because we didn’t add the trailing slash.

MkDir dir

Then we create the actually directory…

i = i + 1

Increment the variable so the loop isn’t endless…

Wend

And close the loop.

This is a basic way to create several folders using a prefix. From now on it’s easy to create directories using a prefix or a suffix, or both. Or following a pattern.

There is no need to explain the rest of the code in the program, like the one for deleting a directory because it’s very simple.
Remember! RmDir only works on empty directories

Nathan Pakovskie is an esteemed senior developer and educator in the tech community, best known for his contributions to Geekpedia.com. With a passion for coding and a knack for simplifying complex tech concepts, Nathan has authored several popular tutorials on C# programming, ranging from basic operations to advanced coding techniques. His articles, often characterized by clarity and precision, serve as invaluable resources for both novice and experienced programmers. Beyond his technical expertise, Nathan is an advocate for continuous learning and enjoys exploring emerging technologies in AI and software development. When he’s not coding or writing, Nathan engages in mentoring upcoming developers, emphasizing the importance of both technical skills and creative problem-solving in the ever-evolving world of technology. Specialties: C# Programming, Technical Writing, Software Development, AI Technologies, Educational Outreach

Leave a Reply

Your email address will not be published. Required fields are marked *

Back To Top