Rename a Directory Using VB.net

9
Date Written: 18 March 2009 Rename a Directory using a VB.NET Program Jennifer Lewis

description

This document is a step-by-step tutorial on how to rename a directory using VB.NET. This document also contains a section on ways to improve the functionality.

Transcript of Rename a Directory Using VB.net

Page 1: Rename a Directory Using VB.net

Date Written: 18 March 2009

Rename a Directory using a VB.NET Program

Jennifer Lewis

Page 2: Rename a Directory Using VB.net

Rename a Directory using VB.Net Page 2

Date Written: 18 March 2009

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

3. Under Visual Basic, select Console Application

Page 3: Rename a Directory Using VB.net

Rename a Directory using VB.Net Page 3

Date Written: 18 March 2009

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

Page 4: Rename a Directory Using VB.net

Rename a Directory using VB.Net Page 4

Date Written: 18 March 2009

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

Page 5: Rename a Directory Using VB.net

Rename a Directory using VB.Net Page 5

Date Written: 18 March 2009

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" Dim strToBackup As Directory 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" Dim strToBackup As Directory strToBackup.Move(strSource, strTarget) End Sub

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.

Ways to Enhance the Program The purpose of this illustration was to give a simple demonstration of how to rename a directory using VB. NET. In a practical situation, you may not want to “hard code” the directories in the variables, and you may not want to assume that the “source” directory exists. This section demonstrates how to make the code more practical. 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

Page 6: Rename a Directory Using VB.net

Rename a Directory using VB.Net Page 6

Date Written: 18 March 2009

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

Page 7: Rename a Directory Using VB.net

Rename a Directory using VB.Net Page 7

Date Written: 18 March 2009

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

Page 8: Rename a Directory Using VB.net

Rename a Directory using VB.Net Page 8

Date Written: 18 March 2009

Module BackupDir

Sub Main() Dim strSource As String = My.Settings("sourceDir")

Dim strTarget As String = My.Settings("targetDir") Dim strToBackup As Directory strToBackup.Move(strSource, strTarget)

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

Sub Main(ByVal args() As String)

If args.Length = 0 Then Console.WriteLine("Missing Parameters") Console.WriteLine("syntax: BackupDir [sourcedirectory] [targetdirectory]") Console.ReadLine() Exit Sub End If Dim strSource As String = args(0) Dim strTarget As String = args(1) Dim strToBackup As Directory strToBackup.Move(strSource, strTarget) End Sub

End Module

Page 9: Rename a Directory Using VB.net

Rename a Directory using VB.Net Page 9

Date Written: 18 March 2009

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 Add exception handling For a more robust error check, put the Move() function in a Try-Catch loop so you can handle any error that occurs, including a “Directory Does Not Exist” error. Here is a code snippet of the simplest error trapping: Try strToBackup.Move(strSource, strTarget) Console.WriteLine("Directory renamed") Console.ReadLine() Catch nex As DirectoryNotFoundException ' Directory doesn't exist Console.WriteLine("Source Directory Does Not Exist") Console.ReadLine() Catch ex As Exception ' All other errors Console.WriteLine(ex.Message) Console.ReadLine() End Try Display status messages The Console.WriteLine() function will write a message in the command line interface, while the Console.ReadLine() function will wait for user input before continuing (ex: “press a key to continue”). You may want to display messages when there was an error with the process, or if the process completed successfully.