Treeview1 has a-z zip names and each zip has child items.
Treeview2 has same zip names but each extra unwanted child items may exist. I need to remove the extra items from Treeview2.
Here's a solution that should work, with useful functions for later in your project:
Private Sub PruneTreeView(ByRef p_objSourceTreeView As TreeView, ByRef p_objTargetTreeView As TreeView)
Dim objSourceNode As Node
Dim objMatchNode As Node
Dim objSourceChildNode As Node
Dim objTargetChildNode As Node
Dim fFound As Boolean
Dim iNodeIndex As Integer
Dim sSummary As String
' Build summary string
sSummary = "Prune TreeView Summary:" & vbCrLf
' Get first node from Source TreeView
Set objSourceNode = p_objSourceTreeView.Nodes(1)
Do While Not objSourceNode Is Nothing
' Check if node has children, otherwise no need to look for match
If objSourceNode.Children > 0 Then
' Find matching node in Target TreeView
Set objMatchNode = GetMatchingNode(p_objTargetTreeView, p_objTargetTreeView.Nodes(1), objSourceNode)
If Not objMatchNode Is Nothing Then
sSummary = sSummary & "Source Node '" & objSourceNode.Text & "' Found in Target." & vbCrLf
' Check all children in Target Node
If objMatchNode.Children > 0 Then
' Set Found flag to False
fFound = False
' Get first Child of Target
Set objTargetChildNode = objMatchNode.Child
Do While Not objTargetChildNode Is Nothing
' Look for match in Source Tree
fFound = Not GetMatchingNode(p_objSourceTreeView, objSourceNode.Child, objTargetChildNode) Is Nothing
' Keep Index reference
iNodeIndex = objTargetChildNode.Index
sSummary = sSummary & "Target Child Node '" & objMatchNode.Text & ":" & objTargetChildNode.Text & "'"
' Go to next sibling
Set objTargetChildNode = GetNextSibling(p_objTargetTreeView, objTargetChildNode)
If fFound Then
sSummary = sSummary & " Found in Target." & vbCrLf
Else
' No Match found
sSummary = sSummary & " Not Found in Target: Deleting at Index " & iNodeIndex & vbCrLf
p_objTargetTreeView.Nodes.Remove iNodeIndex
End If
DoEvents
Loop
End If
End If ' MatchNode exists
End If ' Source Node Children > 0
' Go to next sibling
Set objSourceNode = GetNextSibling(p_objSourceTreeView, objSourceNode)
Loop
End Sub
The following are Helper functions I wrote to handle common tasks while looking up nodes:
Function GetMatchingNode(ByRef p_objTreeView As TreeView, ByRef p_objStartNode As Node, ByRef p_objCompareToNode As Node) As Node
Dim objNode As Node
' Get First Node
Set objNode = p_objStartNode
' Check all Siblings and match on Text property
Do While Not objNode Is Nothing
If objNode.Text = p_objCompareToNode.Text Then
' Match found
Set GetMatchingNode = objNode
Exit Do
Else
Set objNode = GetNextSibling(p_objTreeView, objNode)
End If
Loop
Set GetMatchingNode = objNode
End Function
Function GetNextSibling(ByRef p_objTreeView As TreeView, ByRef p_objNode As Node) As Node
If HasSibling(p_objTreeView, p_objNode) Then
Set GetNextSibling = p_objTreeView.Nodes(GetNextSiblingIndex(p_objNode))
Else
Set GetNextSibling = Nothing
End If
End Function
Function HasSibling(ByRef p_objTreeView As TreeView, ByRef p_objNode As Node) As Boolean
HasSibling = Not (p_objNode.LastSibling Is p_objNode)
End Function
Function GetNextSiblingIndex(ByRef p_objNode As Node) As Integer
With p_objNode
GetNextSiblingIndex = .Index + .Children + 1
End With
End Function
all credits go to Étienne Laneville
Private Sub PruneTreeView(ByRef p_objSourceTreeView As TreeView, ByRef p_objTargetTreeView As TreeView)
Dim objSourceNode As Node
Dim objMatchNode As Node
Dim objSourceChildNode As Node
Dim objTargetChildNode As Node
Dim fFound As Boolean
Dim iNodeIndex As Integer
Dim sSummary As String
' Build summary string
sSummary = "Prune TreeView Summary:" & vbCrLf
' Get first node from Source TreeView
Set objSourceNode = p_objSourceTreeView.Nodes(1)
Do While Not objSourceNode Is Nothing
' Check if node has children, otherwise no need to look for match
If objSourceNode.Children > 0 Then
' Find matching node in Target TreeView
Set objMatchNode = GetMatchingNode(p_objTargetTreeView, p_objTargetTreeView.Nodes(1), objSourceNode)
If Not objMatchNode Is Nothing Then
sSummary = sSummary & "Source Node '" & objSourceNode.Text & "' Found in Target." & vbCrLf
' Check all children in Target Node
If objMatchNode.Children > 0 Then
' Set Found flag to False
fFound = False
' Get first Child of Target
Set objTargetChildNode = objMatchNode.Child
Do While Not objTargetChildNode Is Nothing
' Look for match in Source Tree
fFound = Not GetMatchingNode(p_objSourceTreeView, objSourceNode.Child, objTargetChildNode) Is Nothing
' Keep Index reference
iNodeIndex = objTargetChildNode.Index
sSummary = sSummary & "Target Child Node '" & objMatchNode.Text & ":" & objTargetChildNode.Text & "'"
' Go to next sibling
Set objTargetChildNode = GetNextSibling(p_objTargetTreeView, objTargetChildNode)
If fFound Then
sSummary = sSummary & " Found in Target." & vbCrLf
Else
' No Match found
sSummary = sSummary & " Not Found in Target: Deleting at Index " & iNodeIndex & vbCrLf
p_objTargetTreeView.Nodes.Remove iNodeIndex
End If
DoEvents
Loop
End If
End If ' MatchNode exists
End If ' Source Node Children > 0
' Go to next sibling
Set objSourceNode = GetNextSibling(p_objSourceTreeView, objSourceNode)
DoEvents
Loop
End Sub
Related
I want to scan and match items of treeview1 in treeview2 and add child items to treeview2.
Here's my current code:
Dim FoundIt As Boolean, ii As Integer, ix As Integer
Dim NodX As Node, NodX2 As Node, namme As String
On Error Resume Next
For Each NodX In TreeView1.Nodes
ii = NodX.Index
For Each NodX2 In TreeView2.Nodes
ix = NodX.Index
On Error Resume Next
If NodX.FullPath = NodX2.FullPath Then
If TreeView1.Nodes(ii).Parent.Text = TreeView2.Nodes(ix).Parent.Text Then
If TreeView1.Nodes(ii).Parent.Image = 9 And TreeView1.Nodes(ii).Image = 3 Then
namme = TreeView2.Nodes(ix).Parent.Key
TreeView2.Nodes.Add namme, tvwChild, TreeView1.Nodes(ii).Parent.Child.Text, TreeView1.Nodes(ii).Parent.Child.Text, 5
Pause 0
End If
End If
End If
'Exit For
Next
Next
next
Currently, treeview1 items may have parent image as 9 and child as 3
so based on that I want to add all items that has image index 3 to treeview2 matching parent child section for each.
enter image description here
Here is code that should get you pretty close to what you want:
Dim objNode1 As Node
Dim objNode2 As Node
Dim objMatchNode As Node
Dim objChildNode1 As Node
Dim objChildNode2 As Node
Dim iCounter1 As Integer
Dim iCounter2 As Integer
Dim fFound As Boolean
On Error Resume Next
For Each objNode1 In TreeView1.Nodes
' Find matching node in Treeview2
For Each objNode2 In TreeView2.Nodes
If objNode2.Text = objNode1.Text Then
' Match found
Set objMatchNode = objNode2
Exit For
End If
Next
If Not objMatchNode Is Nothing Then
' Check all children
If objNode1.Children > 0 Then
' Get first Child
Set objChildNode1 = objNode1.Child
' Loop through all children
For iCounter1 = 1 To objNode1.Children
If objChildNode1.Image = 3 And objNode1.Image = 9 Then
' Check if it already exists in Treeview2
If objMatchNode.Children > 0 Then
' Get first Child
Set objChildNode2 = objMatchNode.Child
' Set Found flag to False
fFound = False
' Loop through all children
For iCounter2 = 1 To objMatchNode.Children
' Check for match
If objChildNode2.Text = objChildNode1.Text Then
fFound = True
Exit For
End If
' Get next node
Set objChildNode2 = objChildNode2.Next
Next
If Not fFound Then
' Add to Treeview2
TreeView2.Nodes.Add objMatchNode.Key, tvwChild, objChildNode1.Key, objChildNode1.Text, 3
End If
End If
End If
' Get next node
Set objChildNode1 = objChildNode1.Next
Next
End If
End If
' Give UI some time to do other things
DoEvents
Next
This is the code i have, after execution it starts to delete unmatched items from treeview2.
if some zip files does not contain matched files than the contents of zip file gets deleted but zip remains with the size of 1k and its empty.
Instead of leaving the dead empty zip can i rather move the zip to newly created folder in the zip path and leave the contents as it is and move on.
Private Sub Command9_Click()
Dim objNode1 As Node
Dim objNode2 As Node
Dim objMatchNode As Node
Dim objChildNode1 As Node
Dim objChildNode2 As Node
Dim iCounter1 As Integer
Dim iCounter2 As Integer
Dim fFound As Boolean
On Error Resume Next
For Each objNode1 In TreeView2.Nodes
' Find matching node in Treeview2
For Each objNode2 In TreeView1.Nodes
If objNode2.Text = objNode1.Text Then
' Match found
Set objMatchNode = objNode2
Exit For
End If
Next
If Not objMatchNode Is Nothing Then
' Check all children
If objNode1.Children > 0 Then
' Get first Child
Set objChildNode1 = objNode1.Child
' Loop through all children
For iCounter1 = 1 To objNode1.Children
'If objChildNode1.Image = 3 And objNode1.Image = 9 Then
' Check if it already exists in Treeview2
If objMatchNode.Children > 0 Then
' Get first Child
Set objChildNode2 = objMatchNode.Child
' Set Found flag to False
fFound = False
' Loop through all children
For iCounter2 = 1 To objMatchNode.Children
' Check for match
If objChildNode2.Text = objChildNode1.Text Then
fFound = True
Exit For
End If
' Get next node
Set objChildNode2 = objChildNode2.Next
DoEvents
Next
If fFound Then
' Add to Treeview2
'TreeView2.Nodes.Add objMatchNode.Key, tvwChild, objChildNode1.Key, objChildNode1.Text, 3
Else
DeleteFileFromArchive objChildNode1.Text, "C:\Users\sarah\Desktop\rom test\" & objNode2.Text
End If
End If
' End If
' Get next node
Set objChildNode1 = objChildNode1.Next
DoEvents
Next
End If
End If
Next
End Sub
The following code will find the nodes that are empty and delete them. You can add code to delete the actual Zip file in here, where it says "Delete Zip":
Private Sub DeleteFromTreeView(ByRef p_objTreeView As TreeView)
Dim objNode As Node
Dim fDelete As Boolean
Dim iDeleteIndex As Integer
Dim sDeleteName As String
' Get first node from TreeView
Set objNode = p_objTreeView.Nodes(1)
Do While Not objNode Is Nothing
' Set Delete flag to false
fDelete = False
' Check if node has children, otherwise delete file
If objNode.Children = 0 Then
fDelete = True
iDeleteIndex = objNode.Index
sDeleteName = objNode.Text
End If
' Go to next sibling
Set objNode = GetNextSibling(p_objTreeView, objNode)
If fDelete Then
' Delete Zip
p_objTreeView.Nodes.Remove iDeleteIndex
End If
Loop
End Sub
You can run this code on your TreeView after your existing code. The sDeleteName will contain the name of the Zip you want to delete, just add some code to delete the file using something like this:
Sub DeleteFile(p_sFilePath)
Dim objFSO As New FileSystemObject
If objFSO.FileExists(p_sFilePath) Then objFSO.DeleteFile p_sFilePath
End Sub
This Sub uses the FileSystemObject so make sure you add a reference to Microsoft Scripting Runtime in your project.
You will also need the following Helper functions you might already have in your project:
Function GetNextSibling(ByRef p_objTreeView As TreeView, ByRef p_objNode As Node) As Node
If HasSibling(p_objTreeView, p_objNode) Then
Set GetNextSibling = p_objTreeView.Nodes(GetNextSiblingIndex(p_objNode))
Else
Set GetNextSibling = Nothing
End If
End Function
Function HasSibling(ByRef p_objTreeView As TreeView, ByRef p_objNode As Node) As Boolean
HasSibling = Not (p_objNode.LastSibling Is p_objNode)
End Function
Function GetNextSiblingIndex(ByRef p_objNode As Node) As Integer
With p_objNode
GetNextSiblingIndex = .Index + .Children + 1
End With
End Function
Private Sub btnCreateTreeData(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCreateTreeData.Click
'create buffer for storing string data
Dim buffer As New System.Text.StringBuilder
'loop through each of the treeview's root nodes
For Each rootNode As TreeNode In yourTreeView.Nodes
'call recursive function
BuildTreeString(rootNode, buffer)
Next
'write data to file
IO.File.WriteAllText("C:\treeTest.txt", buffer.ToString)
End Sub
file create successful but no tree node there
Here I get tree nods successfully
Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
For Each nd As TreeNode In TreeView1.Nodes
If nd.Nodes.Count > 0 Then 'it has children, lets look at them
For Each ndChild As TreeNode In nd.Nodes
If ndChild.Nodes.Count > 0 Then 'it has children, lets look at them
Dim outputText As String = String.Concat(ndChild.Text, " ", ndChild.Nodes.Count)
For Each ndSubChild As TreeNode In ndChild.Nodes
outputText = String.Concat(outputText, " ", ndSubChild.Text)
TextBox4.Text += vbTab & outputText & vbNewLine
Next
Debug.Print(outputText)
'TextBox4.Text += vbTab & vbTab & vbTab & vbTab & outputText
End If
Next
End If
Next
End Sub
I can save it to a text file as :
Dim Save As New SaveFileDialog()
Dim myStreamWriter As System.IO.StreamWriter
Save.Filter = "Text [*.txt*]|*.txt|All Files [*.*]|*.*"
Save.CheckPathExists = True
Save.Title = "Save File"
Save.FileName = My.Computer.FileSystem.SpecialDirectories.Desktop & "\Treee Data"
Save.ShowDialog(Me)
Try
myStreamWriter = System.IO.File.AppendText(Save.FileName)
myStreamWriter.Write(TextBox4.Text)
myStreamWriter.Flush()
Catch ex As Exception
End Try
Dim ProcessProperties As New ProcessStartInfo
ProcessProperties.FileName = "notepad"
ProcessProperties.Arguments = Save.FileName
ProcessProperties.WindowStyle = ProcessWindowStyle.Maximized
Dim myProcess As Process = Process.Start(ProcessProperties)
The script I have here is attempting to do recurse through an XML file, storing each RegEx match (stored in a search array) into 2 result arrays; 1 for start date, 1 for end date.
Ubounds of both arrays are checked for equality then the text is passed to a function that uses XMLDOM to find the End_Date node in each parent node, then passes that text to another function, adding 30 days and then passing it back, replacing the previous value. Then it's supposed to write back the contents to the file and save it.
I've got a few problems here. 1. I can't get the +30 day value to be passed back to anything past the first parent node--memory space seems to retain the +30 day value from previous For-Each iteration. 2. I can't write anything back to the file.
I was initially writing for text files, but the format changed to XML as the requirements changed on our project.
I'd love to be able to do this all in XMLDOM in vbscript and just use functions to do specific data changes. But my main concern is my sloppy script not doing the basics.
Can anyone help me by pointing out the flaws in the loops I'm running? I've hit a wall and just can't seem to make any more progress!
Here's the XML file I'm reading(shortened to 2 Ad nodes w/ a ton of child nodes removed):
<?xml version="1.0" encoding="utf-8"?>
<XMLFeederRoot>
<ADS_CREATE_TIME>2016-06-07T01:35:39</ADS_CREATE_TIME>
<Ad>
<Ad_Number>d00524224</Ad_Number>
<Start_Date>2016-08-20T00:00:00</Start_Date>
<End_Date>2016-08-20T00:00:00</End_Date>
<Status>Run</Status>
</Ad><Ad>
<Ad_Number>d00524225</Ad_Number>
<Start_Date>2016-08-20T00:00:00</Start_Date>
<End_Date>2016-08-20T00:00:00</End_Date>
<Status>Run</Status>
</Ad>
</XMLFeederRoot>
Here's the script:
'Setting the Regular Expression object and setting occurrences to all in strings searched.
Set objRegEx= CreateObject("VBScript.RegExp")
objRegEx.Global= True
set Shell= createobject("wscript.shell")
Dim FSO, FLD, FIL, TS, strDate, strEDat, i, d, c
Dim strFolder, strContent, strPath
Const ForReading= 1, ForWriting= 2
strFolder= "C:\Scripts\Run"
Set FSO= CreateObject("Scripting.FileSystemObject")
'Get a reference to the folder you want to search
set FLD= FSO.GetFolder(strFolder)
'loop through the folder and get the files
For Each Fil In FLD.Files
'Open the file to read
Set TS= FSO.OpenTextFile(fil.Path, ForReading)
'Read the contents into a variable
strContent= TS.ReadAll
'Close the file
TS.Close
reDim arrMR(1,1)
arrMR(0,0)= "(\s+)(<Start_Date>(.*?)<\/Start_Date>)"
arrMR(1,0)= "(\s+)(<End_Date>(.*?)<\/End_Date>)"
For i= 0 to Ubound(arrMR)
objRegEx.Pattern= arrMR(i,0)
Set objMatches= objRegEx.Execute(strContent)
d=0
For Each objMatch in objMatches
If i= 0 Then
If d>0 Then
reDim Preserve arrStart(d)
Else
reDim arrStart(d)
End If
arrStart(d)= objMatches.Item(d).SubMatches(2)
'Wscript.Echo arrStart(d)
ElseIf i<> 0 Then
If d>0 Then
reDim Preserve arrEnd(d)
ReDim Preserve arrMatch1(d)
Else
reDim arrEnd(d)
ReDim arrMatch1(d)
End If
arrEnd(d)= objMatches.Item(d).SubMatches(2)
arrMatch1(d)= objMatches.Item(d).SubMatches(1)
End If
If objRegEx.Pattern<> arrMR(0,0) Then
If (ubound(arrStart)= ubound(arrEnd)) Then
'Wscript.Echo "Ubounds Match"
Parse strContent
strContent= Parse(strContent)
Else
'Wscript.Echo "Start & End Dates do not match"
End If
End If
d= d+ 1 'increment to next match
Next
Next
'Close the file
TS.Close
'Open the file to overwrite the contents
Set TS= FSO.OpenTextFile(fil.Path, ForWriting)
'Write the contents back
TS.Write strContent
'Close the current file
TS.Close
Next
'Clean up
Set TS= Nothing
Set FLD= Nothing
Set FSO= Nothing
Function Parse(ParseContent)
'Dim sFSpec : sFSpec = FSO.GetAbsolutePathName("C:\Users\j.levine\Desktop\XML Feeder Scripts\Test_Files\monvid.txt")
Dim oXML : Set oXML = CreateObject("Msxml2.DOMDocument.6.0")
Dim strXMLSDat, strXMLarrStartD, XMLEDat
oXML.setProperty "SelectionLanguage", "XPath"
oXML.async = False
oXML.loadXML(ParseContent)
If 0 = oXML.parseError Then
Dim sXPath3 : sXPath3 = "//XMLFeederRoot/Ad[End_Date=Start_Date]"
Dim ndlFnd : Set ndlFnd = oXML.selectNodes(sXPath3)
If 0 = ndlFnd.length Then
WScript.Echo sXPath, "not found"
ElseIf 0<> ndlFnd.length Then
'WScript.Echo "found", ndlFnd.length, "nodes for", sXPath
Dim ndCur, oldNode
For Each ndCur In ndlFnd
oldNode = oXML.selectsinglenode("//End_Date").text
oldNode= XMLSplitArray(oldNode) 'Pass current Date into Array and add 30 days & return as node text
Set newNode= oXML.selectSingleNode("//End_Date")
newNode.text= oldNode
WScript.Echo ndCur.xml
Next
'WScript.Echo "We have nothing to replace"
End If
Else
WScript.Echo oXML.parseError.reason
End If
Parse= ParseContent
End Function
Function XMLSplitArray(strval1)
dim XmlSA, XmlSA2, XMLEDat
XmlSA = split(strval1, "-")
XmlSA(2) = Left(XmlSA(2), 2)
strXMLDate = XmlSA(1) & "/" & XmlSA(2) & "/" & XmlSA(0)
strXMLDate30 = DateAdd("d", 30, strXMLDate)
XmlSA2 = split(strXMLDate30, "/")
'Add zero to the left
XmlSA2(0)= Right("0" & XmlSA2(0), 2)
XmlSA2(1)= Right("0" & XmlSA2(1), 2)
XmlSA2(1) = XmlSA2(1) & "T00:00:00"
XMLEDat = XmlSA2(2) & "-" & XmlSA2(0) & "-" & XmlSA2(1)
XMLSplitArray= XMLEDat
End Function
Thomas was right w/ the KISS method. I took a big step back, and started over.
Here's what I've come up with. It does what I need regarding the Date+30 and writing back to a file. I think this method is cleaner and will allow me to run my other text massaging through functions.
My questions about this new script are:
1. can this be done without having to write to a new file? Keeping to 1 file is easier.
2. Can I avoid cloning the node and deleting the original one and directly change the original node's value?
3. I seem to be missing how to get that last node <Status> onto it's own line.
Script:
Dim xmlDoc: Set xmlDoc = CreateObject("Msxml2.DOMDocument")
xmlDoc.Async = False
xmlDoc.load "C:\Scripts\Run\MonVid-SHORT.xml"
Dim xmldoc2: set xmldoc2 = CreateObject("Msxml2.DOMDocument")
Dim strSkeleton : strSkeleton= "<?xml version=""1.0"" encoding=""utf-8""?>" & _
"<XMLFeederRoot>" & _
"</XMLFeederRoot>"
xmldoc2.loadXML(strSkeleton)
xmldoc2.save "C:\Scripts\Copy\New_MonVid-Short.xml"
xmlDoc2.async = False
xmlDoc2.load "C:\Scripts\Copy\New_MonVid-Short.xml"
Dim sXPath : sXPath = "/XMLFeederRoot/Ad[Start_Date=End_Date]"
For Each n In XMLDoc.SelectNodes(sXpath)
set l = n.cloneNode(True)
q= l.selectSingleNode("/End_Date").text
strSDat=SplitArray(q)
l.removeChild(l.childNodes.item(2))
set Stat= l.selectSingleNode("/Status")
set Parent= Stat.parentNode
set EDate= xmlDoc2.createElement("End_Date")
EDate.appendChild xmlDoc2.createTextNode(strSDat)
Parent.insertBefore EDate, Stat
xmldoc2.documentElement.appendChild parent
Next
xmlDoc2.save xmldoc2.url
Function SplitArray(strval1)
dim SplitArray1, SplitArray2, strSDat
splitArray1 = split(strval1, "-")
splitArray1(2) = left(splitArray1(2), 2)
strDate1 = SplitArray1(1) & "/" & SplitArray1(2) & "/" & SplitArray1(0)
strDate30 = DateAdd("d", 30, strDate1)
SplitArray2 = split(strDate30, "/")
'Add zero to the left
If Len(SplitArray2(0))<2 Then
SplitArray2(0)= Right("0" & SplitArray2(0), 2)
End If
If Len(SplitArray2(1))<2 Then
SplitArray2(1)= Right("0" & SplitArray2(1), 2)
End If
SplitArray2(1) = splitArray2(1) & "T00:00:00"
strSDat = SplitArray2(2) & "-" & SplitArray2(0) & "-" & SplitArray2(1)
SplitArray= strSDat
End Function
Output File:
<?xml version="1.0" encoding="utf-8"?>
<XMLFeederRoot><Ad>
<Ad_Number>d00524224</Ad_Number>
<Start_Date>2016-08-20T00:00:00</Start_Date>
<End_Date>2016-09-19T00:00:00</End_Date><Status>Run</Status>
</Ad><Ad>
<Ad_Number>d00524225</Ad_Number>
<Start_Date>2016-08-20T00:00:00</Start_Date>
<End_Date>2016-09-19T00:00:00</End_Date><Status>Run</Status>
</Ad>
</XMLFeederRoot>
Since Text and XML files don't use file locks by default just overwrite the original file using xmlDoc.Save monvidPath.
Sub setAdEndDate(monvidPath)
Const sXPath = "/XMLFeederRoot/Ad/End_Date"
Set xmlDoc = CreateObject("Microsoft.XMLDOM")
xmlDoc.Async = "False"
xmlDoc.Load(monvidPath)
Set colNodes=xmlDoc.selectNodes(sXPath)
For Each n In colNodes
n.Text = SplitArray(n.Text)
Next
xmlDoc.Save monvidPath
End Sub
Function SplitArray(strval1)
Dim SplitArray1, SplitArray2, strSDat
splitArray1 = Split(strval1, "-")
splitArray1(2) = Left(splitArray1(2), 2)
strDate1 = SplitArray1(1) & "/" & SplitArray1(2) & "/" & SplitArray1(0)
strDate30 = DateAdd("d", 30, strDate1)
SplitArray2 = Split(strDate30, "/")
'Add zero to the left
If Len(SplitArray2(0))<2 Then
SplitArray2(0)= Right("0" & SplitArray2(0), 2)
End If
If Len(SplitArray2(1))<2 Then
SplitArray2(1)= Right("0" & SplitArray2(1), 2)
End If
SplitArray2(1) = splitArray2(1) & "T00:00:00"
strSDat = SplitArray2(2) & "-" & SplitArray2(0) & "-" & SplitArray2(1)
SplitArray= strSDat
End Function
can any one help me to add all the drives of my computer in tree view..
Dim fs As New FileSystemObject
Private Sub Form_Load()
Dim path As String
path = "D:\MP3"
TreeView1.Nodes.Add , , path, path
Call addtotree(path, TreeView1)
End Sub
Private Sub addtotree(path As String, tv As TreeView)
Dim folder1 As Folder
For Each folder1 In fs.GetFolder(path).SubFolders
tv.Nodes.Add path, tvwChild, path & "\" & folder1.Name, folder1.Name
Call addtotree(path & "\" & folder1.Name, tv)
Next
End Sub
i am doing like this to add nodes and sub nodes but i don't know how to add dynamically all the drives and folder like window explorer.
I'm not sure from your code sample what you're trying to do. If you want to add the drives to your treeview iterate the FileSystemObject.Drives collection. If you are trying to populate the folders under the drives, get the drives, and as the user expands them find the folders under them. Here is a sample that gets drives.
Option Explicit
Private Const EXPANDING = " (expanding...)"
Private Sub LoadDrives(ByVal TreeviewCtrl As TreeView)
Dim objFso As FileSystemObject
Dim objDrive As Drive
Dim objNode As MSComctlLib.Node
On Error GoTo errLoadDrives
Me.MousePointer = vbHourglass
TreeviewCtrl.Nodes.Clear
Set objFso = New FileSystemObject
For Each objDrive In objFso.Drives
Set objNode = TreeView1.Nodes.Add(, tvwFirst, objDrive.Path, objDrive.Path & "\" & IIf(Len(objDrive.ShareName) > 0, " (" & Replace$(objDrive.ShareName, "\\", "") & ")", ""))
If objDrive.IsReady Then
If objDrive.RootFolder.SubFolders.Count > 0 Then
TreeviewCtrl.Nodes.Add objNode, tvwChild
End If
End If
Next objDrive
Me.MousePointer = vbDefault
Exit Sub
errLoadDrives:
Set objFso = Nothing
Me.MousePointer = vbDefault
End Sub
Private Sub TreeView1_Expand(ByVal Node As MSComctlLib.Node)
On Error GoTo errTreeView1_Expand
Me.MousePointer = vbHourglass
Node.Text = Node.Text & EXPANDING ' user feedback for longer operations
TreeView1.Refresh
Call AddToTree(Node)
Node.Text = Replace$(Node.Text, EXPANDING, "")
Me.MousePointer = vbDefault
Exit Sub
errTreeView1_Expand:
Me.MousePointer = vbDefault
MsgBox "There was an error getting the child folders." & vbCrLf & vbCrLf & "Error " & CStr(Err.Number) & ", " & Err.Description, vbOKOnly + vbCritical, Err.Source
End Sub
Private Sub AddToTree(ByVal Node As MSComctlLib.Node)
Dim strPath As String
Dim objParentNode As MSComctlLib.Node
Dim objFso As FileSystemObject
Dim objFolder As Folder
Dim objSubFolder As Folder
Dim objFile As File
Dim objNode As MSComctlLib.Node
On Error GoTo errAddToTree
' remove any place holder node
If Node.Child.Key = "" Then
TreeView1.Nodes.Remove Node.Child.Index
End If
strPath = Node.Key & "\" ' get the path of the current node
Set objFso = New FileSystemObject
Set objFolder = objFso.GetFolder(strPath)
For Each objSubFolder In objFolder.SubFolders
Set objNode = TreeView1.Nodes.Add(Node, tvwChild, objSubFolder.Path, objSubFolder.Name)
If objSubFolder.SubFolders.Count > 0 Or objSubFolder.Files.Count > 0 Then ' add an empty place holder node
TreeView1.Nodes.Add objNode, tvwChild
End If
Next objSubFolder
For Each objFile In objFolder.Files
TreeView1.Nodes.Add Node, tvwChild, Node.Key & "\" & objFile.Name, objFile.Name, "leaf"
Next objFile
Node.EnsureVisible
Exit Sub
errAddToTree:
If Err.Number = 70 Then 'permission denied - ignore it and move on
Resume Next
End If
End Sub