Rename A Directory Using
Rename A Directory Using
NET Program
Jennifer Lewis
Overview
While it’s quite simple to rename a directory using Windows Explorer, you may find a need to
programmatically rename a directory. For example, you may be creating a Setup and
Deployment project where you have to back up the contents of a directory before you install the
application, or you may need a way to rename directory with little to no human interaction.
This instruction will be using Visual Studio 2008 to write the code. However, you may use Visual
Studio 2005, SharpDevelop or the SDK and a text editor to write the code, but you may have to
alter the directions slightly for your development environment.
Instructions
1. Open Microsoft Visual Studio 2008
2. Select File – New – Project
4. Do the following:
a. In the Name field, name your project. In this illustration, the project will be called
BackupADirectory
b. In the Location field, select the directory where you want to put the project. If you
want to leave the project in the default directory displayed in this field, don’t
change this information. In this illustration, the project will be left in the default
directory.
c. If you want to create a separate directory for the solution, leave the Create
directory for solution box checked. In this illustration, the project will create a
directory for the solution.
5. Click OK
If you want to rename the Module1 to another name, you may do so. In this illustration,
Module1 will be renamed to BackupDir. If you use a different name, or you want to leave
the name as Module1, remember the name that you will be using. You will need this
name when you run this program.
6. The System.IO library contains the functions to allow program manipulation of directories.
Import the System.IO library in your code before the Module modName statement. Your
code should look similar to this:
Imports System.IO
Module BackupDir
Sub Main()
End Sub
End Module
7. In the Main() sub, define two variables and assign values to them:
a. strSourceDir – The “source” directory that will be “backed up”. This should have
a value of a directory on your machine that you would like to back up. This
directory must exist on your machine. In this instruction, there is a directory on
the machine called Bogus.
b. strTargetDir – The “back up” of the directory. This should have a value of what
you would like to call the “back up” directory. This directory does not have to
exist on your machine; the system will automatically create the directory.
Your code should look similar to this:
Imports System.IO
Module BackupDir
Sub Main()
Dim strSource As String = "C:\Bogus"
Dim strTarget As String = "C:\BogusBackup"
End Sub
End Module
8. The Directory class in the System.IO library contains functions that can be performed on
directories. Define an instance of the Directory class. In this illustration, the instance will
be named dirToBackup. Your code should look similar to this:
Imports System.IO
Module BackupDir
Sub Main()
Dim strSource As String = "C:\Bogus"
Dim strTarget As String = "C:\BogusBackup"
End Sub
End Module
9. The Move(sourceDir, targetDir) function in the Directory class renames the source
directory to the target directory. Call this function by passing strSource as the first
parameter and strTarget as the second parameter. Your code should look similar to this:
Imports System.IO
Module BackupDir
Sub Main()
Dim strSource As String = "C:\Bogus"
Dim strTarget As String = "C:\BogusBackup"
End Module
10. Compile your program. If you want to test your program, click the run button in the
toolbar.
As you can see in this example, the directory Bogus has been renamed to BogusBackup.
If you want the program to control the source and directory values, put the values of the “source”
and “target” directories in the configuration file
In Visual Studio 2005 and 2008, you can use My.Settings to set two values: sourceDir and
targetDir. To create settings:
1. Right-click on the project name in the Solution Explorer and select Properties
2. Click Settings
3. Add two settings: one to represent the source directory, and another to represent the
target directory. In this example, I will call the settings sourceDir and targetDir
4. Right-click on the tab and select Close. If you are prompted to save, click Yes.
5. In your code:
a. For the strSource variable, assign it the value of My.Settings(“sourceDir”), or
whatever you called the source directory setting.
b. For the strTarget variable, assign it the value of My.Settings(“targetDir”), or
whatever you called the target directory setting.
Your code should look similar to this:
Imports System.IO
Module BackupDir
Sub Main()
Dim strSource As String = My.Settings("sourceDir")
Dim strTarget As String = My.Settings("targetDir")
End Sub
End Module
If you want the user to control the source and directory values, accept parameters for the source
and the directory.
In order to accept parameters for your code, you have to make a few adjustments to your Main()
method, and you have to check for the parameters.
1. Add the following in between the parentheses in the Main() method: ByVal args() as
String. Your Main method declaration should look like this:
Sub Main(ByVal args() As String)
2. If you want to check to make sure that parameters were entered, type the following
immediately after the Main() declaration:
If args.Length = 0 Then
Console.WriteLine("Missing Parameters")
Console.WriteLine("syntax: BackupDir [sourcedirectory]
[targetdirectory]")
Console.ReadLine()
Exit Sub
End If
Note: If you called your module something other than BackupDir, enter that name.
3. In your code:
a. For the strSource variable, assign it the value of args(0).
b. For the strTarget variable, assign it the value of args(1).
Your code should look similar to this:
Imports System.IO
Module BackupDir
End Module
Perform a basic check to make sure the “source” directory actually exists
Before performing the Move function, check to see if the directory exists by using the Exists()
function of the Directory class. Here is a code snippet example:
If strToBackup.Exists(strSource) Then
strToBackup.Move(strSource, strTarget)
Else
Console.WriteLine("Directory " & strSource & " does not exist")
Console.ReadLine()
End If