File Folder Hirerchy to XML(vb.net)
Version: VB.NET 2003
Compatibility: VB.NET 2003, VB 2005
Category: File Manipulation
This code generates an XML file in the given directory with contents same as Explorer hierarchy. It also displays the files size and change date including folders description like Number of files or folders.
It can be used when you want to make a CD/DVD with many number of files and this xml file helps to find out the files/folders snapshots in a single look.
Declarations:
Imports System.IO
Dim Trgt As StreamWriter
Dim T1 As Threading.Thread
'Place on Textbox and and button. Give the path of
'the root folder in textbox1 and click the button.
Code:
Sub CreateXMLPath()
Trgt = New StreamWriter(TextBox1.Text & "\index.xml")
Trgt.WriteLine(" ")
Trgt.WriteLine("
Text = "Started"
Trgt.WriteLine("
Trgt.WriteLine("
ScanFolder(TextBox1.Text)
Trgt.WriteLine("
Trgt.Close()
Text = "Completed"
End Sub
Sub ScanFolder(ByVal FldName As String)
'On Error GoTo NextSub
Dim S As String
Dim TotFiles, TotFolders, TotSize As Long
Dim sZ As Double
Dim Fld As New DirectoryInfo(FldName), Fl As FileInfo
Dim MainFld As New DirectoryInfo(FldName)
Trgt.WriteLine("
For Each Fld In MainFld.GetDirectories
Text = "Scanning folder " & FldName
ScanFolder(Fld.FullName())
TotFolders += 1
Next
For Each Fl In MainFld.GetFiles
TotFiles += 1
S = Fl.FullName
If Fl.Name <> "index.xml" Then
sZ = Math.Round(Fl.Length / 1024, 2)
Trgt.WriteLine("
TotSize += Fl.Length
End If
Next
Trgt.WriteLine("
Trgt.WriteLine("
NextSub:
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
T1 = New Threading.Thread(AddressOf CreateXMLPath)
T1.Start()
End Sub
'Replace the invalid charactes for XML with valid one
Function ParseXML(ByVal S As String) As String
S = S.Replace("&", "&")
S = S.Replace("'", "")
S = S.Replace("""", "")
Return S
End Function
0 comments:
Post a Comment