Selectfolder, how to parse folders inside a folder

Hi all!

How Can I parse subdirectories? For example: A folder contains three XML file, and two folders, Inside of these folders has one or more XML files.

  1. Why the select folder dialog its shows by twice?. So, I mean, when I select the folder and press OK, again the windows appear.

This is my code, I dunno If I’m wrong.
Regards :smiley:

Dim dlg As New SelectFolderDialog
dlg.ActionButtonCaption = “Seleccionar”
dlg.Title = “Lector de CFDis”
dlg.PromptText = “Seleccionar Directorio donde esten guardados los CFDiS”
dlg.InitialDirectory = SelectFolder
dlg.Filter = ftInvoice.InvoiceFile

Dim desktopfolder As FolderItem
desktopfolder = dlg.ShowModal

For i as Integer = 1 to desktopfolder.count

If desktopfolder <> Nil Then

if desktopfolder.item(i).type <> “” then

Dim xDocument As New XmlDocument
xDocument.LoadXml(desktopfolder.Item(i))

ProcessNode(xDocument.FirstChild)
ProcesarConceptos(xDocument)
GrabarCFDi
else
'msgbox “Type Null”

end if

Else
MsgBox “No hay archivo”
Return

// User cancelled
End If

Next

So, When It sees an XML only the XML will be processed, It will process each XML existent on the folder, can be 2 can be be 2,000.

Thanks in Advance

Please put desktopfolder.Item(i) in a local variable and not query it often.

Also, you’re checking desktopFolder <> Nil AFTER you are accessing the folder’s count (If it is nil, you would have crashed before you get to that line). Start the loop after you check for nil.

Just because the type <> “” does not mean it’s an xml file. In fact, I don’t know of any situation in which type will return “”
My previous instructions told you to check for whatever type you have declared your xml files to be in your FileTypes object.

If desktopFolder.item(i).type = "myXMLType" Then

myXMLType is just something I made up to use as an example.

Yeah The loop goes before, of course as it was,If desktopfolder = NIL it crashes.

So now the code is:

Dim dialog As SelectFolderDialog
Dim dir, file As FolderItem

// Create the dialog (does not actually show it)
dialog = New SelectFolderDialog

// The OpenDialog class supports custom prompts…
dialog.PromptText = “Selecciona el directorio a cargar donde residan los XMLs”

// …and custom title’s
dialog.Title = “Lector de CFDis”

//Filtra los puros XML
dialog.Filter = ftInvoice.InvoiceFile

// Also, you can display the dialogs as sheets
// by calling dialog.ShowModalWithin(Self)
//
// For regular modal dialogs, just call dlg.showModal
dir = dialog.ShowModal

Dim xDocument As New XmlDocument

If dir <> Nil Then

For i as Integer = 1 to dir.count
  if dir.item(i).type <> "" then
    
    
    xDocument.LoadXml(dir.Item(i))
    
    
    ProcessNode(xDocument.FirstChild)
    ProcesarConceptos(xDocument)
    GrabarCFDi
    
    
  end if
  
Next

Else
MsgBox “No hay archivo”
Return

// User cancelled

End If

[quote=193368:@Gerardo García]For i as Integer = 1 to dir.count
[/quote]
This is very expensive in disk I/O. Create a variable to hold the count then reference that in the for loop.