Changes

Jump to: navigation, search

Working with Directories in Visual Basic

806 bytes added, 19:04, 10 August 2007
Extracting Parts of a Path Name in Visual Basic
== Extracting Parts of a Path Name in Visual Basic ==
it It is quite common to be passed a full path name by a Visual Basic method and to need only perhaps the file name or the directory path. Visual Basic provides easy access to each part of a path name as follows:
<pre>
Dim fileName As String = tmpFile.Name ' hget just the Filename part of the full pathname
MsgBox(fileName) ' Display it
</pre>
 
== Copying Directories in Visual Basic ==
 
A directory and all of the files therein may be copied to a new folder using the ''CopyDirectory()'' method:
 
<pre>
My.Computer.FileSystem.CopyDirectory("C:\Temp", "C:\Temp2", True)
</pre>
 
The ''True'' parameter tell Visual Basic to overwrite the target directory if it already exists. Setting this to ''False'' will prevent existing directories from being overwritten and instead, the contents of the two directories will be merged.
 
== Renaming a Directory in Visual Basic ==
 
To rename a directory using Visual Basic:
 
<pre>
My.Computer.FileSystem.MoveDirectory("C:\Temp", "C:\Temp2")
</pre>
 
The following code excerpt will overwrite the target directory if it already exists:
 
<pre>
My.Computer.FileSystem.MoveDirectory("C:\Temp", "C:\Temp2", True)
</pre>

Navigation menu