How to get the Items inside a Folder

Hi all!

I have a little question.

How did I count the files existing on a folder?, for make a loop and do an instruction

Regards

FolderItem has a Count property to give you the count of files contained in a folder and the Child method to iterate over the contents of the folder.

Thanks, I gonna read it, in order to know How to load each file existing on a folder

It works!!!

Dim desktopFolder As FolderItem = SelectFolder
If desktopFolder Is Nil Then
Return
End If

Dim count As Integer = desktopFolder.Count
For i As Integer = 1 to count
Dim f As FolderItem = desktopFolder.Item(i)
If f <> Nil Then
MsgBox f.Name

  Dim xDocument As New XmlDocument
  xDocument.LoadXml(f)
  
  
  ProcessNode(xDocument.FirstChild)
  ProcesarConceptos(xDocument)
  GrabarCFDi
  
  
  
End If

Next

I dunno whats Happen It worked, and now no more, What Am I doing wrong?

I saw whats Happen, when It Loads a folder with only XML, it works,but when It finds another type of file it causes an XMLException.
How can I do a filter and a custom dialog?

[quote]Dim count As Integer = desktopFolder.Count
For i As Integer = 1 to count
Dim f As FolderItem = desktopFolder.Item(i)
If f <> Nil Then
MsgBox f.Name [/quote]

First of all, you don’t need f. You have desktopFolder which is a folderitem ( I assume since you are using its count property)
You can to desktopFolder.item(i).name to get the name if you need it.

Then be sure to check the type
desktopFolder.item(i).Type = “myXML” //or whatever you have declared in your fileTypes for your xml to make sure you are dealing with an xml file before proceeding with your code

[quote=192866:@Roger Clary]First of all, you don’t need f. You have desktopFolder which is a folderitem ( I assume since you are using its count property)
You can to desktopFolder.item(i).name to get the name if you need it.

Then be sure to check the type
desktopFolder.item(i).Type = “myXML” //or whatever you have declared in your fileTypes for your xml to make sure you are dealing with an xml file before proceeding with your code[/quote]
Thanks man, that’s what I’m looking for

Of course, I don’t need F because I have Desktopfolder.
But the Issue is already the FileType, If inside of the folder exists a file different to XML, it crashes causing a XML Exception, this is my Code:

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
  // Use the folderitem here
  Dim xDocument As New XmlDocument
  xDocument.LoadXml(desktopfolder.Item(i))
  
  
  ProcessNode(xDocument.FirstChild)
  ProcesarConceptos(xDocument)
  GrabarCFDi
  
Else
  MsgBox "Ningún archivo seleccionado"
  Return
  
  // User cancelled
End If

Next

I made an “If Type” inside of, like this way:

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.

Now I Have 2 little questions:

  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.

  2. 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.

Thanks in Advance