Saturday, 26 January 2013

Create a Directory Structure VB.NET

Create a Directory Structure VB.NET

Creating a Empty Directory Structure using Vb.NET, Directories and Sub directories with in specified source directory,

Shared  Sub CreateDirectoryStructure(ByVal szDir As String, ByVal destDir As String)
Try

'for C drive root path is C:\

'for D drive root path is D:\
'Replace Source path root with destination path root.

Dim destpathroot As String =  Path.GetPathRoot(destDir)

Dim srcpathroot As String =  Path.GetPathRoot(szDir)
Dim dirs() As String =  Directory.GetDirectories(szDir,"*",SearchOption.AllDirectories)
Dim difVolume As String
For Each difVolume In dirs

Dim path As String =  difVolume.Replace(srcpathroot,destpathroot)
If Not Directory.Exists(path) Then
Directory.CreateDirectory(path)
End If
Next


Catch ex As Exception
Console.WriteLine(ex.Message)
End Try
End Sub
for ex:

Public Shared  Sub Main()

'Creates a Empty Directory Structure of Inetpub located in C drive in D:\
CreateDirectoryStructure("c:\inetpub","d:\")
Console.Read()
End Sub


Tags:Create a Directory Structure VB.NET,Replicate folder Structure,Create Directory Structure,Folder Recursion,System.IO.Directory,System.IO.DirectoryInfo,Replicates directory Structure VB.NET

No comments:

Post a Comment